@elliemae/pui-app-bridge 2.35.3 → 2.35.5

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.
@@ -665,6 +665,7 @@ class CAppBridge {
665
665
  const appConfig = this.#microFEConfig.getConfigById(id);
666
666
  if (!appConfig) {
667
667
  this.#logger.warn(`Configuration for application ${id} is not found`);
668
+ app.microFEHost?.[import_microfeHost.DISPOSE_HOST]();
668
669
  this.removeAllScriptingObjects(instanceId);
669
670
  this.#eventManager.unsubscribeByGuestId(instanceId);
670
671
  this.#activeApps.delete(instanceId);
@@ -690,6 +691,7 @@ class CAppBridge {
690
691
  if (frameEle) {
691
692
  frameEle.src = "about:blank";
692
693
  }
694
+ app.microFEHost?.[import_microfeHost.DISPOSE_HOST]();
693
695
  this.removeAllScriptingObjects(instanceId);
694
696
  this.#eventManager.unsubscribeByGuestId(instanceId);
695
697
  this.#activeApps.delete(instanceId);
@@ -838,7 +840,6 @@ class CAppBridge {
838
840
  selfInitialize
839
841
  } = params;
840
842
  const instanceId = (0, import_uuid.v4)();
841
- if (metadata) this.#guestMetadata.set(instanceId, metadata);
842
843
  const appConfig = this.#microFEConfig.getConfigById(id);
843
844
  if (!appConfig) {
844
845
  throw new Error(`Application ${id} is not found in app config`);
@@ -853,23 +854,23 @@ class CAppBridge {
853
854
  hostUrl: appConfig.hostUrl,
854
855
  options: { title: appConfig.name, ...frameOptions }
855
856
  });
856
- if (!frameEle?.contentDocument)
857
+ if (!frameEle?.contentDocument) {
858
+ import_frame.Frame.remove(instanceId);
857
859
  throw new Error("unable to create iframe for the microapp");
860
+ }
861
+ let microFEHost;
858
862
  try {
863
+ if (metadata) this.#guestMetadata.set(instanceId, metadata);
859
864
  this.#appRegistry.add({
860
865
  id,
861
866
  instanceId,
862
867
  documentEle: frameEle.contentDocument
863
868
  });
864
- const microFEHost = this.#setupIframe(
865
- frameEle.contentDocument,
866
- appConfig,
867
- {
868
- id,
869
- instanceId,
870
- containerId: frameOptions?.containerId
871
- }
872
- );
869
+ microFEHost = this.#setupIframe(frameEle.contentDocument, appConfig, {
870
+ id,
871
+ instanceId,
872
+ containerId: frameOptions?.containerId
873
+ });
873
874
  const manifest = await manifestPromise;
874
875
  await this.#loadApp({
875
876
  instanceId,
@@ -891,6 +892,8 @@ class CAppBridge {
891
892
  this.#bindModuleUnload(id, instanceId);
892
893
  return instanceId;
893
894
  } catch (err) {
895
+ microFEHost?.[import_microfeHost.DISPOSE_HOST]();
896
+ this.#eventManager.unsubscribeByGuestId(instanceId);
894
897
  this.#guestMetadata.delete(instanceId);
895
898
  this.#unbindModuleUnload(instanceId, id);
896
899
  this.#unloadApp({
@@ -899,6 +902,8 @@ class CAppBridge {
899
902
  hostUrl: appConfig.hostUrl,
900
903
  documentEle: frameEle.contentDocument
901
904
  });
905
+ this.#activeApps.delete(instanceId);
906
+ this.removeAllScriptingObjects(instanceId);
902
907
  import_frame.Frame.remove(instanceId);
903
908
  throw err;
904
909
  }
@@ -207,7 +207,7 @@ class EventManager {
207
207
  * @param {SubscribeParam<EventId, AppEvents[EventId]>} param - parameters for subscribing to an event
208
208
  */
209
209
  subscribe = (param) => {
210
- const { eventId, callback, criteria, guestId } = param;
210
+ const { eventId, callback, criteria, guestId, ownerInstanceId } = param;
211
211
  if (!eventId) throw new Error("eventId is required");
212
212
  if (!callback) throw new Error("Callback is required");
213
213
  const listeners = this.#listeners.get(eventId) || [];
@@ -216,25 +216,57 @@ class EventManager {
216
216
  token,
217
217
  criteria,
218
218
  guestId,
219
+ ...ownerInstanceId ? { ownerInstanceId } : {},
219
220
  callback
220
221
  });
221
222
  this.#listeners.set(eventId, listeners);
222
223
  return token;
223
224
  };
224
225
  /**
225
- * Unsubscribe from an event
226
+ * Removes the listeners of an event that match the predicate
227
+ * @param {string} eventId event id
228
+ * @param {Function} shouldRemove predicate that selects the listeners to remove
229
+ */
230
+ #removeListeners = (eventId, shouldRemove) => {
231
+ const listeners = this.#listeners.get(eventId);
232
+ if (!listeners) return;
233
+ const remaining = listeners.filter((listener) => !shouldRemove(listener));
234
+ if (remaining.length === 0) {
235
+ this.#listeners.delete(eventId);
236
+ } else if (remaining.length !== listeners.length) {
237
+ this.#listeners.set(eventId, remaining);
238
+ }
239
+ };
240
+ /**
241
+ * Unsubscribe from an event.
242
+ *
243
+ * With a token, removes that subscription; a token that matches nothing
244
+ * (including an empty string) removes nothing. Without a token (undefined or
245
+ * null), removes every listener of the event, or, when `ownerInstanceId` is
246
+ * given, only the listeners registered by that owner. When `ownerInstanceId`
247
+ * is given, a token unsubscribe only removes a listener registered by that
248
+ * owner; a token owned by someone else is kept and a warning is logged.
249
+ * Removal never mutates the listener array an in-progress dispatch iterates.
226
250
  * @param {UnsubscribeParam<EventId>} param - parameters for unsubscribing from an event
227
251
  */
228
252
  unsubscribe = (param) => {
229
- const { eventId, token } = param;
253
+ const { eventId, token, ownerInstanceId } = param;
230
254
  if (!eventId) throw new Error("eventId id is required");
231
- if (token) {
232
- const listeners = this.#listeners.get(eventId) || [];
233
- const index = listeners.findIndex((listener) => listener.token === token);
234
- if (index > -1) {
235
- listeners.splice(index, 1);
236
- this.#listeners.set(eventId, listeners);
255
+ const isOwned = (listener) => !ownerInstanceId || listener.ownerInstanceId === ownerInstanceId;
256
+ if (token !== void 0 && token !== null) {
257
+ const listener = this.#listeners.get(eventId)?.find((item) => item.token === token);
258
+ if (!listener) return;
259
+ if (!isOwned(listener)) {
260
+ this.#logger?.warn(
261
+ `Ignoring unsubscribe from ${eventId} by ${String(
262
+ ownerInstanceId
263
+ )}: the subscription belongs to another owner`
264
+ );
265
+ return;
237
266
  }
267
+ this.#removeListeners(eventId, (item) => item.token === token);
268
+ } else if (ownerInstanceId) {
269
+ this.#removeListeners(eventId, isOwned);
238
270
  } else {
239
271
  this.#listeners.delete(eventId);
240
272
  }
@@ -246,21 +278,18 @@ class EventManager {
246
278
  this.#listeners.clear();
247
279
  };
248
280
  /**
249
- * Removes every listener registered for a guest iframe host instance.
250
- * Global listeners (no guestId) are preserved.
281
+ * Removes every listener registered for a guest iframe host instance:
282
+ * listeners scoped to it (`guestId`) and listeners it owns
283
+ * (`ownerInstanceId`). Listeners of the host and of other guests are preserved.
251
284
  * @param guestId openApp instance id
252
285
  */
253
286
  unsubscribeByGuestId = (guestId) => {
254
287
  if (!guestId) return;
255
- this.#listeners.forEach((listeners, eventId) => {
256
- const remaining = listeners.filter(
257
- (listener) => listener.guestId !== guestId
288
+ [...this.#listeners.keys()].forEach((eventId) => {
289
+ this.#removeListeners(
290
+ eventId,
291
+ (listener) => listener.guestId === guestId || listener.ownerInstanceId === guestId
258
292
  );
259
- if (remaining.length === 0) {
260
- this.#listeners.delete(eventId);
261
- } else if (remaining.length !== listeners.length) {
262
- this.#listeners.set(eventId, remaining);
263
- }
264
293
  });
265
294
  };
266
295
  }
@@ -18,33 +18,49 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var microfeHost_exports = {};
20
20
  __export(microfeHost_exports, {
21
- CMicroFEHost: () => CMicroFEHost
21
+ CMicroFEHost: () => CMicroFEHost,
22
+ DISPOSE_HOST: () => DISPOSE_HOST
22
23
  });
23
24
  module.exports = __toCommonJS(microfeHost_exports);
24
25
  var import_microfe_common = require("@elliemae/microfe-common");
25
26
  var import_constant = require("./constant.js");
27
+ const DISPOSE_HOST = /* @__PURE__ */ Symbol("pui-app-bridge.disposeMicroFEHost");
26
28
  const GUEST_SCOPED_MODULE_EVENTS = /* @__PURE__ */ new Set([
27
29
  "module.unloading",
28
30
  "module.unmount"
29
31
  ]);
30
32
  const inFlightCalls = /* @__PURE__ */ new WeakMap();
31
- const settleCall = (func, callContext) => {
32
- const remaining = (inFlightCalls.get(func) ?? 1) - 1;
33
- if (remaining > 0) {
34
- inFlightCalls.set(func, remaining);
33
+ const startCall = (func, callContext) => {
34
+ const entry = inFlightCalls.get(func);
35
+ inFlightCalls.set(func, {
36
+ count: (entry?.count ?? 0) + 1,
37
+ latestContext: callContext
38
+ });
39
+ };
40
+ const settleCall = (func) => {
41
+ const entry = inFlightCalls.get(func);
42
+ if (!entry) return;
43
+ if (entry.count > 1) {
44
+ entry.count -= 1;
35
45
  return;
36
46
  }
37
47
  inFlightCalls.delete(func);
38
- if (func.callContext === callContext) {
48
+ if (func.callContext === entry.latestContext) {
39
49
  delete func.callContext;
40
50
  }
41
51
  };
52
+ const isThenable = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.then === "function";
42
53
  class CMicroFEHost {
43
54
  #guest;
44
55
  #instanceId;
45
56
  #version;
46
57
  #soManager;
47
58
  #eventManager;
59
+ #logger;
60
+ /**
61
+ * set once the guest is closed; a disposed host registers no new listeners
62
+ */
63
+ #disposed = false;
48
64
  /**
49
65
  * Create new instance of the host frontend application
50
66
  * @param {CMicroFEHostParams} params - constructor parameters
@@ -55,6 +71,7 @@ class CMicroFEHost {
55
71
  this.#version = params?.version || import_constant.LATEST_VERSION;
56
72
  this.#soManager = params.soManager;
57
73
  this.#eventManager = params.eventManager;
74
+ this.#logger = params.logger;
58
75
  }
59
76
  /**
60
77
  * application release version
@@ -99,21 +116,21 @@ class CMicroFEHost {
99
116
  writable: true
100
117
  });
101
118
  const func = propValue;
102
- inFlightCalls.set(func, (inFlightCalls.get(func) ?? 0) + 1);
119
+ startCall(func, callContext);
103
120
  let retVal;
104
121
  try {
105
122
  retVal = so[propName](...args);
106
123
  } catch (err) {
107
- settleCall(func, callContext);
124
+ settleCall(func);
108
125
  throw err;
109
126
  }
110
- if (retVal instanceof Promise) {
127
+ if (isThenable(retVal)) {
111
128
  retVal.then(
112
- () => settleCall(func, callContext),
113
- () => settleCall(func, callContext)
129
+ () => settleCall(func),
130
+ () => settleCall(func)
114
131
  );
115
132
  } else {
116
- settleCall(func, callContext);
133
+ settleCall(func);
117
134
  }
118
135
  return retVal;
119
136
  },
@@ -139,19 +156,41 @@ class CMicroFEHost {
139
156
  }
140
157
  };
141
158
  /**
142
- * add listener to the scripting object event
159
+ * add listener to the scripting object event.
160
+ * The listener is owned by this guest instance and is removed when the guest is closed.
143
161
  * @param {SubscribeParam<EventId, EventListener>} params - parameters to add event listener
144
- * @returns subscription id
162
+ * @returns subscription id, or an empty string when the guest is already closed
145
163
  */
146
- subscribe = (params) => this.#eventManager.subscribe({
147
- ...params,
148
- ...GUEST_SCOPED_MODULE_EVENTS.has(params.eventId) ? { guestId: this.#instanceId } : {}
149
- });
164
+ subscribe = (params) => {
165
+ if (this.#disposed) {
166
+ this.#logger?.warn(
167
+ `Ignoring subscribe to ${params?.eventId} from guest ${this.#guest.id} with instance id ${this.#instanceId}: the guest is closed`
168
+ );
169
+ return "";
170
+ }
171
+ return this.#eventManager.subscribe({
172
+ ...params,
173
+ ...GUEST_SCOPED_MODULE_EVENTS.has(params.eventId) ? { guestId: this.#instanceId } : {},
174
+ ownerInstanceId: this.#instanceId
175
+ });
176
+ };
150
177
  /**
151
- * removes listener from the scripting object event
178
+ * removes listener from the scripting object event.
179
+ * Only listeners registered by this guest instance are removed.
152
180
  * @param {UnsubscribeParam<EventId>} params - parameters to remove event listener
153
181
  */
154
182
  unsubscribe = (params) => {
155
- this.#eventManager.unsubscribe(params);
183
+ this.#eventManager.unsubscribe({
184
+ ...params,
185
+ ownerInstanceId: this.#instanceId
186
+ });
156
187
  };
188
+ /**
189
+ * Called by the app bridge when the guest is closed.
190
+ * Later subscribe calls are ignored so a closed guest cannot register listeners again.
191
+ * @internal
192
+ */
193
+ [DISPOSE_HOST]() {
194
+ this.#disposed = true;
195
+ }
157
196
  }
@@ -16,7 +16,7 @@ import { getCurrentBreakpoint, getViewportSize } from "./typings/window.js";
16
16
  import { CAppConfig } from "./config/app.js";
17
17
  import { CMicroFEConfig } from "./config/microFE.js";
18
18
  import { StyleLoader, ScriptLoader, ManifestLoader } from "./loaders/index.js";
19
- import { CMicroFEHost } from "./microfeHost.js";
19
+ import { CMicroFEHost, DISPOSE_HOST } from "./microfeHost.js";
20
20
  import { CAppRegistry } from "./appRegistry.js";
21
21
  const KEEP_ALIVE_INTERVAL = 12e4;
22
22
  const MODULE_UNLOADING_EVENT = "module.unloading";
@@ -640,6 +640,7 @@ class CAppBridge {
640
640
  const appConfig = this.#microFEConfig.getConfigById(id);
641
641
  if (!appConfig) {
642
642
  this.#logger.warn(`Configuration for application ${id} is not found`);
643
+ app.microFEHost?.[DISPOSE_HOST]();
643
644
  this.removeAllScriptingObjects(instanceId);
644
645
  this.#eventManager.unsubscribeByGuestId(instanceId);
645
646
  this.#activeApps.delete(instanceId);
@@ -665,6 +666,7 @@ class CAppBridge {
665
666
  if (frameEle) {
666
667
  frameEle.src = "about:blank";
667
668
  }
669
+ app.microFEHost?.[DISPOSE_HOST]();
668
670
  this.removeAllScriptingObjects(instanceId);
669
671
  this.#eventManager.unsubscribeByGuestId(instanceId);
670
672
  this.#activeApps.delete(instanceId);
@@ -813,7 +815,6 @@ class CAppBridge {
813
815
  selfInitialize
814
816
  } = params;
815
817
  const instanceId = uuidv4();
816
- if (metadata) this.#guestMetadata.set(instanceId, metadata);
817
818
  const appConfig = this.#microFEConfig.getConfigById(id);
818
819
  if (!appConfig) {
819
820
  throw new Error(`Application ${id} is not found in app config`);
@@ -828,23 +829,23 @@ class CAppBridge {
828
829
  hostUrl: appConfig.hostUrl,
829
830
  options: { title: appConfig.name, ...frameOptions }
830
831
  });
831
- if (!frameEle?.contentDocument)
832
+ if (!frameEle?.contentDocument) {
833
+ Frame.remove(instanceId);
832
834
  throw new Error("unable to create iframe for the microapp");
835
+ }
836
+ let microFEHost;
833
837
  try {
838
+ if (metadata) this.#guestMetadata.set(instanceId, metadata);
834
839
  this.#appRegistry.add({
835
840
  id,
836
841
  instanceId,
837
842
  documentEle: frameEle.contentDocument
838
843
  });
839
- const microFEHost = this.#setupIframe(
840
- frameEle.contentDocument,
841
- appConfig,
842
- {
843
- id,
844
- instanceId,
845
- containerId: frameOptions?.containerId
846
- }
847
- );
844
+ microFEHost = this.#setupIframe(frameEle.contentDocument, appConfig, {
845
+ id,
846
+ instanceId,
847
+ containerId: frameOptions?.containerId
848
+ });
848
849
  const manifest = await manifestPromise;
849
850
  await this.#loadApp({
850
851
  instanceId,
@@ -866,6 +867,8 @@ class CAppBridge {
866
867
  this.#bindModuleUnload(id, instanceId);
867
868
  return instanceId;
868
869
  } catch (err) {
870
+ microFEHost?.[DISPOSE_HOST]();
871
+ this.#eventManager.unsubscribeByGuestId(instanceId);
869
872
  this.#guestMetadata.delete(instanceId);
870
873
  this.#unbindModuleUnload(instanceId, id);
871
874
  this.#unloadApp({
@@ -874,6 +877,8 @@ class CAppBridge {
874
877
  hostUrl: appConfig.hostUrl,
875
878
  documentEle: frameEle.contentDocument
876
879
  });
880
+ this.#activeApps.delete(instanceId);
881
+ this.removeAllScriptingObjects(instanceId);
877
882
  Frame.remove(instanceId);
878
883
  throw err;
879
884
  }
@@ -184,7 +184,7 @@ class EventManager {
184
184
  * @param {SubscribeParam<EventId, AppEvents[EventId]>} param - parameters for subscribing to an event
185
185
  */
186
186
  subscribe = (param) => {
187
- const { eventId, callback, criteria, guestId } = param;
187
+ const { eventId, callback, criteria, guestId, ownerInstanceId } = param;
188
188
  if (!eventId) throw new Error("eventId is required");
189
189
  if (!callback) throw new Error("Callback is required");
190
190
  const listeners = this.#listeners.get(eventId) || [];
@@ -193,25 +193,57 @@ class EventManager {
193
193
  token,
194
194
  criteria,
195
195
  guestId,
196
+ ...ownerInstanceId ? { ownerInstanceId } : {},
196
197
  callback
197
198
  });
198
199
  this.#listeners.set(eventId, listeners);
199
200
  return token;
200
201
  };
201
202
  /**
202
- * Unsubscribe from an event
203
+ * Removes the listeners of an event that match the predicate
204
+ * @param {string} eventId event id
205
+ * @param {Function} shouldRemove predicate that selects the listeners to remove
206
+ */
207
+ #removeListeners = (eventId, shouldRemove) => {
208
+ const listeners = this.#listeners.get(eventId);
209
+ if (!listeners) return;
210
+ const remaining = listeners.filter((listener) => !shouldRemove(listener));
211
+ if (remaining.length === 0) {
212
+ this.#listeners.delete(eventId);
213
+ } else if (remaining.length !== listeners.length) {
214
+ this.#listeners.set(eventId, remaining);
215
+ }
216
+ };
217
+ /**
218
+ * Unsubscribe from an event.
219
+ *
220
+ * With a token, removes that subscription; a token that matches nothing
221
+ * (including an empty string) removes nothing. Without a token (undefined or
222
+ * null), removes every listener of the event, or, when `ownerInstanceId` is
223
+ * given, only the listeners registered by that owner. When `ownerInstanceId`
224
+ * is given, a token unsubscribe only removes a listener registered by that
225
+ * owner; a token owned by someone else is kept and a warning is logged.
226
+ * Removal never mutates the listener array an in-progress dispatch iterates.
203
227
  * @param {UnsubscribeParam<EventId>} param - parameters for unsubscribing from an event
204
228
  */
205
229
  unsubscribe = (param) => {
206
- const { eventId, token } = param;
230
+ const { eventId, token, ownerInstanceId } = param;
207
231
  if (!eventId) throw new Error("eventId id is required");
208
- if (token) {
209
- const listeners = this.#listeners.get(eventId) || [];
210
- const index = listeners.findIndex((listener) => listener.token === token);
211
- if (index > -1) {
212
- listeners.splice(index, 1);
213
- this.#listeners.set(eventId, listeners);
232
+ const isOwned = (listener) => !ownerInstanceId || listener.ownerInstanceId === ownerInstanceId;
233
+ if (token !== void 0 && token !== null) {
234
+ const listener = this.#listeners.get(eventId)?.find((item) => item.token === token);
235
+ if (!listener) return;
236
+ if (!isOwned(listener)) {
237
+ this.#logger?.warn(
238
+ `Ignoring unsubscribe from ${eventId} by ${String(
239
+ ownerInstanceId
240
+ )}: the subscription belongs to another owner`
241
+ );
242
+ return;
214
243
  }
244
+ this.#removeListeners(eventId, (item) => item.token === token);
245
+ } else if (ownerInstanceId) {
246
+ this.#removeListeners(eventId, isOwned);
215
247
  } else {
216
248
  this.#listeners.delete(eventId);
217
249
  }
@@ -223,21 +255,18 @@ class EventManager {
223
255
  this.#listeners.clear();
224
256
  };
225
257
  /**
226
- * Removes every listener registered for a guest iframe host instance.
227
- * Global listeners (no guestId) are preserved.
258
+ * Removes every listener registered for a guest iframe host instance:
259
+ * listeners scoped to it (`guestId`) and listeners it owns
260
+ * (`ownerInstanceId`). Listeners of the host and of other guests are preserved.
228
261
  * @param guestId openApp instance id
229
262
  */
230
263
  unsubscribeByGuestId = (guestId) => {
231
264
  if (!guestId) return;
232
- this.#listeners.forEach((listeners, eventId) => {
233
- const remaining = listeners.filter(
234
- (listener) => listener.guestId !== guestId
265
+ [...this.#listeners.keys()].forEach((eventId) => {
266
+ this.#removeListeners(
267
+ eventId,
268
+ (listener) => listener.guestId === guestId || listener.ownerInstanceId === guestId
235
269
  );
236
- if (remaining.length === 0) {
237
- this.#listeners.delete(eventId);
238
- } else if (remaining.length !== listeners.length) {
239
- this.#listeners.set(eventId, remaining);
240
- }
241
270
  });
242
271
  };
243
272
  }
@@ -4,28 +4,43 @@ import {
4
4
  ScriptingObjectProxy
5
5
  } from "@elliemae/microfe-common";
6
6
  import { LATEST_VERSION } from "./constant.js";
7
+ const DISPOSE_HOST = /* @__PURE__ */ Symbol("pui-app-bridge.disposeMicroFEHost");
7
8
  const GUEST_SCOPED_MODULE_EVENTS = /* @__PURE__ */ new Set([
8
9
  "module.unloading",
9
10
  "module.unmount"
10
11
  ]);
11
12
  const inFlightCalls = /* @__PURE__ */ new WeakMap();
12
- const settleCall = (func, callContext) => {
13
- const remaining = (inFlightCalls.get(func) ?? 1) - 1;
14
- if (remaining > 0) {
15
- inFlightCalls.set(func, remaining);
13
+ const startCall = (func, callContext) => {
14
+ const entry = inFlightCalls.get(func);
15
+ inFlightCalls.set(func, {
16
+ count: (entry?.count ?? 0) + 1,
17
+ latestContext: callContext
18
+ });
19
+ };
20
+ const settleCall = (func) => {
21
+ const entry = inFlightCalls.get(func);
22
+ if (!entry) return;
23
+ if (entry.count > 1) {
24
+ entry.count -= 1;
16
25
  return;
17
26
  }
18
27
  inFlightCalls.delete(func);
19
- if (func.callContext === callContext) {
28
+ if (func.callContext === entry.latestContext) {
20
29
  delete func.callContext;
21
30
  }
22
31
  };
32
+ const isThenable = (value) => (typeof value === "object" || typeof value === "function") && value !== null && typeof value.then === "function";
23
33
  class CMicroFEHost {
24
34
  #guest;
25
35
  #instanceId;
26
36
  #version;
27
37
  #soManager;
28
38
  #eventManager;
39
+ #logger;
40
+ /**
41
+ * set once the guest is closed; a disposed host registers no new listeners
42
+ */
43
+ #disposed = false;
29
44
  /**
30
45
  * Create new instance of the host frontend application
31
46
  * @param {CMicroFEHostParams} params - constructor parameters
@@ -36,6 +51,7 @@ class CMicroFEHost {
36
51
  this.#version = params?.version || LATEST_VERSION;
37
52
  this.#soManager = params.soManager;
38
53
  this.#eventManager = params.eventManager;
54
+ this.#logger = params.logger;
39
55
  }
40
56
  /**
41
57
  * application release version
@@ -80,21 +96,21 @@ class CMicroFEHost {
80
96
  writable: true
81
97
  });
82
98
  const func = propValue;
83
- inFlightCalls.set(func, (inFlightCalls.get(func) ?? 0) + 1);
99
+ startCall(func, callContext);
84
100
  let retVal;
85
101
  try {
86
102
  retVal = so[propName](...args);
87
103
  } catch (err) {
88
- settleCall(func, callContext);
104
+ settleCall(func);
89
105
  throw err;
90
106
  }
91
- if (retVal instanceof Promise) {
107
+ if (isThenable(retVal)) {
92
108
  retVal.then(
93
- () => settleCall(func, callContext),
94
- () => settleCall(func, callContext)
109
+ () => settleCall(func),
110
+ () => settleCall(func)
95
111
  );
96
112
  } else {
97
- settleCall(func, callContext);
113
+ settleCall(func);
98
114
  }
99
115
  return retVal;
100
116
  },
@@ -120,22 +136,45 @@ class CMicroFEHost {
120
136
  }
121
137
  };
122
138
  /**
123
- * add listener to the scripting object event
139
+ * add listener to the scripting object event.
140
+ * The listener is owned by this guest instance and is removed when the guest is closed.
124
141
  * @param {SubscribeParam<EventId, EventListener>} params - parameters to add event listener
125
- * @returns subscription id
142
+ * @returns subscription id, or an empty string when the guest is already closed
126
143
  */
127
- subscribe = (params) => this.#eventManager.subscribe({
128
- ...params,
129
- ...GUEST_SCOPED_MODULE_EVENTS.has(params.eventId) ? { guestId: this.#instanceId } : {}
130
- });
144
+ subscribe = (params) => {
145
+ if (this.#disposed) {
146
+ this.#logger?.warn(
147
+ `Ignoring subscribe to ${params?.eventId} from guest ${this.#guest.id} with instance id ${this.#instanceId}: the guest is closed`
148
+ );
149
+ return "";
150
+ }
151
+ return this.#eventManager.subscribe({
152
+ ...params,
153
+ ...GUEST_SCOPED_MODULE_EVENTS.has(params.eventId) ? { guestId: this.#instanceId } : {},
154
+ ownerInstanceId: this.#instanceId
155
+ });
156
+ };
131
157
  /**
132
- * removes listener from the scripting object event
158
+ * removes listener from the scripting object event.
159
+ * Only listeners registered by this guest instance are removed.
133
160
  * @param {UnsubscribeParam<EventId>} params - parameters to remove event listener
134
161
  */
135
162
  unsubscribe = (params) => {
136
- this.#eventManager.unsubscribe(params);
163
+ this.#eventManager.unsubscribe({
164
+ ...params,
165
+ ownerInstanceId: this.#instanceId
166
+ });
137
167
  };
168
+ /**
169
+ * Called by the app bridge when the guest is closed.
170
+ * Later subscribe calls are ignored so a closed guest cannot register listeners again.
171
+ * @internal
172
+ */
173
+ [DISPOSE_HOST]() {
174
+ this.#disposed = true;
175
+ }
138
176
  }
139
177
  export {
140
- CMicroFEHost
178
+ CMicroFEHost,
179
+ DISPOSE_HOST
141
180
  };