@elliemae/pui-app-bridge 2.29.1 → 2.30.0

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.
@@ -90,9 +90,17 @@ class CAppBridge {
90
90
  */
91
91
  #activeApps = /* @__PURE__ */ new Map();
92
92
  /**
93
- * host-supplied metadata per guest, keyed by app id
93
+ * host-supplied metadata per guest, keyed by openApp instance id
94
94
  */
95
95
  #guestMetadata = /* @__PURE__ */ new Map();
96
+ /**
97
+ * per-instance module.unload handlers (instance id → closeApp)
98
+ */
99
+ #moduleUnloadHandlers = /* @__PURE__ */ new Map();
100
+ /**
101
+ * app ids that already have a shared module.unload router installed
102
+ */
103
+ #moduleUnloadRouterInstalled = /* @__PURE__ */ new Set();
96
104
  /**
97
105
  * instance of the app registry
98
106
  */
@@ -164,6 +172,7 @@ class CAppBridge {
164
172
  * @param options.documentEle - iframe document
165
173
  * @param options.elementIds - ids of injected script/style elements
166
174
  * @param options.microFEHost - pre-created host reference for guest-initiated loading
175
+ * @param options.selfInitialize
167
176
  */
168
177
  #addAppToActiveAppList = ({
169
178
  id,
@@ -211,14 +220,7 @@ class CAppBridge {
211
220
  throw new Error(
212
221
  `Application ${id} with instance id ${instanceId} doesn't expose init method`
213
222
  );
214
- const host = microFEHost ?? new import_microfeHost.CMicroFEHost({
215
- guest: { id },
216
- version: this.#version,
217
- containerId,
218
- logger: this.#logger,
219
- soManager: this.#soManager,
220
- eventManager: this.#eventManager
221
- });
223
+ const host = microFEHost ?? this.#createMicroFEHost({ id, instanceId, containerId });
222
224
  return app.init({
223
225
  host,
224
226
  hostUrl,
@@ -363,23 +365,74 @@ class CAppBridge {
363
365
  });
364
366
  }
365
367
  };
368
+ /**
369
+ * Resolve which openApp instance invoked module.unload when multiple
370
+ * instances share one module SO (keyed by app id).
371
+ * @param appId
372
+ * @param unloadTarget
373
+ */
374
+ #resolveModuleUnloadInstanceId = (appId, unloadTarget) => {
375
+ const guest = unloadTarget?.callContext?.guest;
376
+ if (guest?.instanceId) {
377
+ const active = this.#activeApps.get(guest.instanceId);
378
+ if (active?.id === appId) return guest.instanceId;
379
+ }
380
+ const guestWindow = guest?.guestWindow;
381
+ const openInstances = Array.from(this.#activeApps.entries()).filter(
382
+ ([, app]) => app.id === appId
383
+ );
384
+ if (guestWindow) {
385
+ const match = openInstances.find(
386
+ ([, app]) => app.guest.guestWindow === guestWindow
387
+ );
388
+ if (match) return match[0];
389
+ }
390
+ return openInstances.length === 1 ? openInstances[0][0] : void 0;
391
+ };
366
392
  /**
367
393
  * If the guest's module SO exposes _setUnloadHandler (duck-typed),
368
394
  * bind it to closeApp so the guest can trigger its own teardown.
395
+ * Multiple instances of the same app share one module SO; a router
396
+ * dispatches unload to the correct instance.
369
397
  * @param id
370
398
  * @param instanceId
371
399
  */
372
400
  #bindModuleUnload = (id, instanceId) => {
401
+ this.#moduleUnloadHandlers.set(instanceId, () => this.closeApp(instanceId));
373
402
  const moduleSO = this.#soManager.getObject(
374
403
  import_pui_scripting_object.ScriptingObjectNames.Module,
375
404
  { id }
376
405
  );
377
406
  const so = moduleSO;
378
- const handler = so?._setUnloadHandler;
379
- if (typeof handler === "function") {
380
- handler(
381
- () => this.closeApp(instanceId)
382
- );
407
+ const setHandler = so?._setUnloadHandler;
408
+ if (typeof setHandler !== "function") return;
409
+ if (this.#moduleUnloadRouterInstalled.has(id)) return;
410
+ this.#moduleUnloadRouterInstalled.add(id);
411
+ setHandler.call(
412
+ moduleSO,
413
+ async () => {
414
+ const unloadFn = moduleSO.unload;
415
+ const resolvedInstanceId = this.#resolveModuleUnloadInstanceId(
416
+ id,
417
+ typeof unloadFn === "function" ? unloadFn : moduleSO
418
+ );
419
+ if (!resolvedInstanceId) {
420
+ this.#logger.warn(
421
+ `Cannot resolve instance for module.unload of ${id}`
422
+ );
423
+ return;
424
+ }
425
+ await this.#moduleUnloadHandlers.get(resolvedInstanceId)?.();
426
+ }
427
+ );
428
+ };
429
+ #unbindModuleUnload = (instanceId, appId) => {
430
+ this.#moduleUnloadHandlers.delete(instanceId);
431
+ const hasOpenInstances = [...this.#activeApps.entries()].some(
432
+ ([openInstanceId, app]) => app.id === appId && openInstanceId !== instanceId
433
+ );
434
+ if (!hasOpenInstances) {
435
+ this.#moduleUnloadRouterInstalled.delete(appId);
383
436
  }
384
437
  };
385
438
  /**
@@ -390,37 +443,54 @@ class CAppBridge {
390
443
  import_pui_scripting_object.ScriptingObjectNames.Module,
391
444
  { id: guestAppId }
392
445
  );
446
+ #createMicroFEHost = ({
447
+ id,
448
+ instanceId,
449
+ containerId
450
+ }) => new import_microfeHost.CMicroFEHost({
451
+ guest: { id },
452
+ instanceId,
453
+ version: this.#version,
454
+ containerId,
455
+ logger: this.#logger,
456
+ soManager: this.#soManager,
457
+ eventManager: this.#eventManager
458
+ });
393
459
  /**
394
460
  * Dispatch module.unloading or module.unmount to guest listeners.
395
461
  * Uses a stub module SO when none is registered; routing is by eventId.
396
- * @param guestAppId
462
+ * eventOptions.guestId is the openApp instance id (iframe id), not app id.
463
+ * @param guest
397
464
  * @param eventId
398
465
  * @param eventName
399
466
  */
400
- #dispatchGuestModuleEvent = async (guestAppId, eventId, eventName) => {
401
- const moduleSO = this.#getGuestModuleScriptingObject(guestAppId) ?? MODULE_EVENT_STUB;
467
+ #dispatchGuestModuleEvent = async (guest, eventId, eventName) => {
468
+ const moduleSO = this.#getGuestModuleScriptingObject(guest.id) ?? MODULE_EVENT_STUB;
402
469
  return this.#eventManager.dispatchEvent(moduleSO, {
403
470
  event: { id: eventId, name: eventName },
404
- eventParams: { moduleId: guestAppId },
405
- eventOptions: { timeout: MODULE_EVENT_TIMEOUT_MS }
471
+ eventParams: { moduleId: guest.id },
472
+ eventOptions: {
473
+ timeout: MODULE_EVENT_TIMEOUT_MS,
474
+ guestId: guest.instanceId
475
+ }
406
476
  });
407
477
  };
408
- #canUnload = async (id) => {
478
+ #canUnload = async (guest) => {
409
479
  try {
410
480
  const feedback = await this.#dispatchGuestModuleEvent(
411
- id,
481
+ guest,
412
482
  MODULE_UNLOADING_EVENT,
413
483
  "unloading"
414
484
  );
415
485
  if (isModuleUnloadVetoed(feedback)) {
416
486
  this.#logger.info(
417
- `Unload denied by guest ${id} via module.unloading event`
487
+ `Unload denied by guest ${guest.id} via module.unloading event`
418
488
  );
419
489
  return false;
420
490
  }
421
491
  } catch (err) {
422
492
  this.#logger.warn({
423
- message: `Error dispatching module.unloading event for ${id}; proceeding with unload`,
493
+ message: `Error dispatching module.unloading event for ${guest.id}; proceeding with unload`,
424
494
  exception: err
425
495
  });
426
496
  }
@@ -443,7 +513,11 @@ class CAppBridge {
443
513
  return;
444
514
  }
445
515
  try {
446
- await this.#dispatchGuestModuleEvent(id, MODULE_UNMOUNT_EVENT, "unmount");
516
+ await this.#dispatchGuestModuleEvent(
517
+ { id, instanceId },
518
+ MODULE_UNMOUNT_EVENT,
519
+ "unmount"
520
+ );
447
521
  } catch (err) {
448
522
  this.#logger.warn({
449
523
  message: `Error dispatching module.unmount event for ${id}`,
@@ -524,9 +598,10 @@ class CAppBridge {
524
598
  const callerCtx = so[propName]?.callContext;
525
599
  if (callerCtx?.guest) {
526
600
  const existingChain = callerCtx.callChain ?? [];
527
- const guestId = callerCtx.guest.id;
528
- const meta = this.#guestMetadata.get(guestId);
529
- const callerEntry = { id: guestId };
601
+ const guestCtx = callerCtx.guest;
602
+ const metadataKey = guestCtx.instanceId ?? guestCtx.id;
603
+ const meta = this.#guestMetadata.get(metadataKey);
604
+ const callerEntry = { id: guestCtx.id };
530
605
  if (meta) callerEntry.metadata = meta;
531
606
  Object.defineProperty(propValue, "callContext", {
532
607
  value: {
@@ -583,11 +658,13 @@ class CAppBridge {
583
658
  return;
584
659
  }
585
660
  const { id, selfInitialize } = app;
586
- if (!await this.#canUnload(id)) return;
661
+ if (!await this.#canUnload({ id, instanceId })) return;
587
662
  const appConfig = this.#microFEConfig.getConfigById(id);
588
663
  if (!appConfig) {
589
664
  this.#logger.warn(`Configuration for application ${id} is not found`);
590
665
  this.#activeApps.delete(instanceId);
666
+ this.#guestMetadata.delete(instanceId);
667
+ this.#unbindModuleUnload(instanceId, id);
591
668
  import_frame.Frame.remove(instanceId);
592
669
  return;
593
670
  }
@@ -609,7 +686,8 @@ class CAppBridge {
609
686
  frameEle.src = "about:blank";
610
687
  }
611
688
  this.#activeApps.delete(instanceId);
612
- this.#guestMetadata.delete(id);
689
+ this.#guestMetadata.delete(instanceId);
690
+ this.#unbindModuleUnload(instanceId, id);
613
691
  import_frame.Frame.remove(instanceId);
614
692
  }
615
693
  };
@@ -715,17 +793,11 @@ class CAppBridge {
715
793
  * @param appConfig.hostUrl
716
794
  * @param options
717
795
  * @param options.id
796
+ * @param options.instanceId
718
797
  * @param options.containerId
719
798
  */
720
799
  #setupIframe = (frameDoc, appConfig, options) => {
721
- const microFEHost = new import_microfeHost.CMicroFEHost({
722
- guest: { id: options.id },
723
- version: this.#version,
724
- containerId: options.containerId,
725
- logger: this.#logger,
726
- soManager: this.#soManager,
727
- eventManager: this.#eventManager
728
- });
800
+ const microFEHost = this.#createMicroFEHost(options);
729
801
  const iframeWindow = frameDoc.defaultView;
730
802
  if (iframeWindow) {
731
803
  iframeWindow.emui = iframeWindow.emui ?? {};
@@ -759,7 +831,7 @@ class CAppBridge {
759
831
  selfInitialize
760
832
  } = params;
761
833
  const instanceId = (0, import_uuid.v4)();
762
- if (metadata) this.#guestMetadata.set(id, metadata);
834
+ if (metadata) this.#guestMetadata.set(instanceId, metadata);
763
835
  const appConfig = this.#microFEConfig.getConfigById(id);
764
836
  if (!appConfig) {
765
837
  throw new Error(`Application ${id} is not found in app config`);
@@ -787,6 +859,7 @@ class CAppBridge {
787
859
  appConfig,
788
860
  {
789
861
  id,
862
+ instanceId,
790
863
  containerId: frameOptions?.containerId
791
864
  }
792
865
  );
@@ -811,6 +884,8 @@ class CAppBridge {
811
884
  this.#bindModuleUnload(id, instanceId);
812
885
  return instanceId;
813
886
  } catch (err) {
887
+ this.#guestMetadata.delete(instanceId);
888
+ this.#unbindModuleUnload(instanceId, id);
814
889
  this.#unloadApp({
815
890
  id,
816
891
  instanceId,
@@ -56,9 +56,22 @@ class EventManager {
56
56
  * emit an event to set of listeners
57
57
  * @param {EmitEventParam} param - parameters for emitting an event
58
58
  */
59
+ /**
60
+ * Guest-scoped dispatches skip listeners registered for a different guest.
61
+ * @param listener
62
+ * @param eventOptions
63
+ */
64
+ #matchesGuestScope = (listener, eventOptions) => {
65
+ const targetGuestId = eventOptions?.guestId;
66
+ if (!targetGuestId || !listener.guestId) return true;
67
+ return listener.guestId === targetGuestId;
68
+ };
59
69
  #emitEvent = (param) => {
60
- const { eventName, scriptingObject, eventParams, listeners } = param;
70
+ const { eventName, scriptingObject, eventParams, listeners, eventOptions } = param;
61
71
  listeners.forEach((listener) => {
72
+ if (!this.#matchesGuestScope(listener, eventOptions)) {
73
+ return;
74
+ }
62
75
  if (listener.criteria) {
63
76
  const matches = eventParams ? this.#matchesCriteria(eventParams, listener.criteria) : false;
64
77
  if (!matches) {
@@ -83,10 +96,14 @@ class EventManager {
83
96
  eventName,
84
97
  scriptingObject,
85
98
  eventParams,
86
- feedbackWaitTime
99
+ feedbackWaitTime,
100
+ eventOptions
87
101
  }) => {
88
102
  const listeners = this.#listeners.get(eventId) || [];
89
103
  const promises = listeners.map((listener) => {
104
+ if (!this.#matchesGuestScope(listener, eventOptions)) {
105
+ return Promise.resolve();
106
+ }
90
107
  if (listener.criteria) {
91
108
  const matches = eventParams ? this.#matchesCriteria(eventParams, listener.criteria) : false;
92
109
  if (!matches) {
@@ -171,7 +188,8 @@ class EventManager {
171
188
  eventName: name,
172
189
  scriptingObject,
173
190
  eventParams,
174
- listeners
191
+ listeners,
192
+ eventOptions
175
193
  });
176
194
  return Promise.resolve();
177
195
  }
@@ -180,7 +198,8 @@ class EventManager {
180
198
  eventName: name,
181
199
  scriptingObject,
182
200
  eventParams,
183
- feedbackWaitTime: timeout
201
+ feedbackWaitTime: timeout,
202
+ eventOptions
184
203
  });
185
204
  };
186
205
  /**
@@ -188,7 +207,7 @@ class EventManager {
188
207
  * @param {SubscribeParam<EventId, AppEvents[EventId]>} param - parameters for subscribing to an event
189
208
  */
190
209
  subscribe = (param) => {
191
- const { eventId, callback, criteria } = param;
210
+ const { eventId, callback, criteria, guestId } = param;
192
211
  if (!eventId) throw new Error("eventId is required");
193
212
  if (!callback) throw new Error("Callback is required");
194
213
  const listeners = this.#listeners.get(eventId) || [];
@@ -196,6 +215,7 @@ class EventManager {
196
215
  listeners.push({
197
216
  token,
198
217
  criteria,
218
+ guestId,
199
219
  callback
200
220
  });
201
221
  this.#listeners.set(eventId, listeners);
@@ -23,12 +23,13 @@ __export(microfeHost_exports, {
23
23
  module.exports = __toCommonJS(microfeHost_exports);
24
24
  var import_microfe_common = require("@elliemae/microfe-common");
25
25
  var import_constant = require("./constant.js");
26
+ const GUEST_SCOPED_MODULE_EVENTS = /* @__PURE__ */ new Set([
27
+ "module.unloading",
28
+ "module.unmount"
29
+ ]);
26
30
  class CMicroFEHost {
27
- /**
28
- * unique id of the iframe container
29
- */
30
- #containerId;
31
31
  #guest;
32
+ #instanceId;
32
33
  #version;
33
34
  #soManager;
34
35
  #eventManager;
@@ -38,7 +39,7 @@ class CMicroFEHost {
38
39
  */
39
40
  constructor(params) {
40
41
  this.#guest = params.guest;
41
- this.#containerId = params.containerId;
42
+ this.#instanceId = params.instanceId;
42
43
  this.#version = params?.version || import_constant.LATEST_VERSION;
43
44
  this.#soManager = params.soManager;
44
45
  this.#eventManager = params.eventManager;
@@ -77,7 +78,7 @@ class CMicroFEHost {
77
78
  const existingCallChain = proxy[propName]?.callContext?.callChain;
78
79
  Object.defineProperty(propValue, "callContext", {
79
80
  value: {
80
- guest: this.#guest,
81
+ guest: { ...this.#guest, instanceId: this.#instanceId },
81
82
  ...existingCallChain?.length ? { callChain: existingCallChain } : {}
82
83
  },
83
84
  configurable: true,
@@ -102,9 +103,8 @@ class CMicroFEHost {
102
103
  * @param {AppWindowSize} appSize window size of the application
103
104
  */
104
105
  setAppWindowSize = (appSize) => {
105
- if (!this.#containerId) return;
106
106
  const { size } = appSize;
107
- const frameEle = document.getElementById(this.#containerId);
107
+ const frameEle = document.getElementById(this.#instanceId);
108
108
  if (frameEle) {
109
109
  frameEle.style.height = `${size.height}px`;
110
110
  frameEle.style.width = `${size.width}px`;
@@ -115,7 +115,10 @@ class CMicroFEHost {
115
115
  * @param {SubscribeParam<EventId, EventListener>} params - parameters to add event listener
116
116
  * @returns subscription id
117
117
  */
118
- subscribe = (params) => this.#eventManager.subscribe(params);
118
+ subscribe = (params) => this.#eventManager.subscribe({
119
+ ...params,
120
+ ...GUEST_SCOPED_MODULE_EVENTS.has(params.eventId) ? { guestId: this.#instanceId } : {}
121
+ });
119
122
  /**
120
123
  * removes listener from the scripting object event
121
124
  * @param {UnsubscribeParam<EventId>} params - parameters to remove event listener
@@ -65,9 +65,17 @@ class CAppBridge {
65
65
  */
66
66
  #activeApps = /* @__PURE__ */ new Map();
67
67
  /**
68
- * host-supplied metadata per guest, keyed by app id
68
+ * host-supplied metadata per guest, keyed by openApp instance id
69
69
  */
70
70
  #guestMetadata = /* @__PURE__ */ new Map();
71
+ /**
72
+ * per-instance module.unload handlers (instance id → closeApp)
73
+ */
74
+ #moduleUnloadHandlers = /* @__PURE__ */ new Map();
75
+ /**
76
+ * app ids that already have a shared module.unload router installed
77
+ */
78
+ #moduleUnloadRouterInstalled = /* @__PURE__ */ new Set();
71
79
  /**
72
80
  * instance of the app registry
73
81
  */
@@ -139,6 +147,7 @@ class CAppBridge {
139
147
  * @param options.documentEle - iframe document
140
148
  * @param options.elementIds - ids of injected script/style elements
141
149
  * @param options.microFEHost - pre-created host reference for guest-initiated loading
150
+ * @param options.selfInitialize
142
151
  */
143
152
  #addAppToActiveAppList = ({
144
153
  id,
@@ -186,14 +195,7 @@ class CAppBridge {
186
195
  throw new Error(
187
196
  `Application ${id} with instance id ${instanceId} doesn't expose init method`
188
197
  );
189
- const host = microFEHost ?? new CMicroFEHost({
190
- guest: { id },
191
- version: this.#version,
192
- containerId,
193
- logger: this.#logger,
194
- soManager: this.#soManager,
195
- eventManager: this.#eventManager
196
- });
198
+ const host = microFEHost ?? this.#createMicroFEHost({ id, instanceId, containerId });
197
199
  return app.init({
198
200
  host,
199
201
  hostUrl,
@@ -338,23 +340,74 @@ class CAppBridge {
338
340
  });
339
341
  }
340
342
  };
343
+ /**
344
+ * Resolve which openApp instance invoked module.unload when multiple
345
+ * instances share one module SO (keyed by app id).
346
+ * @param appId
347
+ * @param unloadTarget
348
+ */
349
+ #resolveModuleUnloadInstanceId = (appId, unloadTarget) => {
350
+ const guest = unloadTarget?.callContext?.guest;
351
+ if (guest?.instanceId) {
352
+ const active = this.#activeApps.get(guest.instanceId);
353
+ if (active?.id === appId) return guest.instanceId;
354
+ }
355
+ const guestWindow = guest?.guestWindow;
356
+ const openInstances = Array.from(this.#activeApps.entries()).filter(
357
+ ([, app]) => app.id === appId
358
+ );
359
+ if (guestWindow) {
360
+ const match = openInstances.find(
361
+ ([, app]) => app.guest.guestWindow === guestWindow
362
+ );
363
+ if (match) return match[0];
364
+ }
365
+ return openInstances.length === 1 ? openInstances[0][0] : void 0;
366
+ };
341
367
  /**
342
368
  * If the guest's module SO exposes _setUnloadHandler (duck-typed),
343
369
  * bind it to closeApp so the guest can trigger its own teardown.
370
+ * Multiple instances of the same app share one module SO; a router
371
+ * dispatches unload to the correct instance.
344
372
  * @param id
345
373
  * @param instanceId
346
374
  */
347
375
  #bindModuleUnload = (id, instanceId) => {
376
+ this.#moduleUnloadHandlers.set(instanceId, () => this.closeApp(instanceId));
348
377
  const moduleSO = this.#soManager.getObject(
349
378
  ScriptingObjectNames.Module,
350
379
  { id }
351
380
  );
352
381
  const so = moduleSO;
353
- const handler = so?._setUnloadHandler;
354
- if (typeof handler === "function") {
355
- handler(
356
- () => this.closeApp(instanceId)
357
- );
382
+ const setHandler = so?._setUnloadHandler;
383
+ if (typeof setHandler !== "function") return;
384
+ if (this.#moduleUnloadRouterInstalled.has(id)) return;
385
+ this.#moduleUnloadRouterInstalled.add(id);
386
+ setHandler.call(
387
+ moduleSO,
388
+ async () => {
389
+ const unloadFn = moduleSO.unload;
390
+ const resolvedInstanceId = this.#resolveModuleUnloadInstanceId(
391
+ id,
392
+ typeof unloadFn === "function" ? unloadFn : moduleSO
393
+ );
394
+ if (!resolvedInstanceId) {
395
+ this.#logger.warn(
396
+ `Cannot resolve instance for module.unload of ${id}`
397
+ );
398
+ return;
399
+ }
400
+ await this.#moduleUnloadHandlers.get(resolvedInstanceId)?.();
401
+ }
402
+ );
403
+ };
404
+ #unbindModuleUnload = (instanceId, appId) => {
405
+ this.#moduleUnloadHandlers.delete(instanceId);
406
+ const hasOpenInstances = [...this.#activeApps.entries()].some(
407
+ ([openInstanceId, app]) => app.id === appId && openInstanceId !== instanceId
408
+ );
409
+ if (!hasOpenInstances) {
410
+ this.#moduleUnloadRouterInstalled.delete(appId);
358
411
  }
359
412
  };
360
413
  /**
@@ -365,37 +418,54 @@ class CAppBridge {
365
418
  ScriptingObjectNames.Module,
366
419
  { id: guestAppId }
367
420
  );
421
+ #createMicroFEHost = ({
422
+ id,
423
+ instanceId,
424
+ containerId
425
+ }) => new CMicroFEHost({
426
+ guest: { id },
427
+ instanceId,
428
+ version: this.#version,
429
+ containerId,
430
+ logger: this.#logger,
431
+ soManager: this.#soManager,
432
+ eventManager: this.#eventManager
433
+ });
368
434
  /**
369
435
  * Dispatch module.unloading or module.unmount to guest listeners.
370
436
  * Uses a stub module SO when none is registered; routing is by eventId.
371
- * @param guestAppId
437
+ * eventOptions.guestId is the openApp instance id (iframe id), not app id.
438
+ * @param guest
372
439
  * @param eventId
373
440
  * @param eventName
374
441
  */
375
- #dispatchGuestModuleEvent = async (guestAppId, eventId, eventName) => {
376
- const moduleSO = this.#getGuestModuleScriptingObject(guestAppId) ?? MODULE_EVENT_STUB;
442
+ #dispatchGuestModuleEvent = async (guest, eventId, eventName) => {
443
+ const moduleSO = this.#getGuestModuleScriptingObject(guest.id) ?? MODULE_EVENT_STUB;
377
444
  return this.#eventManager.dispatchEvent(moduleSO, {
378
445
  event: { id: eventId, name: eventName },
379
- eventParams: { moduleId: guestAppId },
380
- eventOptions: { timeout: MODULE_EVENT_TIMEOUT_MS }
446
+ eventParams: { moduleId: guest.id },
447
+ eventOptions: {
448
+ timeout: MODULE_EVENT_TIMEOUT_MS,
449
+ guestId: guest.instanceId
450
+ }
381
451
  });
382
452
  };
383
- #canUnload = async (id) => {
453
+ #canUnload = async (guest) => {
384
454
  try {
385
455
  const feedback = await this.#dispatchGuestModuleEvent(
386
- id,
456
+ guest,
387
457
  MODULE_UNLOADING_EVENT,
388
458
  "unloading"
389
459
  );
390
460
  if (isModuleUnloadVetoed(feedback)) {
391
461
  this.#logger.info(
392
- `Unload denied by guest ${id} via module.unloading event`
462
+ `Unload denied by guest ${guest.id} via module.unloading event`
393
463
  );
394
464
  return false;
395
465
  }
396
466
  } catch (err) {
397
467
  this.#logger.warn({
398
- message: `Error dispatching module.unloading event for ${id}; proceeding with unload`,
468
+ message: `Error dispatching module.unloading event for ${guest.id}; proceeding with unload`,
399
469
  exception: err
400
470
  });
401
471
  }
@@ -418,7 +488,11 @@ class CAppBridge {
418
488
  return;
419
489
  }
420
490
  try {
421
- await this.#dispatchGuestModuleEvent(id, MODULE_UNMOUNT_EVENT, "unmount");
491
+ await this.#dispatchGuestModuleEvent(
492
+ { id, instanceId },
493
+ MODULE_UNMOUNT_EVENT,
494
+ "unmount"
495
+ );
422
496
  } catch (err) {
423
497
  this.#logger.warn({
424
498
  message: `Error dispatching module.unmount event for ${id}`,
@@ -499,9 +573,10 @@ class CAppBridge {
499
573
  const callerCtx = so[propName]?.callContext;
500
574
  if (callerCtx?.guest) {
501
575
  const existingChain = callerCtx.callChain ?? [];
502
- const guestId = callerCtx.guest.id;
503
- const meta = this.#guestMetadata.get(guestId);
504
- const callerEntry = { id: guestId };
576
+ const guestCtx = callerCtx.guest;
577
+ const metadataKey = guestCtx.instanceId ?? guestCtx.id;
578
+ const meta = this.#guestMetadata.get(metadataKey);
579
+ const callerEntry = { id: guestCtx.id };
505
580
  if (meta) callerEntry.metadata = meta;
506
581
  Object.defineProperty(propValue, "callContext", {
507
582
  value: {
@@ -558,11 +633,13 @@ class CAppBridge {
558
633
  return;
559
634
  }
560
635
  const { id, selfInitialize } = app;
561
- if (!await this.#canUnload(id)) return;
636
+ if (!await this.#canUnload({ id, instanceId })) return;
562
637
  const appConfig = this.#microFEConfig.getConfigById(id);
563
638
  if (!appConfig) {
564
639
  this.#logger.warn(`Configuration for application ${id} is not found`);
565
640
  this.#activeApps.delete(instanceId);
641
+ this.#guestMetadata.delete(instanceId);
642
+ this.#unbindModuleUnload(instanceId, id);
566
643
  Frame.remove(instanceId);
567
644
  return;
568
645
  }
@@ -584,7 +661,8 @@ class CAppBridge {
584
661
  frameEle.src = "about:blank";
585
662
  }
586
663
  this.#activeApps.delete(instanceId);
587
- this.#guestMetadata.delete(id);
664
+ this.#guestMetadata.delete(instanceId);
665
+ this.#unbindModuleUnload(instanceId, id);
588
666
  Frame.remove(instanceId);
589
667
  }
590
668
  };
@@ -690,17 +768,11 @@ class CAppBridge {
690
768
  * @param appConfig.hostUrl
691
769
  * @param options
692
770
  * @param options.id
771
+ * @param options.instanceId
693
772
  * @param options.containerId
694
773
  */
695
774
  #setupIframe = (frameDoc, appConfig, options) => {
696
- const microFEHost = new CMicroFEHost({
697
- guest: { id: options.id },
698
- version: this.#version,
699
- containerId: options.containerId,
700
- logger: this.#logger,
701
- soManager: this.#soManager,
702
- eventManager: this.#eventManager
703
- });
775
+ const microFEHost = this.#createMicroFEHost(options);
704
776
  const iframeWindow = frameDoc.defaultView;
705
777
  if (iframeWindow) {
706
778
  iframeWindow.emui = iframeWindow.emui ?? {};
@@ -734,7 +806,7 @@ class CAppBridge {
734
806
  selfInitialize
735
807
  } = params;
736
808
  const instanceId = uuidv4();
737
- if (metadata) this.#guestMetadata.set(id, metadata);
809
+ if (metadata) this.#guestMetadata.set(instanceId, metadata);
738
810
  const appConfig = this.#microFEConfig.getConfigById(id);
739
811
  if (!appConfig) {
740
812
  throw new Error(`Application ${id} is not found in app config`);
@@ -762,6 +834,7 @@ class CAppBridge {
762
834
  appConfig,
763
835
  {
764
836
  id,
837
+ instanceId,
765
838
  containerId: frameOptions?.containerId
766
839
  }
767
840
  );
@@ -786,6 +859,8 @@ class CAppBridge {
786
859
  this.#bindModuleUnload(id, instanceId);
787
860
  return instanceId;
788
861
  } catch (err) {
862
+ this.#guestMetadata.delete(instanceId);
863
+ this.#unbindModuleUnload(instanceId, id);
789
864
  this.#unloadApp({
790
865
  id,
791
866
  instanceId,