@elliemae/ssf-host 2.29.0-alpha.0 → 2.29.1

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 (49) hide show
  1. package/dist/cjs/guest.js +5 -5
  2. package/dist/cjs/host.js +242 -199
  3. package/dist/cjs/tests/utils.js +6 -8
  4. package/dist/cjs/utils.js +1 -1
  5. package/dist/esm/guest.js +5 -5
  6. package/dist/esm/host.js +242 -199
  7. package/dist/esm/tests/utils.js +6 -8
  8. package/dist/esm/utils.js +1 -1
  9. package/dist/public/callchain-host.html +1 -1
  10. package/dist/public/callchain-intermediate.html +1 -1
  11. package/dist/public/e2e-host.html +1 -1
  12. package/dist/public/e2e-index.html +1 -1
  13. package/dist/public/index.html +1 -1
  14. package/dist/public/js/emuiSsfHost.e05998eeb058c9f027f1.js +3 -0
  15. package/dist/public/js/emuiSsfHost.e05998eeb058c9f027f1.js.br +0 -0
  16. package/dist/public/js/emuiSsfHost.e05998eeb058c9f027f1.js.gz +0 -0
  17. package/dist/public/js/emuiSsfHost.e05998eeb058c9f027f1.js.map +1 -0
  18. package/dist/public/popup-focus-host.html +1 -1
  19. package/dist/public/utils.js +1 -1
  20. package/dist/public/utils.js.br +0 -0
  21. package/dist/public/utils.js.gz +0 -0
  22. package/dist/public/utils.js.map +1 -1
  23. package/dist/public/v1-guest-v2-host.html +1 -1
  24. package/dist/public/v2-host-v1-guest.html +1 -1
  25. package/dist/types/lib/guest.d.ts +2 -2
  26. package/dist/types/lib/host.d.ts +5 -5
  27. package/dist/types/lib/ihost.d.ts +4 -4
  28. package/dist/types/lib/tests/scriptingObjects/analytics.d.ts +1 -1
  29. package/dist/types/lib/tests/scriptingObjects/appEvents.d.ts +2 -2
  30. package/dist/types/lib/tests/scriptingObjects/appObjects.d.ts +3 -3
  31. package/dist/types/lib/tests/scriptingObjects/appraisalServiceModule.d.ts +1 -1
  32. package/dist/types/lib/tests/scriptingObjects/creditServiceModule.d.ts +1 -1
  33. package/dist/types/lib/tests/server.d.ts +1 -1
  34. package/dist/types/lib/tests/utils.d.ts +13 -21
  35. package/dist/types/lib/utils.d.ts +2 -2
  36. package/dist/types/tsconfig.tsbuildinfo +1 -1
  37. package/dist/umd/index.js +1 -1
  38. package/dist/umd/index.js.br +0 -0
  39. package/dist/umd/index.js.gz +0 -0
  40. package/dist/umd/index.js.map +1 -1
  41. package/dist/umd/utils.js +1 -1
  42. package/dist/umd/utils.js.br +0 -0
  43. package/dist/umd/utils.js.gz +0 -0
  44. package/dist/umd/utils.js.map +1 -1
  45. package/package.json +10 -10
  46. package/dist/public/js/emuiSsfHost.4b00d8e6203361c7da33.js +0 -3
  47. package/dist/public/js/emuiSsfHost.4b00d8e6203361c7da33.js.br +0 -0
  48. package/dist/public/js/emuiSsfHost.4b00d8e6203361c7da33.js.gz +0 -0
  49. package/dist/public/js/emuiSsfHost.4b00d8e6203361c7da33.js.map +0 -1
package/dist/cjs/guest.js CHANGED
@@ -119,7 +119,7 @@ class Guest {
119
119
  try {
120
120
  this.window.document;
121
121
  this.window.close();
122
- } catch {
122
+ } catch (e) {
123
123
  this.#remoting.send({
124
124
  targetWin: this.window,
125
125
  targetOrigin: this.origin,
@@ -148,18 +148,18 @@ class Guest {
148
148
  handShake = () => new Promise((resolve) => {
149
149
  let handShakeCount = 0;
150
150
  const handShakeRetries = 5;
151
- const handShakeHandleRef = {};
151
+ let handShakeHandle;
152
152
  const onAck = () => {
153
- clearInterval(handShakeHandleRef.id);
153
+ clearInterval(handShakeHandle);
154
154
  this.#remoting.unlisten({
155
155
  messageType: import_microfe_common.MessageType.HandShakeAck,
156
156
  callback: onAck
157
157
  });
158
158
  resolve(true);
159
159
  };
160
- handShakeHandleRef.id = setInterval(() => {
160
+ handShakeHandle = setInterval(() => {
161
161
  if (handShakeCount >= handShakeRetries) {
162
- clearInterval(handShakeHandleRef.id);
162
+ clearInterval(handShakeHandle);
163
163
  this.#remoting.unlisten({
164
164
  messageType: import_microfe_common.MessageType.HandShakeAck,
165
165
  callback: onAck
package/dist/cjs/host.js CHANGED
@@ -36,7 +36,16 @@ const SANDBOX_DEFAULT = [
36
36
  import_types.IFrameSandboxValues.AllowDownloads,
37
37
  import_types.IFrameSandboxValues.AllowSameOrigin
38
38
  ];
39
+ const MODULE_OBJECT_ID = "module";
40
+ const MODULE_UNLOADING_EVENT = "module.unloading";
41
+ const MODULE_UNMOUNT_EVENT = "module.unmount";
42
+ const MODULE_EVENT_TIMEOUT_MS = 1e3;
43
+ const MODULE_EVENT_STUB = new import_microfe_common.ScriptingObject(MODULE_OBJECT_ID);
44
+ const isModuleUnloadVetoed = (feedback) => Array.isArray(feedback) && feedback.some((v) => v === false);
45
+ const ignoreRejectedPromise = () => {
46
+ };
39
47
  class SSFHost {
48
+ /* eslint-enable indent */
40
49
  /**
41
50
  * unique identifier for the host
42
51
  */
@@ -56,6 +65,7 @@ class SSFHost {
56
65
  /**
57
66
  * reference to the analytics object
58
67
  */
68
+ #analyticsObj;
59
69
  /**
60
70
  * list of guests
61
71
  */
@@ -111,16 +121,31 @@ class SSFHost {
111
121
  */
112
122
  constructor(hostId, option) {
113
123
  this.hostId = hostId;
114
- this.#validateHostOptions(option);
124
+ if (!option?.logger) throw new Error("Logger is required");
125
+ if (!option?.analyticsObj) throw new Error("Analytics object is required");
115
126
  this.#logger = option.logger;
116
- const trackers = this.#createPerfTrackers(option);
117
- this.#perfTracker = trackers.perfTracker;
118
- this.#guestPerfTracker = trackers.guestPerfTracker;
127
+ this.#analyticsObj = option.analyticsObj;
128
+ const perfOpts = {
129
+ analyticsObj: option.analyticsObj,
130
+ enabled: option?.measurePerformance,
131
+ samplingRatio: option?.performanceSamplingRatio,
132
+ onError: (msg) => this.#logger.debug(msg)
133
+ };
134
+ this.#perfTracker = new import_performanceTracker.PerformanceTracker({
135
+ ...perfOpts,
136
+ dedupWindowMs: option?.performanceDedupWindowMs
137
+ });
138
+ this.#guestPerfTracker = new import_performanceTracker.PerformanceTracker({
139
+ ...perfOpts,
140
+ dedupWindowMs: 0
141
+ });
119
142
  this.#correlationId = (0, import_uuid.v4)();
120
143
  this.#remoting = new import_microfe_common.Remoting(this.#logger, this.#correlationId);
121
- this.#readyStateCallback = option.readyStateCallback ?? null;
122
- this.#onGuestEventSubscribe = option.onGuestEventSubscribe ?? null;
123
- this.#onGuestEventUnsubscribe = option.onGuestEventUnsubscribe ?? null;
144
+ if (option?.readyStateCallback && typeof option?.readyStateCallback !== "function")
145
+ throw new Error("readyStateCallback must be a function");
146
+ this.#readyStateCallback = option?.readyStateCallback || null;
147
+ this.#onGuestEventSubscribe = option?.onGuestEventSubscribe || null;
148
+ this.#onGuestEventUnsubscribe = option?.onGuestEventUnsubscribe || null;
124
149
  this.#soManager = new import_microfe_common.ScriptingObjectManager();
125
150
  this.#remoting.initialize(window);
126
151
  this.#connect();
@@ -129,34 +154,17 @@ class SSFHost {
129
154
  `host is initialized. hostId: ${this.hostId}, correlationId: ${this.#correlationId}`
130
155
  );
131
156
  }
132
- #validateHostOptions(option) {
133
- if (!option?.logger) throw new Error("Logger is required");
134
- if (!option?.analyticsObj) throw new Error("Analytics object is required");
135
- if (option.readyStateCallback && typeof option.readyStateCallback !== "function") {
136
- throw new Error("readyStateCallback must be a function");
137
- }
138
- }
139
- #createPerfTrackers(option) {
140
- const perfOpts = {
141
- analyticsObj: option.analyticsObj,
142
- enabled: option.measurePerformance,
143
- samplingRatio: option.performanceSamplingRatio,
144
- onError: (msg) => this.#logger.debug(msg)
145
- };
146
- return {
147
- perfTracker: new import_performanceTracker.PerformanceTracker({
148
- ...perfOpts,
149
- dedupWindowMs: option.performanceDedupWindowMs
150
- }),
151
- guestPerfTracker: new import_performanceTracker.PerformanceTracker({
152
- ...perfOpts,
153
- dedupWindowMs: 0
154
- })
155
- };
156
- }
157
+ #sendBAEvent = (event, data) => {
158
+ const baEvent = { event, ...data };
159
+ this.#analyticsObj.sendBAEvent(baEvent).catch((e) => {
160
+ this.#logger.debug(
161
+ `Analytics sendBAEvent failed: ${e.message}`
162
+ );
163
+ });
164
+ };
157
165
  #closeAllPopupGuests = () => {
158
166
  const popupIds = Array.from(this.#guests.values()).filter((guest) => guest.openMode === import_types.OpenMode.Popup).map((guest) => guest.id);
159
- popupIds.forEach((id) => this.unloadGuest(id));
167
+ popupIds.forEach((id) => this.#unloadGuestInBackground(id));
160
168
  };
161
169
  /**
162
170
  * get the reference to the guest application by its window
@@ -175,7 +183,10 @@ class SSFHost {
175
183
  * @param value javascript object
176
184
  * @returns true if the object is a scripting object
177
185
  */
178
- #isScriptingObject = (value) => typeof value === "object" && value !== null && "_toJSON" in value && typeof value._toJSON === "function";
186
+ #isScriptingObject = (value) => (
187
+ // eslint-disable-next-line no-underscore-dangle, @typescript-eslint/no-unsafe-member-access
188
+ typeof value?._toJSON === "function"
189
+ );
179
190
  #encodeResponse = (val, guest) => {
180
191
  if (this.#isScriptingObject(val)) {
181
192
  if (!this.#soManager.has(
@@ -190,6 +201,15 @@ class SSFHost {
190
201
  }
191
202
  return { type: "value", value: val };
192
203
  };
204
+ #encodeException = (ex) => {
205
+ if (typeof ex === "string") {
206
+ return ex;
207
+ }
208
+ if (ex instanceof Error) {
209
+ return ex.message;
210
+ }
211
+ return "An unexpected error occurred in the host application";
212
+ };
193
213
  #dispatchConfigEvent = (guest) => {
194
214
  if (guest.ready) {
195
215
  this.#remoting.send({
@@ -234,7 +254,7 @@ class SSFHost {
234
254
  enumerable: true,
235
255
  writable: true
236
256
  });
237
- resolve(Reflect.apply(func, obj, functionParams));
257
+ resolve(func(...functionParams));
238
258
  });
239
259
  };
240
260
  #handleGuestReadyComplete = ({
@@ -297,6 +317,8 @@ class SSFHost {
297
317
  message: `Guest is initialized`,
298
318
  ...guest.getInfo()
299
319
  });
320
+ } else if (body) {
321
+ guest.capabilities = { ...guest.capabilities, ...body };
300
322
  }
301
323
  if (!body || !body.onReady) {
302
324
  this.#handleGuestReadyComplete({
@@ -317,7 +339,7 @@ class SSFHost {
317
339
  if (sourceWin?.window) {
318
340
  const guest = this.#findGuest(sourceWin);
319
341
  if (guest && !await guest.handShake()) {
320
- this.unloadGuest(sourceWin);
342
+ this.#unloadGuestInBackground(sourceWin);
321
343
  }
322
344
  }
323
345
  };
@@ -641,7 +663,12 @@ class SSFHost {
641
663
  * @param value
642
664
  * @returns
643
665
  */
644
- #isProxyEvent = (value) => value instanceof import_microfe_common.ProxyEvent || typeof value === "object" && value !== null && "subscribe" in value && typeof value.subscribe === "function";
666
+ #isProxyEvent = (value) => (
667
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
668
+ value instanceof import_microfe_common.ProxyEvent || // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
669
+ typeof value?.subscribe === "function"
670
+ );
671
+ // support v1 scripting objects
645
672
  /**
646
673
  * Register pre-rendered guest application to host
647
674
  * @param {AttachParam} param - parameters for attaching the guest
@@ -685,7 +712,7 @@ class SSFHost {
685
712
  });
686
713
  guestsToRemove.forEach((guest) => {
687
714
  const { id } = guest;
688
- this.unloadGuest(id);
715
+ this.#unloadGuestInBackground(id);
689
716
  const callbacks = this.#guestCloseCallbackList.get(id);
690
717
  callbacks?.forEach((callback) => {
691
718
  Promise.resolve(callback({ id })).catch(() => {
@@ -710,25 +737,6 @@ class SSFHost {
710
737
  const popupUrl = handshakeToken ? `${url}#hs=${handshakeToken}` : url;
711
738
  return { popupUrl, handshakeToken, trusted };
712
739
  };
713
- #focusExistingPopupGuest = (guest, url, title) => {
714
- if (guest.window.closed) return;
715
- if ((0, import_utils.isTrustedDomain)(url)) {
716
- window.open("", title);
717
- } else {
718
- guest.window.focus();
719
- }
720
- };
721
- #scheduleGuestLifecycleCallback = (callback, guestId, label) => {
722
- setTimeout(() => {
723
- try {
724
- callback?.(guestId);
725
- } catch (error) {
726
- this.#logger.debug(
727
- `Error occurred in ${label} for guest with id '${guestId}': ${error.message}`
728
- );
729
- }
730
- }, 0);
731
- };
732
740
  #openPopupGuest = (param) => {
733
741
  const {
734
742
  url,
@@ -747,7 +755,13 @@ class SSFHost {
747
755
  } = popupWindowFeatures;
748
756
  let guest = this.#getGuestForUrl(url);
749
757
  if (guest) {
750
- this.#focusExistingPopupGuest(guest, url, title);
758
+ if (!guest.window.closed) {
759
+ if ((0, import_utils.isTrustedDomain)(url)) {
760
+ window.open("", title);
761
+ } else {
762
+ guest.window.focus();
763
+ }
764
+ }
751
765
  } else {
752
766
  const windowFeatures = Object.entries({ width, height, top, left }).filter(([, v]) => v).map(([k, v]) => `${k}=${v}`).join(",");
753
767
  const { popupUrl, handshakeToken, trusted } = this.#buildPopupUrl(url);
@@ -757,10 +771,27 @@ class SSFHost {
757
771
  `popup, ${windowFeatures}`
758
772
  );
759
773
  if (!guestWindow) {
760
- this.#scheduleGuestLifecycleCallback(onError, guestId, "onError");
774
+ setTimeout(() => {
775
+ try {
776
+ onError?.(guestId);
777
+ } catch (error) {
778
+ this.#logger.debug(
779
+ `Error occurred in onError for guest with id '${guestId}': ${error.message}`
780
+ );
781
+ }
782
+ }, 0);
761
783
  throw new Error("Failed to open guest application in popup window");
784
+ } else {
785
+ setTimeout(() => {
786
+ try {
787
+ onLoad?.(guestId);
788
+ } catch (error) {
789
+ this.#logger.debug(
790
+ `Error occurred in onLoad for guest with id '${guestId}': ${error.message}`
791
+ );
792
+ }
793
+ }, 0);
762
794
  }
763
- this.#scheduleGuestLifecycleCallback(onLoad, guestId, "onLoad");
764
795
  if (!trusted) {
765
796
  guestWindow.opener = null;
766
797
  }
@@ -777,28 +808,53 @@ class SSFHost {
777
808
  }
778
809
  return guest;
779
810
  };
780
- #createEmbedGuestFrame = ({
781
- targetElementDocument,
782
- guestId,
783
- title,
784
- url,
785
- style,
786
- disableSandbox,
787
- sandboxValues,
788
- customSandboxValues,
789
- permissionPolicy,
790
- onLoad,
791
- onError
792
- }) => {
811
+ #openEmbedGuest = (param) => {
812
+ const {
813
+ url,
814
+ title,
815
+ targetElement,
816
+ searchParams,
817
+ guestId,
818
+ onLoad,
819
+ onError,
820
+ options = {}
821
+ } = param;
822
+ const targetElementDocument = targetElement.ownerDocument ?? document;
823
+ const {
824
+ fitToContent = false,
825
+ disableSandbox = false,
826
+ sandboxValues = [],
827
+ customSandboxValues = [],
828
+ style = "",
829
+ permissionPolicy = ""
830
+ } = options;
831
+ if (!title) throw new Error("title is required");
832
+ if (fitToContent) this.#subscribeToGuestResizeEvent();
793
833
  const frame = targetElementDocument.createElement("iframe");
794
834
  frame.setAttribute("id", guestId);
795
835
  const onFrameLoad = () => {
796
- this.#scheduleGuestLifecycleCallback(onLoad, guestId, "onLoad");
836
+ setTimeout(() => {
837
+ try {
838
+ onLoad?.(guestId);
839
+ } catch (error) {
840
+ this.#logger.debug(
841
+ `Error occurred in onLoad for guest with id '${guestId}': ${error.message}`
842
+ );
843
+ }
844
+ }, 0);
797
845
  this.#logger.debug(`frame loaded for guest with id '${guestId}'`);
798
846
  frame.removeEventListener("load", onFrameLoad);
799
847
  };
800
848
  const onFrameLoadFailure = () => {
801
- this.#scheduleGuestLifecycleCallback(onError, guestId, "onError");
849
+ setTimeout(() => {
850
+ try {
851
+ onError?.(guestId);
852
+ } catch (error) {
853
+ this.#logger.debug(
854
+ `Error occurred in onError for guest with id '${guestId}': ${error.message}`
855
+ );
856
+ }
857
+ }, 0);
802
858
  this.#logger.error(`frame load failed for guest with id '${guestId}'`);
803
859
  frame.removeEventListener("error", onFrameLoadFailure);
804
860
  };
@@ -817,41 +873,6 @@ class SSFHost {
817
873
  frame.setAttribute("title", title);
818
874
  frame.setAttribute("src", url);
819
875
  if (permissionPolicy) frame.setAttribute("allow", permissionPolicy);
820
- return frame;
821
- };
822
- #mountEmbedGuest = ({
823
- guestId,
824
- url,
825
- title,
826
- searchParams,
827
- targetElement,
828
- targetElementDocument,
829
- onLoad,
830
- onError,
831
- options
832
- }) => {
833
- const {
834
- fitToContent = false,
835
- disableSandbox = false,
836
- sandboxValues = [],
837
- customSandboxValues = [],
838
- style = "",
839
- permissionPolicy = ""
840
- } = options;
841
- if (fitToContent) this.#subscribeToGuestResizeEvent();
842
- const frame = this.#createEmbedGuestFrame({
843
- targetElementDocument,
844
- guestId,
845
- title,
846
- url,
847
- style,
848
- disableSandbox,
849
- sandboxValues,
850
- customSandboxValues,
851
- permissionPolicy,
852
- onLoad,
853
- onError
854
- });
855
876
  targetElement.appendChild(frame);
856
877
  const guestFrameEle = targetElementDocument.getElementById(
857
878
  guestId
@@ -866,31 +887,6 @@ class SSFHost {
866
887
  openMode: import_types.OpenMode.Embed
867
888
  });
868
889
  };
869
- #openEmbedGuest = (param) => {
870
- const {
871
- url,
872
- title,
873
- targetElement,
874
- searchParams,
875
- guestId,
876
- onLoad,
877
- onError,
878
- options = {}
879
- } = param;
880
- if (!title) throw new Error("title is required");
881
- const targetElementDocument = targetElement.ownerDocument ?? document;
882
- return this.#mountEmbedGuest({
883
- guestId,
884
- url,
885
- title,
886
- searchParams,
887
- targetElement,
888
- targetElementDocument,
889
- onLoad,
890
- onError,
891
- options
892
- });
893
- };
894
890
  #getGuestUrl = (url, params) => {
895
891
  let querystring = "";
896
892
  Object.keys(params).forEach((queryParam) => {
@@ -941,7 +937,9 @@ class SSFHost {
941
937
  eventOptions
942
938
  }) => this.dispatchEvent({
943
939
  event,
940
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
944
941
  eventParams,
942
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
945
943
  eventOptions
946
944
  });
947
945
  const token = propValue.subscribe(
@@ -951,18 +949,17 @@ class SSFHost {
951
949
  propValue.unsubscribe(token);
952
950
  });
953
951
  } else {
954
- const v1Proxy = propValue;
955
- const token = v1Proxy.subscribe?.(
956
- (_obj, params, options) => {
957
- void this.dispatchEvent({
958
- event,
959
- eventParams: params,
960
- eventOptions: options
961
- });
962
- }
952
+ const token = propValue.subscribe?.(
953
+ (obj, params, options) => this.dispatchEvent({
954
+ event,
955
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
956
+ eventParams: params,
957
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
958
+ eventOptions: options
959
+ })
963
960
  );
964
961
  unsubscribers.push(() => {
965
- if (token) v1Proxy.unsubscribe?.(token);
962
+ propValue.unsubscribe?.(token);
966
963
  });
967
964
  }
968
965
  } else if ((0, import_utils.isFunction)(propValue)) {
@@ -1025,30 +1022,6 @@ class SSFHost {
1025
1022
  `host is closed. hostId: ${this.hostId}, correlationId: ${this.#correlationId}`
1026
1023
  );
1027
1024
  };
1028
- #buildEventObject = ({
1029
- scriptingObject,
1030
- eventName,
1031
- eventParams,
1032
- eventHandler,
1033
- timeout
1034
- }) => {
1035
- const eventObj = {
1036
- object: scriptingObject._toJSON(),
1037
- eventName,
1038
- eventParams,
1039
- eventHandler,
1040
- eventOptions: {
1041
- allowsFeedback: false
1042
- }
1043
- };
1044
- if (timeout && !Number.isNaN(timeout)) {
1045
- eventObj.eventOptions = {
1046
- allowsFeedback: true,
1047
- timeout: Number(timeout)
1048
- };
1049
- }
1050
- return eventObj;
1051
- };
1052
1025
  #dispatchEventToMatchingGuests = ({
1053
1026
  targetGuest,
1054
1027
  guestCapabilities,
@@ -1073,6 +1046,10 @@ class SSFHost {
1073
1046
  this.#guests.forEach(tryDispatch);
1074
1047
  }
1075
1048
  };
1049
+ #resolveEventSourceObject = (objectId, targetGuest) => {
1050
+ const scriptingObject = targetGuest ? this.getScriptingObject(objectId, { guest: targetGuest }) : this.getScriptingObject(objectId);
1051
+ return scriptingObject ?? (objectId === MODULE_OBJECT_ID && targetGuest ? MODULE_EVENT_STUB : null);
1052
+ };
1076
1053
  /**
1077
1054
  * dispatch event to guest application(s)
1078
1055
  * @param {EventDetail} params details of the event to be dispatched
@@ -1094,20 +1071,32 @@ class SSFHost {
1094
1071
  const objectId = id.split(".")[0];
1095
1072
  const guestIdorWnd = guestId || targetWindow;
1096
1073
  const targetGuest = guestIdorWnd ? this.#findGuest(guestIdorWnd) : null;
1097
- const scriptingObject = targetGuest ? this.getScriptingObject(objectId, { guest: targetGuest }) : this.getScriptingObject(objectId);
1098
- if (!scriptingObject) {
1074
+ const eventSourceObject = this.#resolveEventSourceObject(
1075
+ objectId,
1076
+ targetGuest ?? null
1077
+ );
1078
+ if (!eventSourceObject) {
1099
1079
  this.#logger.warn(
1100
1080
  `Attempt to dispatch event ${name} on unknown object ${objectId}`
1101
1081
  );
1102
1082
  return Promise.resolve([]);
1103
1083
  }
1104
- const eventObj = this.#buildEventObject({
1105
- scriptingObject,
1084
+ const eventObj = {
1085
+ // eslint-disable-next-line no-underscore-dangle
1086
+ object: eventSourceObject._toJSON(),
1106
1087
  eventName: name,
1107
1088
  eventParams,
1108
1089
  eventHandler,
1109
- timeout
1110
- });
1090
+ eventOptions: {
1091
+ allowsFeedback: false
1092
+ }
1093
+ };
1094
+ if (timeout && !Number.isNaN(timeout)) {
1095
+ eventObj.eventOptions = {
1096
+ allowsFeedback: true,
1097
+ timeout: Number(timeout)
1098
+ };
1099
+ }
1111
1100
  const guestPromises = [];
1112
1101
  let eventTimingToken;
1113
1102
  const dispatchToGuest = (guest) => {
@@ -1116,7 +1105,7 @@ class SSFHost {
1116
1105
  guestPromises.push(guest.dispatchEvent(eventObj, timeout));
1117
1106
  if (!eventTimingToken && this.#perfTracker.enabled) {
1118
1107
  eventTimingToken = this.#perfTracker.start(
1119
- `ScriptingObject.Event.${scriptingObject.id}.${name}`,
1108
+ `ScriptingObject.Event.${eventSourceObject.id}.${name}`,
1120
1109
  { appId: this.hostId, appUrl: window.location.href }
1121
1110
  );
1122
1111
  }
@@ -1177,14 +1166,6 @@ class SSFHost {
1177
1166
  * @returns scripting object
1178
1167
  */
1179
1168
  getScriptingObject = (objectId, params) => this.#soManager.getObject(objectId, params?.guest);
1180
- #resolveUniqueGuestInstanceId = (guestId) => {
1181
- if (!this.#guests.has(guestId)) return guestId;
1182
- let counter = 1;
1183
- while (this.#guests.has(`${guestId}-${counter}`)) {
1184
- counter += 1;
1185
- }
1186
- return `${guestId}-${counter}`;
1187
- };
1188
1169
  /**
1189
1170
  * loads guest application inside sandboxed iframe
1190
1171
  * @param {LoadGuestParam} param - parameters for loading the guest application
@@ -1203,14 +1184,21 @@ class SSFHost {
1203
1184
  metadata
1204
1185
  } = param;
1205
1186
  if (!guestId) throw new Error("id for guest application is required");
1206
- const instanceId = this.#resolveUniqueGuestInstanceId(guestId);
1187
+ let instanceId = guestId;
1188
+ if (this.#guests.has(instanceId)) {
1189
+ let counter = 1;
1190
+ while (this.#guests.has(`${guestId}-${counter}`)) {
1191
+ counter += 1;
1192
+ }
1193
+ instanceId = `${guestId}-${counter}`;
1194
+ }
1207
1195
  const { openMode = import_types.OpenMode.Embed, popupWindowFeatures = {} } = options;
1208
1196
  const srcUrl = this.#getGuestUrl(url, searchParams);
1197
+ let guest = null;
1209
1198
  this.#perfTracker.start("SSF.Guest.Load", {
1210
1199
  appId: instanceId,
1211
1200
  appUrl: srcUrl
1212
1201
  });
1213
- let guest;
1214
1202
  if (openMode === import_types.OpenMode.Popup) {
1215
1203
  guest = this.#openPopupGuest({
1216
1204
  guestId: instanceId,
@@ -1290,25 +1278,80 @@ class SSFHost {
1290
1278
  this.#guests.forEach(this.#dispatchConfigEvent);
1291
1279
  this.#logger.debug("Dispatched config events to all guests");
1292
1280
  };
1281
+ #dispatchGuestModuleEvent = async (guest, eventId, eventName) => (
1282
+ /* eslint-disable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */
1283
+ this.dispatchEvent({
1284
+ event: { id: eventId, name: eventName },
1285
+ eventParams: { moduleId: guest.id },
1286
+ eventOptions: {
1287
+ timeout: MODULE_EVENT_TIMEOUT_MS,
1288
+ guestId: guest.id
1289
+ }
1290
+ })
1291
+ );
1292
+ /* eslint-enable @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */
1293
+ #canUnloadGuest = async (guest) => {
1294
+ try {
1295
+ const feedback = await this.#dispatchGuestModuleEvent(
1296
+ guest,
1297
+ MODULE_UNLOADING_EVENT,
1298
+ "unloading"
1299
+ );
1300
+ if (isModuleUnloadVetoed(feedback)) {
1301
+ this.#logger.info(
1302
+ `Unload denied by guest ${guest.id} via module.unloading event`
1303
+ );
1304
+ return false;
1305
+ }
1306
+ } catch (err) {
1307
+ this.#logger.warn({
1308
+ message: `Error dispatching module.unloading event for ${guest.id}; proceeding with unload`,
1309
+ exception: err
1310
+ });
1311
+ }
1312
+ return true;
1313
+ };
1314
+ /**
1315
+ * Signal SDK teardown after the module.unloading veto phase passes.
1316
+ * @param guest
1317
+ */
1318
+ #dispatchModuleUnmount = async (guest) => {
1319
+ try {
1320
+ await this.#dispatchGuestModuleEvent(
1321
+ guest,
1322
+ MODULE_UNMOUNT_EVENT,
1323
+ "unmount"
1324
+ );
1325
+ } catch (err) {
1326
+ this.#logger.warn({
1327
+ message: `Error dispatching module.unmount event for ${guest.id}`,
1328
+ exception: err
1329
+ });
1330
+ }
1331
+ };
1332
+ #unloadGuestInBackground = (guestIdOrWindowOrEle) => {
1333
+ this.unloadGuest(guestIdOrWindowOrEle).catch(ignoreRejectedPromise);
1334
+ };
1293
1335
  /**
1294
1336
  * unloads guest application from host
1295
1337
  * @param guestIdOrWindowOrEle unique id of the guest application or window object or dom element of the guest application
1296
1338
  * @throws Error if guestId or Window reference is invalid
1297
1339
  */
1298
- unloadGuest = (guestIdOrWindowOrEle) => {
1340
+ unloadGuest = async (guestIdOrWindowOrEle) => {
1299
1341
  const guest = this.#findGuest(guestIdOrWindowOrEle);
1300
- if (guest) {
1301
- guest.dispose();
1302
- this.#guestsByWindow.delete(guest.window);
1303
- this.#guestsByUrl.delete(guest.url);
1304
- this.#guestMetadata.delete(guest.id);
1305
- this.#guests.delete(guest.id);
1306
- this.#logger.audit({
1307
- message: `Guest is removed from host`,
1308
- ...guest.getInfo()
1309
- });
1310
- this.#stopMonitoringPopupGuests();
1311
- }
1342
+ if (!guest) return;
1343
+ if (!await this.#canUnloadGuest(guest)) return;
1344
+ await this.#dispatchModuleUnmount(guest);
1345
+ guest.dispose();
1346
+ this.#guestsByWindow.delete(guest.window);
1347
+ this.#guestsByUrl.delete(guest.url);
1348
+ this.#guestMetadata.delete(guest.id);
1349
+ this.#guests.delete(guest.id);
1350
+ this.#logger.audit({
1351
+ message: `Guest is removed from host`,
1352
+ ...guest.getInfo()
1353
+ });
1354
+ this.#stopMonitoringPopupGuests();
1312
1355
  };
1313
1356
  /**
1314
1357
  * monitors guest window close action and executes the callback from host.