-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathresolve-initial-context.ts
More file actions
57 lines (53 loc) · 2.61 KB
/
Copy pathresolve-initial-context.ts
File metadata and controls
57 lines (53 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { ModulesInstance } from '@equinor/fusion-framework-module';
import type { ContextModule } from '../module';
import type { ContextModuleConfig } from '../ContextModuleConfig';
import { concat, EMPTY, first, of } from 'rxjs';
import { type ContextPathResolveArgs, resolveContextFromPath } from './resolve-context-from-path';
import type { NavigationModule } from '@equinor/fusion-framework-module-navigation';
/**
* Resolves the initial context from the parent module.
*
* @param ref - parent modules.
* @returns An Observable of the resolved initial context.
*/
export const resolveContextFromParent: ContextModuleConfig['resolveInitialContext'] = ({ ref }) => {
const parentContext = (ref as ModulesInstance<[ContextModule]>)?.context;
// check if the parent has context module
if (!parentContext) {
throw Error(['resolveContextFromNavigation', 'ref does not support context!'].join('\n'));
}
// return the current context from the parent or empty if the parent does not have a context
return parentContext.currentContext ? of(parentContext.currentContext) : EMPTY;
};
/**
* Resolves the initial context for a Fusion Framework context module.
*
* will try to resolve the initial context from the path, and if that fails, it will try to resolve the context from the parent.
*
* @param options - Optional configuration for resolving the context path.
* @returns A function that accepts the module's reference and modules, and returns an Observable of the resolved initial context.
*/
// Deliberately co-located with resolveContextFromParent, which it composes with
// fusion-lint-disable-next-line single-export-per-file
export const resolveInitialContext =
(options?: {
path?: ContextPathResolveArgs;
}): Required<ContextModuleConfig>['resolveInitialContext'] =>
({ ref, modules }) => {
const { context, navigation } = modules;
// create a path resolver from the context module
const pathResolver = resolveContextFromPath(context, options?.path);
// use the path from the navigation module, or the path from the parent navigation module
const pathname =
navigation?.path.pathname ??
(ref as Partial<ModulesInstance<[NavigationModule]>>).navigation?.path.pathname;
// try to resolve the context from the path, and if that fails, try to resolve the context from the parent
return concat(
pathname ? pathResolver(pathname) : EMPTY,
resolveContextFromParent({ ref, modules }),
).pipe(
// having no initial context is valid; a default avoids first() throwing EmptyError for it
first(undefined, undefined),
);
};
export default resolveInitialContext;