@equinor/fusion-framework-module-context 4.0.0-next.11 → 4.0.0-next.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/esm/ContextConfigBuilder.js +2 -2
  2. package/dist/esm/ContextConfigBuilder.js.map +1 -1
  3. package/dist/esm/ContextProvider.js +34 -50
  4. package/dist/esm/ContextProvider.js.map +1 -1
  5. package/dist/esm/configurator.js +4 -2
  6. package/dist/esm/configurator.js.map +1 -1
  7. package/dist/esm/index.js +1 -1
  8. package/dist/esm/index.js.map +1 -1
  9. package/dist/esm/module.js +47 -19
  10. package/dist/esm/module.js.map +1 -1
  11. package/dist/esm/{enable-context.js → utils/enable-context.js} +1 -1
  12. package/dist/esm/utils/enable-context.js.map +1 -0
  13. package/dist/esm/utils/index.js +3 -0
  14. package/dist/esm/utils/index.js.map +1 -0
  15. package/dist/esm/utils/resolve-context-from-path.js +17 -0
  16. package/dist/esm/utils/resolve-context-from-path.js.map +1 -0
  17. package/dist/esm/utils/resolve-initial-context.js +16 -0
  18. package/dist/esm/utils/resolve-initial-context.js.map +1 -0
  19. package/dist/tsconfig.tsbuildinfo +1 -1
  20. package/dist/types/ContextConfigBuilder.d.ts +1 -1
  21. package/dist/types/ContextProvider.d.ts +3 -3
  22. package/dist/types/configurator.d.ts +9 -3
  23. package/dist/types/index.d.ts +1 -1
  24. package/dist/types/module.d.ts +3 -1
  25. package/dist/types/{enable-context.d.ts → utils/enable-context.d.ts} +2 -2
  26. package/dist/types/utils/index.d.ts +2 -0
  27. package/dist/types/utils/resolve-context-from-path.d.ts +8 -0
  28. package/dist/types/utils/resolve-initial-context.d.ts +7 -0
  29. package/package.json +12 -7
  30. package/src/ContextConfigBuilder.ts +2 -2
  31. package/src/ContextProvider.ts +15 -27
  32. package/src/configurator.ts +22 -3
  33. package/src/index.ts +1 -1
  34. package/src/module.ts +69 -16
  35. package/src/{enable-context.ts → utils/enable-context.ts} +3 -3
  36. package/src/utils/index.ts +2 -0
  37. package/src/utils/resolve-context-from-path.ts +33 -0
  38. package/src/utils/resolve-initial-context.ts +29 -0
  39. package/tsconfig.json +5 -2
  40. package/dist/esm/enable-context.js.map +0 -1
@@ -15,7 +15,7 @@ export declare class ContextConfigBuilder<TModules extends Array<AnyModule> = []
15
15
  setContextParameterFn(fn: ContextModuleConfig['contextParameterFn']): void;
16
16
  setValidateContext(fn: ContextModuleConfig['validateContext']): void;
17
17
  setResolveContext(fn: ContextModuleConfig['resolveContext']): void;
18
- setSkipInitialContext(skip: ContextModuleConfig['skipInitialContext']): void;
18
+ setResolveInitialContext(fn: ContextModuleConfig['resolveInitialContext']): void;
19
19
  setContextClient(client: {
20
20
  get: QueryFn<ContextItem, GetContextParameters> | QueryCtorOptions<ContextItem, GetContextParameters>;
21
21
  query: QueryFn<ContextItem[], QueryContextParameters> | QueryCtorOptions<ContextItem[], QueryContextParameters>;
@@ -41,9 +41,9 @@ export declare class ContextProvider implements IContextProvider {
41
41
  event?: ModuleType<EventModule>;
42
42
  parentContext?: IContextProvider;
43
43
  });
44
- connectParentContextAsync(provider: IContextProvider, opt?: {
45
- setCurrent?: boolean;
46
- }): Promise<Subscription>;
44
+ connectParentContext(provider: IContextProvider, opt?: {
45
+ skipFirst: boolean;
46
+ }): Subscription;
47
47
  setCurrentContextById(id: string): Observable<ContextItem<Record<string, unknown>>>;
48
48
  setCurrentContextByIdAsync(id: string): Promise<ContextItem<Record<string, unknown>>>;
49
49
  setCurrentContext(context: ContextItem<Record<string, unknown>>, opt?: {
@@ -1,10 +1,12 @@
1
- import { ModuleInitializerArgs } from '@equinor/fusion-framework-module';
1
+ import { ObservableInput } from 'rxjs';
2
+ import { AnyModuleInstance, ModuleInitializerArgs, ModuleInstance } from '@equinor/fusion-framework-module';
2
3
  import { ServicesModule, IApiProvider } from '@equinor/fusion-framework-module-services';
4
+ import { NavigationModule } from '@equinor/fusion-framework-module-navigation';
3
5
  import { QueryCtorOptions } from '@equinor/fusion-query';
4
6
  import { ContextFilterFn, ContextItem, QueryContextParameters, RelatedContextParameters } from './types';
5
7
  import { GetContextParameters } from './client/ContextClient';
6
8
  import { ContextConfigBuilderCallback } from './ContextConfigBuilder';
7
- import { IContextProvider } from 'ContextProvider';
9
+ import { type IContextProvider } from './ContextProvider';
8
10
  export interface ContextModuleConfig {
9
11
  client: {
10
12
  get: QueryCtorOptions<ContextItem, GetContextParameters>;
@@ -20,6 +22,10 @@ export interface ContextModuleConfig {
20
22
  }) => string | QueryContextParameters;
21
23
  resolveContext?: (this: IContextProvider, item: ContextItem | null) => ReturnType<IContextProvider['resolveContext']>;
22
24
  validateContext?: (this: IContextProvider, item: ContextItem | null) => ReturnType<IContextProvider['validateContext']>;
25
+ resolveInitialContext?: (args: {
26
+ ref?: AnyModuleInstance | any;
27
+ modules: ModuleInstance;
28
+ }) => ObservableInput<ContextItem | void>;
23
29
  }
24
30
  export interface IContextModuleConfigurator {
25
31
  addConfigBuilder: (init: ContextConfigBuilderCallback) => void;
@@ -29,6 +35,6 @@ export declare class ContextModuleConfigurator implements IContextModuleConfigur
29
35
  defaultExpireTime: number;
30
36
  addConfigBuilder(init: ContextConfigBuilderCallback): void;
31
37
  protected _getServiceProvider(init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule]>): Promise<IApiProvider>;
32
- createConfig(init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule]>): Promise<ContextModuleConfig>;
38
+ createConfig(init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule, NavigationModule]>): Promise<ContextModuleConfig>;
33
39
  }
34
40
  export default ContextModuleConfigurator;
@@ -1,5 +1,5 @@
1
1
  export { ContextModuleConfigurator, IContextModuleConfigurator, ContextModuleConfig, } from './configurator';
2
2
  export { IContextProvider, ContextProvider } from './ContextProvider';
3
3
  export { default, ContextModule, module as contextModule, moduleKey as contextModuleKey, } from './module';
4
- export { enableContext } from './enable-context';
4
+ export { enableContext } from './utils/enable-context';
5
5
  export * from './types';
@@ -1,13 +1,15 @@
1
1
  import { Module } from '@equinor/fusion-framework-module';
2
2
  import { EventModule } from '@equinor/fusion-framework-module-event';
3
3
  import { ServicesModule } from '@equinor/fusion-framework-module-services';
4
+ import { NavigationModule } from '@equinor/fusion-framework-module-navigation';
4
5
  import { IContextModuleConfigurator } from './configurator';
5
6
  import { IContextProvider } from './ContextProvider';
6
7
  export type ContextModuleKey = 'context';
7
8
  export declare const moduleKey: ContextModuleKey;
8
9
  export type ContextModule = Module<ContextModuleKey, IContextProvider, IContextModuleConfigurator, [
9
10
  ServicesModule,
10
- EventModule
11
+ EventModule,
12
+ NavigationModule
11
13
  ]>;
12
14
  export declare const module: ContextModule;
13
15
  declare module '@equinor/fusion-framework-module' {
@@ -1,4 +1,4 @@
1
1
  import type { IModulesConfigurator, AnyModule, ModuleInitializerArgs } from '@equinor/fusion-framework-module';
2
- import type { IContextModuleConfigurator } from './configurator';
3
- import type { ContextConfigBuilder } from './ContextConfigBuilder';
2
+ import type { IContextModuleConfigurator } from '../configurator';
3
+ import type { ContextConfigBuilder } from '../ContextConfigBuilder';
4
4
  export declare const enableContext: (configurator: IModulesConfigurator<any, any>, builder?: (<TDeps extends AnyModule[] = []>(builder: ContextConfigBuilder<TDeps, ModuleInitializerArgs<IContextModuleConfigurator, TDeps>>) => void | Promise<void>) | undefined) => void;
@@ -0,0 +1,2 @@
1
+ export { enableContext } from './enable-context';
2
+ export { resolveInitialContext } from './resolve-initial-context';
@@ -0,0 +1,8 @@
1
+ import { ModuleType } from '@equinor/fusion-framework-module';
2
+ import { type ContextModule } from '../module';
3
+ export type ContextPathResolveArgs = {
4
+ extract?: (path: string) => string | undefined;
5
+ validate?: (contextId: string) => boolean;
6
+ };
7
+ export declare const resolveContextFromPath: (context: ModuleType<ContextModule>, args?: ContextPathResolveArgs) => (path: string) => import("rxjs").Observable<import("..").ContextItem<Record<string, unknown>>>;
8
+ export default resolveContextFromPath;
@@ -0,0 +1,7 @@
1
+ import { type ContextModuleConfig } from '../configurator';
2
+ import { ContextPathResolveArgs } from './resolve-context-from-path';
3
+ export declare const resolveContextFromParent: ContextModuleConfig['resolveInitialContext'];
4
+ export declare const resolveInitialContext: (options?: {
5
+ path?: ContextPathResolveArgs;
6
+ }) => Required<ContextModuleConfig>['resolveInitialContext'];
7
+ export default resolveInitialContext;
package/package.json CHANGED
@@ -1,17 +1,21 @@
1
1
  {
2
2
  "name": "@equinor/fusion-framework-module-context",
3
- "version": "4.0.0-next.11",
3
+ "version": "4.0.0-next.12",
4
4
  "description": "",
5
5
  "main": "./dist/esm/index.js",
6
6
  "exports": {
7
7
  ".": "./dist/esm/index.js",
8
- "./package.json": "./package.json"
8
+ "./package.json": "./package.json",
9
+ "./utils": "./dist/esm/utils/index.js"
9
10
  },
10
11
  "types": "index.d.ts",
11
12
  "typesVersions": {
12
13
  ">=4.2": {
13
14
  "*": [
14
15
  "dist/types/*"
16
+ ],
17
+ "utils": [
18
+ "dist/types/utils/index.d.ts"
15
19
  ]
16
20
  }
17
21
  },
@@ -31,18 +35,19 @@
31
35
  "directory": "packages/modules/context"
32
36
  },
33
37
  "dependencies": {
34
- "@equinor/fusion-query": "^3.0.1-next.2",
38
+ "@equinor/fusion-query": "^3.0.1-next.3",
35
39
  "fast-deep-equal": "^3.1.3"
36
40
  },
37
41
  "devDependencies": {
38
- "@equinor/fusion-framework-module": "^3.0.0",
39
- "@equinor/fusion-framework-module-event": "^3.0.0",
40
- "@equinor/fusion-framework-module-services": "^3.1.0-next.3",
42
+ "@equinor/fusion-framework-module": "^4.0.0-next.0",
43
+ "@equinor/fusion-framework-module-event": "^4.0.0-next.0",
44
+ "@equinor/fusion-framework-module-navigation": "^2.1.0-next.0",
45
+ "@equinor/fusion-framework-module-services": "^3.1.0-next.4",
41
46
  "rxjs": "^7.5.7"
42
47
  },
43
48
  "peerDependencies": {
44
49
  "@equinor/fusion-framework-module": ">=1.2.5",
45
50
  "rxjs": "^7.5.7"
46
51
  },
47
- "gitHead": "278330e42e38f94327a98c0fc6121efe87a63b00"
52
+ "gitHead": "a2d5050515d375c7546b321aba27d11239869d97"
48
53
  }
@@ -65,8 +65,8 @@ export class ContextConfigBuilder<
65
65
  this.config.resolveContext = fn;
66
66
  }
67
67
 
68
- setSkipInitialContext(skip: ContextModuleConfig['skipInitialContext']) {
69
- this.config.skipInitialContext = skip;
68
+ setResolveInitialContext(fn: ContextModuleConfig['resolveInitialContext']) {
69
+ this.config.resolveInitialContext = fn;
70
70
  }
71
71
 
72
72
  setContextClient(
@@ -158,12 +158,22 @@ export class ContextProvider implements IContextProvider {
158
158
  }
159
159
  }
160
160
 
161
- public async connectParentContextAsync(
161
+ public connectParentContext(
162
162
  provider: IContextProvider,
163
- opt?: { setCurrent?: boolean }
164
- ): Promise<Subscription> {
163
+ opt?: { skipFirst: boolean }
164
+ ): Subscription {
165
165
  const parentContext$ = provider.currentContext$.pipe(
166
- filter((next) => this.currentContext !== next),
166
+ filter((next, index) => {
167
+ if (opt?.skipFirst && index <= 1) {
168
+ console.debug(
169
+ 'ContextProvider::connectParentContext',
170
+ 'skipping first item',
171
+ next
172
+ );
173
+ return false;
174
+ }
175
+ return this.currentContext !== next;
176
+ }),
167
177
  switchMap(async (next) => {
168
178
  if (next) {
169
179
  const onParentContextChanged = await this.#event?.dispatchEvent(
@@ -206,29 +216,7 @@ export class ContextProvider implements IContextProvider {
206
216
 
207
217
  const subscription = parentContext$.subscribe();
208
218
  this.#subscriptions.add(subscription);
209
-
210
- return new Promise((resolve, reject) => {
211
- if (opt?.setCurrent && provider.currentContext) {
212
- subscription.add(reject);
213
- subscription.add(
214
- this.setCurrentContext(provider.currentContext, {
215
- validate: true,
216
- resolve: true,
217
- }).subscribe({
218
- error: (err) => {
219
- subscription.remove(reject);
220
- reject(err);
221
- },
222
- complete: () => {
223
- subscription.remove(reject);
224
- resolve(subscription);
225
- },
226
- })
227
- );
228
- } else {
229
- return resolve(subscription);
230
- }
231
- });
219
+ return subscription;
232
220
  }
233
221
 
234
222
  public setCurrentContextById(id: string): Observable<ContextItem<Record<string, unknown>>> {
@@ -1,5 +1,13 @@
1
- import { ModuleInitializerArgs, ModulesInstanceType } from '@equinor/fusion-framework-module';
1
+ import { ObservableInput } from 'rxjs';
2
+
3
+ import {
4
+ AnyModuleInstance,
5
+ ModuleInitializerArgs,
6
+ ModuleInstance,
7
+ ModulesInstanceType,
8
+ } from '@equinor/fusion-framework-module';
2
9
  import { ServicesModule, IApiProvider } from '@equinor/fusion-framework-module-services';
10
+ import { NavigationModule } from '@equinor/fusion-framework-module-navigation';
3
11
  import { getContextSelector, queryContextSelector, relatedContextSelector } from './selectors';
4
12
  import { QueryCtorOptions } from '@equinor/fusion-query';
5
13
  import {
@@ -10,7 +18,8 @@ import {
10
18
  } from './types';
11
19
  import { GetContextParameters } from './client/ContextClient';
12
20
  import { ContextConfigBuilder, ContextConfigBuilderCallback } from './ContextConfigBuilder';
13
- import { IContextProvider } from 'ContextProvider';
21
+ import { type IContextProvider } from './ContextProvider';
22
+ import resolveInitialContext from './utils/resolve-initial-context';
14
23
 
15
24
  export interface ContextModuleConfig {
16
25
  client: {
@@ -41,6 +50,14 @@ export interface ContextModuleConfig {
41
50
  this: IContextProvider,
42
51
  item: ContextItem | null
43
52
  ) => ReturnType<IContextProvider['validateContext']>;
53
+
54
+ resolveInitialContext?: (
55
+ args: {
56
+ ref?: AnyModuleInstance | any;
57
+ modules: ModuleInstance;
58
+ }
59
+ // ...args: Parameters<ContextModule['postInitialize']>
60
+ ) => ObservableInput<ContextItem | void>;
44
61
  }
45
62
 
46
63
  export interface IContextModuleConfigurator {
@@ -72,7 +89,7 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
72
89
  }
73
90
 
74
91
  public async createConfig(
75
- init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule]>
92
+ init: ModuleInitializerArgs<IContextModuleConfigurator, [ServicesModule, NavigationModule]>
76
93
  ): Promise<ContextModuleConfig> {
77
94
  const config = await this.#configBuilders.reduce(async (cur, cb) => {
78
95
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -81,6 +98,8 @@ export class ContextModuleConfigurator implements IContextModuleConfigurator {
81
98
  return Object.assign(cur, builder.config);
82
99
  }, Promise.resolve({} as Partial<ContextModuleConfig>));
83
100
 
101
+ config.resolveInitialContext ??= resolveInitialContext();
102
+
84
103
  // TODO - make less lazy
85
104
  config.client ??= await (async (): Promise<ContextModuleConfig['client']> => {
86
105
  const apiProvider = await this._getServiceProvider(init);
package/src/index.ts CHANGED
@@ -13,6 +13,6 @@ export {
13
13
  moduleKey as contextModuleKey,
14
14
  } from './module';
15
15
 
16
- export { enableContext } from './enable-context';
16
+ export { enableContext } from './utils/enable-context';
17
17
 
18
18
  export * from './types';
package/src/module.ts CHANGED
@@ -2,9 +2,12 @@ import { Module, ModulesInstance } from '@equinor/fusion-framework-module';
2
2
 
3
3
  import { EventModule } from '@equinor/fusion-framework-module-event';
4
4
  import { ServicesModule } from '@equinor/fusion-framework-module-services';
5
+ import { NavigationModule } from '@equinor/fusion-framework-module-navigation';
5
6
 
6
7
  import { IContextModuleConfigurator, ContextModuleConfigurator } from './configurator';
7
8
  import { IContextProvider, ContextProvider } from './ContextProvider';
9
+ import { catchError, EMPTY, from, Observable, switchMap, filter, Subscription } from 'rxjs';
10
+ import { ContextItem } from 'types';
8
11
 
9
12
  export type ContextModuleKey = 'context';
10
13
 
@@ -14,34 +17,84 @@ export type ContextModule = Module<
14
17
  ContextModuleKey,
15
18
  IContextProvider,
16
19
  IContextModuleConfigurator,
17
- [ServicesModule, EventModule]
20
+ [ServicesModule, EventModule, NavigationModule]
18
21
  >;
19
22
 
20
23
  export const module: ContextModule = {
21
24
  name: moduleKey,
22
25
  configure: () => new ContextModuleConfigurator(),
23
- initialize: async (args) => {
26
+ initialize: async function (args) {
24
27
  const config = await (args.config as ContextModuleConfigurator).createConfig(args);
25
28
  const event = args.hasModule('event') ? await args.requireInstance('event') : undefined;
26
29
  const parentProvider = (args.ref as ModulesInstance<[ContextModule]>)?.context;
27
30
  const provider = new ContextProvider({ config, event, parentContext: parentProvider });
28
31
 
29
- // TODO add option for skipping this step
30
- if (parentProvider) {
31
- try {
32
- await provider.connectParentContextAsync(parentProvider, {
33
- setCurrent: !config.skipInitialContext,
34
- });
35
- } catch (err) {
36
- console.warn('provider.connectParentContext', 'failed to set parent context', err);
37
- }
38
- }
32
+ const subscription = new Subscription(() => provider.dispose());
39
33
 
40
- return provider;
41
- },
34
+ this.postInitialize = (args) =>
35
+ new Observable((subscriber) => {
36
+ const resolveInitialContext$ = config.resolveInitialContext
37
+ ? from(config.resolveInitialContext(args)).pipe(
38
+ filter((item): item is ContextItem => !!item),
39
+ switchMap((item) =>
40
+ args.modules.context.setCurrentContext(item, {
41
+ validate: true,
42
+ resolve: true,
43
+ })
44
+ )
45
+ )
46
+ : EMPTY;
47
+
48
+ subscriber.add(
49
+ resolveInitialContext$
50
+ .pipe(
51
+ catchError((err) => {
52
+ console.warn(
53
+ 'ContextModule.postInitialize',
54
+ 'failed to resolve initial context',
55
+ err
56
+ );
57
+ return EMPTY;
58
+ })
59
+ )
60
+ .subscribe({
61
+ next: (item) => {
62
+ console.debug(
63
+ 'ContextModule.postInitialize',
64
+ `initial context was resolved to [${item.id}]`,
65
+ item
66
+ );
67
+ },
68
+ complete: () => {
69
+ provider.connectParentContext(parentProvider, { skipFirst: true });
70
+ subscriber.complete();
71
+ },
72
+ })
73
+ );
42
74
 
43
- dispose: (args) => {
44
- (args.instance as ContextProvider).dispose();
75
+ if (args.modules.navigation) {
76
+ subscription.add(
77
+ provider.currentContext$.subscribe((item) => {
78
+ if (!item) {
79
+ return args.modules.navigation.replace('/');
80
+ }
81
+ const { path } = args.modules.navigation;
82
+ const partial = path.pathname.split('/') ?? [];
83
+ if (partial[0] !== item.id) {
84
+ partial[0] = item?.id;
85
+ args.modules.navigation.replace({
86
+ ...path,
87
+ pathname: partial.join('/'),
88
+ });
89
+ }
90
+ })
91
+ );
92
+ }
93
+ });
94
+
95
+ this.dispose = () => subscription.unsubscribe();
96
+
97
+ return provider;
45
98
  },
46
99
  };
47
100
 
@@ -3,10 +3,10 @@ import type {
3
3
  AnyModule,
4
4
  ModuleInitializerArgs,
5
5
  } from '@equinor/fusion-framework-module';
6
- import type { IContextModuleConfigurator } from './configurator';
7
- import type { ContextConfigBuilder } from './ContextConfigBuilder';
6
+ import type { IContextModuleConfigurator } from '../configurator';
7
+ import type { ContextConfigBuilder } from '../ContextConfigBuilder';
8
8
 
9
- import { module } from './module';
9
+ import { module } from '../module';
10
10
 
11
11
  /**
12
12
  * Method for enabling the Service module
@@ -0,0 +1,2 @@
1
+ export { enableContext } from './enable-context';
2
+ export { resolveInitialContext } from './resolve-initial-context';
@@ -0,0 +1,33 @@
1
+ import { EMPTY } from 'rxjs';
2
+
3
+ import { ModuleType } from '@equinor/fusion-framework-module';
4
+
5
+ import { type ContextModule } from '../module';
6
+
7
+ export type ContextPathResolveArgs = {
8
+ extract?: (path: string) => string | undefined;
9
+ validate?: (contextId: string) => boolean;
10
+ };
11
+
12
+ const matchGUID =
13
+ /^(?:(?:[0-9a-fA-F]){8}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){4}-(?:[0-9a-fA-F]){12})$/;
14
+
15
+ const extractContextIdFromPath = (path: string): string | undefined => path.split('/')[0];
16
+
17
+ const validateContextId = (contextId: string): boolean => !!contextId.match(matchGUID);
18
+
19
+ export const resolveContextFromPath =
20
+ (context: ModuleType<ContextModule>, args?: ContextPathResolveArgs) => (path: string) => {
21
+ const { extract = extractContextIdFromPath, validate = validateContextId } = args ?? {};
22
+ const contextId = extract(path);
23
+ if (!contextId) {
24
+ return EMPTY;
25
+ }
26
+ if (validate(contextId)) {
27
+ return context.contextClient.resolveContext(contextId);
28
+ }
29
+
30
+ throw Error(`Failed to validate context [${contextId}] from path [${path}]`);
31
+ };
32
+
33
+ export default resolveContextFromPath;
@@ -0,0 +1,29 @@
1
+ import { ModulesInstance } from '@equinor/fusion-framework-module';
2
+ import { ContextModule } from '../module';
3
+ import { type ContextModuleConfig } from '../configurator';
4
+ import { concat, EMPTY, first, of } from 'rxjs';
5
+
6
+ import { ContextPathResolveArgs, resolveContextFromPath } from './resolve-context-from-path';
7
+
8
+ export const resolveContextFromParent: ContextModuleConfig['resolveInitialContext'] = ({ ref }) => {
9
+ const parentContext = (ref as ModulesInstance<[ContextModule]>)?.context;
10
+ if (!parentContext) {
11
+ throw Error(['resolveContextFromNavigation', 'ref does not support context!'].join('\n'));
12
+ }
13
+ return parentContext.currentContext ? of(parentContext.currentContext) : EMPTY;
14
+ };
15
+
16
+ export const resolveInitialContext =
17
+ (options?: {
18
+ path?: ContextPathResolveArgs;
19
+ }): Required<ContextModuleConfig>['resolveInitialContext'] =>
20
+ ({ ref, modules }) => {
21
+ const { context, navigation } = modules;
22
+ const pathResolver = resolveContextFromPath(context, options?.path);
23
+ return concat(
24
+ navigation ? pathResolver(navigation.path.pathname) : EMPTY,
25
+ resolveContextFromParent(ref)
26
+ ).pipe(first());
27
+ };
28
+
29
+ export default resolveInitialContext;
package/tsconfig.json CHANGED
@@ -14,11 +14,14 @@
14
14
  "path": "../module"
15
15
  },
16
16
  {
17
- "path": "../services"
17
+ "path": "../navigation"
18
18
  },
19
19
  {
20
20
  "path": "../event"
21
- }
21
+ },
22
+ {
23
+ "path": "../services"
24
+ },
22
25
  ],
23
26
  "include": [
24
27
  "src/**/*",
@@ -1 +0,0 @@
1
- {"version":3,"file":"enable-context.js","sourceRoot":"","sources":["../../src/enable-context.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAMlC,MAAM,CAAC,MAAM,aAAa,GAAG,CAEzB,YAA4C,EAC5C,OAKyB,EACrB,EAAE;IACN,YAAY,CAAC,SAAS,CAAC;QACnB,MAAM;QACN,SAAS,EAAE,CAAC,mBAAmB,EAAE,EAAE;YAC/B,OAAO,IAAI,mBAAmB,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAC7D,CAAC;KACJ,CAAC,CAAC;AACP,CAAC,CAAC"}