@equinor/fusion-framework-module-widget 9.0.2 → 9.0.3

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 (41) hide show
  1. package/CHANGELOG.md +176 -162
  2. package/dist/esm/Widget.js.map +1 -1
  3. package/dist/esm/WidgetModuleConfigurator.js.map +1 -1
  4. package/dist/esm/WidgetModuleProvider.js.map +1 -1
  5. package/dist/esm/enable-widget-module.js.map +1 -1
  6. package/dist/esm/errors.js.map +1 -1
  7. package/dist/esm/module.js.map +1 -1
  8. package/dist/esm/state/actions.js.map +1 -1
  9. package/dist/esm/state/create-reducer.js.map +1 -1
  10. package/dist/esm/state/create-state.js.map +1 -1
  11. package/dist/esm/state/flows.js.map +1 -1
  12. package/dist/esm/utils.js.map +1 -1
  13. package/dist/esm/version.js +1 -1
  14. package/dist/tsconfig.tsbuildinfo +1 -1
  15. package/dist/types/Widget.d.ts +5 -5
  16. package/dist/types/WidgetModuleConfigurator.d.ts +1 -1
  17. package/dist/types/WidgetModuleProvider.d.ts +4 -4
  18. package/dist/types/enable-widget-module.d.ts +1 -1
  19. package/dist/types/module.d.ts +3 -3
  20. package/dist/types/state/actions.d.ts +2 -2
  21. package/dist/types/state/create-reducer.d.ts +1 -1
  22. package/dist/types/state/create-state.d.ts +2 -2
  23. package/dist/types/state/flows.d.ts +1 -1
  24. package/dist/types/types.d.ts +1 -1
  25. package/dist/types/utils.d.ts +2 -2
  26. package/dist/types/version.d.ts +1 -1
  27. package/package.json +12 -12
  28. package/src/Widget.ts +294 -294
  29. package/src/WidgetModuleConfigurator.ts +45 -45
  30. package/src/WidgetModuleProvider.ts +144 -144
  31. package/src/enable-widget-module.ts +10 -10
  32. package/src/errors.ts +52 -56
  33. package/src/events.ts +51 -51
  34. package/src/module.ts +23 -23
  35. package/src/state/actions.ts +56 -56
  36. package/src/state/create-reducer.ts +33 -33
  37. package/src/state/create-state.ts +10 -10
  38. package/src/state/flows.ts +57 -57
  39. package/src/types.ts +38 -38
  40. package/src/utils.ts +44 -44
  41. package/src/version.ts +1 -1
package/src/Widget.ts CHANGED
@@ -1,324 +1,324 @@
1
- import { ModuleType } from '@equinor/fusion-framework-module';
2
- import {
3
- GetWidgetParameters,
4
- WidgetConfig,
5
- WidgetManifest,
6
- WidgetScriptModule,
7
- WidgetState,
8
- WidgetStateInitial,
1
+ import type { ModuleType } from '@equinor/fusion-framework-module';
2
+ import type {
3
+ GetWidgetParameters,
4
+ WidgetConfig,
5
+ WidgetManifest,
6
+ WidgetScriptModule,
7
+ WidgetState,
8
+ WidgetStateInitial,
9
9
  } from './types';
10
- import { Actions, actions } from './state/actions';
11
- import { FlowSubject } from '@equinor/fusion-observable';
10
+ import { type Actions, actions } from './state/actions';
11
+ import type { FlowSubject } from '@equinor/fusion-observable';
12
12
 
13
13
  import { createState } from './state/create-state';
14
- import { EventModule } from '@equinor/fusion-framework-module-event';
14
+ import type { EventModule } from '@equinor/fusion-framework-module-event';
15
15
  import { Observable, Subscription, combineLatest, firstValueFrom, lastValueFrom, of } from 'rxjs';
16
- import WidgetModuleProvider from './WidgetModuleProvider';
17
- import { WidgetModuleConfig } from './WidgetModuleConfigurator';
16
+ import type WidgetModuleProvider from './WidgetModuleProvider';
17
+ import type { WidgetModuleConfig } from './WidgetModuleConfigurator';
18
18
 
19
19
  import './events';
20
20
 
21
21
  // Class representing a fusion widget
22
22
  export class Widget {
23
- #state: FlowSubject<WidgetState, Actions>;
24
- name: string;
25
- config?: WidgetModuleConfig;
26
- widgetPrams?: GetWidgetParameters['args'];
23
+ #state: FlowSubject<WidgetState, Actions>;
24
+ name: string;
25
+ config?: WidgetModuleConfig;
26
+ widgetPrams?: GetWidgetParameters['args'];
27
27
 
28
- #subscription = new Subscription();
28
+ #subscription = new Subscription();
29
29
 
30
- // Getter for accessing the current state of the widget
31
- get state(): WidgetState {
32
- return this.#state.value;
33
- }
30
+ // Getter for accessing the current state of the widget
31
+ get state(): WidgetState {
32
+ return this.#state.value;
33
+ }
34
34
 
35
- /**
36
- * Constructs a new Widget instance.
37
- * @param value - Initial state of the widget.
38
- * @param args - Configuration and event parameters for the widget.
39
- */
40
- constructor(
41
- value: WidgetStateInitial,
42
- args: {
43
- provider: WidgetModuleProvider;
44
- config?: WidgetModuleConfig;
45
- event?: ModuleType<EventModule>;
46
- widgetPrams?: GetWidgetParameters['args'];
47
- },
48
- ) {
49
- this.name = value.name;
50
- this.widgetPrams = args.widgetPrams;
51
- this.config = args.config;
52
- this.#state = createState(value, args.provider);
53
- args.event && this.#registerEvents(args.event);
54
- }
35
+ /**
36
+ * Constructs a new Widget instance.
37
+ * @param value - Initial state of the widget.
38
+ * @param args - Configuration and event parameters for the widget.
39
+ */
40
+ constructor(
41
+ value: WidgetStateInitial,
42
+ args: {
43
+ provider: WidgetModuleProvider;
44
+ config?: WidgetModuleConfig;
45
+ event?: ModuleType<EventModule>;
46
+ widgetPrams?: GetWidgetParameters['args'];
47
+ },
48
+ ) {
49
+ this.name = value.name;
50
+ this.widgetPrams = args.widgetPrams;
51
+ this.config = args.config;
52
+ this.#state = createState(value, args.provider);
53
+ args.event && this.#registerEvents(args.event);
54
+ }
55
55
 
56
- /**
57
- * Registers event listeners for various actions in the widget's state.
58
- * @param event - The event module to dispatch events.
59
- */
60
- #registerEvents(event: ModuleType<EventModule>): void {
61
- const { name } = this;
56
+ /**
57
+ * Registers event listeners for various actions in the widget's state.
58
+ * @param event - The event module to dispatch events.
59
+ */
60
+ #registerEvents(event: ModuleType<EventModule>): void {
61
+ const { name } = this;
62
62
 
63
- this.#state.addEffect(actions.fetchManifest.type, () => {
64
- event.dispatchEvent('onWidgetManifestLoad', {
65
- detail: { name },
66
- source: this,
67
- });
68
- });
69
- this.#state.addEffect(actions.fetchManifest.success.type, (action) => {
70
- event.dispatchEvent('onWidgetManifestLoaded', {
71
- detail: { name, manifest: action.payload },
72
- source: this,
73
- });
74
- });
75
- this.#state.addEffect(actions.fetchManifest.failure.type, (action) => {
76
- event.dispatchEvent('onWidgetManifestFailure', {
77
- detail: { name, error: action.payload },
78
- source: this,
79
- });
80
- });
63
+ this.#state.addEffect(actions.fetchManifest.type, () => {
64
+ event.dispatchEvent('onWidgetManifestLoad', {
65
+ detail: { name },
66
+ source: this,
67
+ });
68
+ });
69
+ this.#state.addEffect(actions.fetchManifest.success.type, (action) => {
70
+ event.dispatchEvent('onWidgetManifestLoaded', {
71
+ detail: { name, manifest: action.payload },
72
+ source: this,
73
+ });
74
+ });
75
+ this.#state.addEffect(actions.fetchManifest.failure.type, (action) => {
76
+ event.dispatchEvent('onWidgetManifestFailure', {
77
+ detail: { name, error: action.payload },
78
+ source: this,
79
+ });
80
+ });
81
81
 
82
- this.#state.addEffect(actions.importWidget.type, () => {
83
- event.dispatchEvent('onWidgetScriptLoad', {
84
- detail: { name },
85
- source: this,
86
- });
87
- });
88
- this.#state.addEffect(actions.importWidget.success.type, (action) => {
89
- event.dispatchEvent('onWidgetScriptLoaded', {
90
- detail: { name, script: action.payload },
91
- source: this,
92
- });
93
- });
94
- this.#state.addEffect(actions.importWidget.failure.type, (action) => {
95
- event.dispatchEvent('onWidgetScriptFailure', {
96
- detail: { name, error: action.payload },
97
- source: this,
98
- });
99
- });
82
+ this.#state.addEffect(actions.importWidget.type, () => {
83
+ event.dispatchEvent('onWidgetScriptLoad', {
84
+ detail: { name },
85
+ source: this,
86
+ });
87
+ });
88
+ this.#state.addEffect(actions.importWidget.success.type, (action) => {
89
+ event.dispatchEvent('onWidgetScriptLoaded', {
90
+ detail: { name, script: action.payload },
91
+ source: this,
92
+ });
93
+ });
94
+ this.#state.addEffect(actions.importWidget.failure.type, (action) => {
95
+ event.dispatchEvent('onWidgetScriptFailure', {
96
+ detail: { name, error: action.payload },
97
+ source: this,
98
+ });
99
+ });
100
100
 
101
- this.#state.addEffect(actions.initialize.type, () => {
102
- event.dispatchEvent('onWidgetInitialize', {
103
- detail: { name },
104
- source: this,
105
- });
106
- });
101
+ this.#state.addEffect(actions.initialize.type, () => {
102
+ event.dispatchEvent('onWidgetInitialize', {
103
+ detail: { name },
104
+ source: this,
105
+ });
106
+ });
107
107
 
108
- this.#state.addEffect(actions.initialize.success.type, () => {
109
- event.dispatchEvent('onWidgetInitialized', {
110
- detail: { name },
111
- source: this,
112
- });
113
- });
108
+ this.#state.addEffect(actions.initialize.success.type, () => {
109
+ event.dispatchEvent('onWidgetInitialized', {
110
+ detail: { name },
111
+ source: this,
112
+ });
113
+ });
114
114
 
115
- this.#state.addEffect(actions.initialize.failure.type, ({ payload }) => {
116
- event.dispatchEvent('onWidgetInitializeFailure', {
117
- detail: { name, error: payload },
118
- source: this,
119
- });
120
- });
121
- }
115
+ this.#state.addEffect(actions.initialize.failure.type, ({ payload }) => {
116
+ event.dispatchEvent('onWidgetInitializeFailure', {
117
+ detail: { name, error: payload },
118
+ source: this,
119
+ });
120
+ });
121
+ }
122
122
 
123
- /**
124
- * Retrieves the manifest of the widget as an observable stream.
125
- * @param force_refresh - Flag to force refresh the manifest.
126
- * @returns An observable stream of the widget manifest.
127
- */
128
- public getManifest(force_refresh = false): Observable<WidgetManifest> {
129
- return new Observable((subscriber) => {
130
- if (this.#state.value.manifest) {
131
- subscriber.next(this.#state.value.manifest);
132
- if (!force_refresh) {
133
- return subscriber.complete();
134
- }
135
- }
136
- subscriber.add(
137
- this.#state.addEffect('set_manifest', ({ payload }) => {
138
- subscriber.next(payload);
139
- }),
140
- );
141
- subscriber.add(
142
- this.#state.addEffect('fetch_manifest::success', ({ payload }) => {
143
- subscriber.next(payload);
144
- subscriber.complete();
145
- }),
146
- );
147
- subscriber.add(
148
- this.#state.addEffect('fetch_manifest::failure', ({ payload }) => {
149
- subscriber.error(
150
- Error('failed to load widget manifest', {
151
- cause: payload,
152
- }),
153
- );
154
- }),
155
- );
123
+ /**
124
+ * Retrieves the manifest of the widget as an observable stream.
125
+ * @param force_refresh - Flag to force refresh the manifest.
126
+ * @returns An observable stream of the widget manifest.
127
+ */
128
+ public getManifest(force_refresh = false): Observable<WidgetManifest> {
129
+ return new Observable((subscriber) => {
130
+ if (this.#state.value.manifest) {
131
+ subscriber.next(this.#state.value.manifest);
132
+ if (!force_refresh) {
133
+ return subscriber.complete();
134
+ }
135
+ }
136
+ subscriber.add(
137
+ this.#state.addEffect('set_manifest', ({ payload }) => {
138
+ subscriber.next(payload);
139
+ }),
140
+ );
141
+ subscriber.add(
142
+ this.#state.addEffect('fetch_manifest::success', ({ payload }) => {
143
+ subscriber.next(payload);
144
+ subscriber.complete();
145
+ }),
146
+ );
147
+ subscriber.add(
148
+ this.#state.addEffect('fetch_manifest::failure', ({ payload }) => {
149
+ subscriber.error(
150
+ Error('failed to load widget manifest', {
151
+ cause: payload,
152
+ }),
153
+ );
154
+ }),
155
+ );
156
156
 
157
- this.loadManifest();
158
- });
159
- }
157
+ this.loadManifest();
158
+ });
159
+ }
160
160
 
161
- /**
162
- * Retrieves the configuration of the widget as an observable stream.
163
- * @param force_refresh - Flag to force refresh the configuration.
164
- * @returns An observable stream of the widget configuration.
165
- */
166
- public getConfig(force_refresh = false): Observable<WidgetConfig> {
167
- return new Observable((subscriber) => {
168
- if (this.#state.value.manifest) {
169
- subscriber.next(this.#state.value.config);
170
- if (!force_refresh) {
171
- return subscriber.complete();
172
- }
173
- }
174
- subscriber.add(
175
- this.#state.addEffect('set_config', ({ payload }) => {
176
- subscriber.next(payload);
177
- }),
178
- );
179
- subscriber.add(
180
- this.#state.addEffect('fetch_config::success', ({ payload }) => {
181
- subscriber.next(payload);
182
- subscriber.complete();
183
- }),
184
- );
185
- subscriber.add(
186
- this.#state.addEffect('fetch_config::failure', ({ payload }) => {
187
- subscriber.error(
188
- Error('failed to load widget manifest', {
189
- cause: payload,
190
- }),
191
- );
192
- }),
193
- );
161
+ /**
162
+ * Retrieves the configuration of the widget as an observable stream.
163
+ * @param force_refresh - Flag to force refresh the configuration.
164
+ * @returns An observable stream of the widget configuration.
165
+ */
166
+ public getConfig(force_refresh = false): Observable<WidgetConfig> {
167
+ return new Observable((subscriber) => {
168
+ if (this.#state.value.manifest) {
169
+ subscriber.next(this.#state.value.config);
170
+ if (!force_refresh) {
171
+ return subscriber.complete();
172
+ }
173
+ }
174
+ subscriber.add(
175
+ this.#state.addEffect('set_config', ({ payload }) => {
176
+ subscriber.next(payload);
177
+ }),
178
+ );
179
+ subscriber.add(
180
+ this.#state.addEffect('fetch_config::success', ({ payload }) => {
181
+ subscriber.next(payload);
182
+ subscriber.complete();
183
+ }),
184
+ );
185
+ subscriber.add(
186
+ this.#state.addEffect('fetch_config::failure', ({ payload }) => {
187
+ subscriber.error(
188
+ Error('failed to load widget manifest', {
189
+ cause: payload,
190
+ }),
191
+ );
192
+ }),
193
+ );
194
194
 
195
- this.loadConfig();
196
- });
197
- }
195
+ this.loadConfig();
196
+ });
197
+ }
198
198
 
199
- /**
200
- * Loads the configuration for the widget.
201
- * @param update - Flag to force an update of the configuration.
202
- */
203
- public loadConfig(update?: boolean) {
204
- this.#state.next(actions.fetchConfig({ key: this.name, ...this.widgetPrams }, update));
205
- }
199
+ /**
200
+ * Loads the configuration for the widget.
201
+ * @param update - Flag to force an update of the configuration.
202
+ */
203
+ public loadConfig(update?: boolean) {
204
+ this.#state.next(actions.fetchConfig({ key: this.name, ...this.widgetPrams }, update));
205
+ }
206
206
 
207
- /**
208
- * Loads the manifest for the widget.
209
- * @param update - Flag to force an update of the manifest.
210
- */
211
- public loadManifest(update?: boolean) {
212
- this.#state.next(actions.fetchManifest({ key: this.name, ...this.widgetPrams }, update));
213
- }
207
+ /**
208
+ * Loads the manifest for the widget.
209
+ * @param update - Flag to force an update of the manifest.
210
+ */
211
+ public loadManifest(update?: boolean) {
212
+ this.#state.next(actions.fetchManifest({ key: this.name, ...this.widgetPrams }, update));
213
+ }
214
214
 
215
- /**
216
- * Retrieves the widget module as an observable stream.
217
- * @param force_refresh - Flag to force refresh the widget module.
218
- * @returns An observable stream of the widget module.
219
- */
220
- public getWidgetModule(force_refresh = false): Observable<WidgetScriptModule> {
221
- return new Observable((subscriber) => {
222
- if (this.#state.value.modules) {
223
- subscriber.next(this.#state.value.modules);
224
- if (!force_refresh) {
225
- return subscriber.complete();
226
- }
227
- }
228
- subscriber.add(
229
- this.#state.addEffect('set_module', ({ payload }) => {
230
- subscriber.next(payload);
231
- }),
232
- );
233
- subscriber.add(
234
- this.#state.addEffect('import_widget::success', ({ payload }) => {
235
- subscriber.next(payload);
236
- subscriber.complete();
237
- }),
238
- );
239
- subscriber.add(
240
- this.#state.addEffect('import_widget::failure', ({ payload }) => {
241
- subscriber.error(
242
- Error('failed to load widget modules from script', {
243
- cause: payload,
244
- }),
245
- );
246
- }),
247
- );
215
+ /**
216
+ * Retrieves the widget module as an observable stream.
217
+ * @param force_refresh - Flag to force refresh the widget module.
218
+ * @returns An observable stream of the widget module.
219
+ */
220
+ public getWidgetModule(force_refresh = false): Observable<WidgetScriptModule> {
221
+ return new Observable((subscriber) => {
222
+ if (this.#state.value.modules) {
223
+ subscriber.next(this.#state.value.modules);
224
+ if (!force_refresh) {
225
+ return subscriber.complete();
226
+ }
227
+ }
228
+ subscriber.add(
229
+ this.#state.addEffect('set_module', ({ payload }) => {
230
+ subscriber.next(payload);
231
+ }),
232
+ );
233
+ subscriber.add(
234
+ this.#state.addEffect('import_widget::success', ({ payload }) => {
235
+ subscriber.next(payload);
236
+ subscriber.complete();
237
+ }),
238
+ );
239
+ subscriber.add(
240
+ this.#state.addEffect('import_widget::failure', ({ payload }) => {
241
+ subscriber.error(
242
+ Error('failed to load widget modules from script', {
243
+ cause: payload,
244
+ }),
245
+ );
246
+ }),
247
+ );
248
248
 
249
- subscriber.add(
250
- this.getManifest().subscribe((manifest) => {
251
- const path = manifest.assetPath
252
- ? `${manifest.assetPath}/${manifest.entryPoint}?api-version=${this.config?.client.apiVersion}`
253
- : `${manifest.entryPoint}?api-version=${this.config?.client.apiVersion}`;
249
+ subscriber.add(
250
+ this.getManifest().subscribe((manifest) => {
251
+ const path = manifest.assetPath
252
+ ? `${manifest.assetPath}/${manifest.entryPoint}?api-version=${this.config?.client.apiVersion}`
253
+ : `${manifest.entryPoint}?api-version=${this.config?.client.apiVersion}`;
254
254
 
255
- const url = new URL(path, this.config?.client.baseImportUrl);
255
+ const url = new URL(path, this.config?.client.baseImportUrl);
256
256
 
257
- return of(this.#state.next(actions.importWidget(url.href)));
258
- }),
259
- );
260
- });
261
- }
262
- /**
263
- * Initializes the widget and returns an observable stream with the combined results.
264
- * @returns An observable stream with the manifest, script, and configuration.
265
- */
266
- public initialize(): Observable<{
267
- manifest: WidgetManifest;
268
- script: WidgetScriptModule;
269
- config?: WidgetConfig;
270
- }> {
271
- return new Observable((observer) => {
272
- this.#state.next(actions.initialize());
273
- observer.add(
274
- combineLatest([
275
- this.getManifest(),
276
- this.getWidgetModule(),
277
- // this.getConfig(),
278
- ]).subscribe({
279
- next: ([manifest, script]) =>
280
- observer.next({
281
- manifest,
282
- script,
283
- //Todo: uncomment the getConfig on line #273 and replace config when backend support widget config.
284
- config: {
285
- environment: {},
286
- endpoints: {},
287
- },
288
- }),
289
- error: (err) => {
290
- observer.error(err), this.#state.next(actions.initialize.failure(err));
291
- },
292
- complete: () => {
293
- this.#state.next(actions.initialize.success());
294
- observer.complete();
295
- },
296
- }),
297
- );
298
- });
299
- }
300
- /**
301
- * Retrieves the widget module asynchronously as a Promise.
302
- * @param allow_cache - Flag to allow caching of the widget module.
303
- * @returns A Promise containing the widget module.
304
- */
305
- public getWidgetModuleAsync(allow_cache = true): Promise<WidgetScriptModule> {
306
- const operator = allow_cache ? firstValueFrom : lastValueFrom;
307
- return operator(this.getWidgetModule(!allow_cache));
308
- }
309
- /**
310
- * Updates the manifest of the widget.
311
- * @param manifest - The new manifest for the widget.
312
- * @param replace - Flag to replace the existing manifest.
313
- */
314
- public updateManifest(manifest: WidgetManifest, replace?: false) {
315
- this.#state.next(actions.setManifest(manifest, !replace));
316
- }
257
+ return of(this.#state.next(actions.importWidget(url.href)));
258
+ }),
259
+ );
260
+ });
261
+ }
262
+ /**
263
+ * Initializes the widget and returns an observable stream with the combined results.
264
+ * @returns An observable stream with the manifest, script, and configuration.
265
+ */
266
+ public initialize(): Observable<{
267
+ manifest: WidgetManifest;
268
+ script: WidgetScriptModule;
269
+ config?: WidgetConfig;
270
+ }> {
271
+ return new Observable((observer) => {
272
+ this.#state.next(actions.initialize());
273
+ observer.add(
274
+ combineLatest([
275
+ this.getManifest(),
276
+ this.getWidgetModule(),
277
+ // this.getConfig(),
278
+ ]).subscribe({
279
+ next: ([manifest, script]) =>
280
+ observer.next({
281
+ manifest,
282
+ script,
283
+ //Todo: uncomment the getConfig on line #273 and replace config when backend support widget config.
284
+ config: {
285
+ environment: {},
286
+ endpoints: {},
287
+ },
288
+ }),
289
+ error: (err) => {
290
+ observer.error(err), this.#state.next(actions.initialize.failure(err));
291
+ },
292
+ complete: () => {
293
+ this.#state.next(actions.initialize.success());
294
+ observer.complete();
295
+ },
296
+ }),
297
+ );
298
+ });
299
+ }
300
+ /**
301
+ * Retrieves the widget module asynchronously as a Promise.
302
+ * @param allow_cache - Flag to allow caching of the widget module.
303
+ * @returns A Promise containing the widget module.
304
+ */
305
+ public getWidgetModuleAsync(allow_cache = true): Promise<WidgetScriptModule> {
306
+ const operator = allow_cache ? firstValueFrom : lastValueFrom;
307
+ return operator(this.getWidgetModule(!allow_cache));
308
+ }
309
+ /**
310
+ * Updates the manifest of the widget.
311
+ * @param manifest - The new manifest for the widget.
312
+ * @param replace - Flag to replace the existing manifest.
313
+ */
314
+ public updateManifest(manifest: WidgetManifest, replace?: false) {
315
+ this.#state.next(actions.setManifest(manifest, !replace));
316
+ }
317
317
 
318
- /**
319
- * Disposes of the widget by unsubscribing from any active subscriptions.
320
- */
321
- public dispose() {
322
- this.#subscription.unsubscribe();
323
- }
318
+ /**
319
+ * Disposes of the widget by unsubscribing from any active subscriptions.
320
+ */
321
+ public dispose() {
322
+ this.#subscription.unsubscribe();
323
+ }
324
324
  }