@capillarytech/cap-ui-utils 3.0.14 → 3.0.15-alpha.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.
package/index.js CHANGED
@@ -12,3 +12,4 @@ export { default as sanitizeTemplateWithRegexp } from "./utils/contentSanitizati
12
12
  export { default as decompressJsonObject } from "./utils/zlibDataDecompress";
13
13
  export { default as getEnvVariable } from "./utils/getEnvVariables";
14
14
  export { formatDateWithTimezone, getTimezoneTooltip, hasTimezoneFeatureAccess } from "./utils/timezone";
15
+ export { default as MFEEventBus } from "./utils/mfeEventBus";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capillarytech/cap-ui-utils",
3
- "version": "3.0.14",
3
+ "version": "3.0.15-alpha.1",
4
4
  "description": "Utility functions shared accross all the modules",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,47 @@
1
+ /**
2
+ * MFEEventBus
3
+ *
4
+ * Lightweight event bus for cross-boundary communication between the MFE
5
+ * host shell and remote apps. Uses window CustomEvent — no build-time
6
+ * coupling between apps.
7
+ *
8
+ * All events are namespaced with the MFE_EVENT_PREFIX to avoid collisions
9
+ * with other window events (e.g. analytics, third-party libraries).
10
+ */
11
+
12
+ const MFE_EVENT_PREFIX = 'mfe:';
13
+
14
+ const MFEEventBus = {
15
+ /**
16
+ * Emit an event to all listeners (host or any remote).
17
+ *
18
+ * @param {string} eventName - Event name without prefix (e.g. 'loyalty:navigate')
19
+ * @param {object} payload - Arbitrary serialisable data
20
+ */
21
+ emit(eventName, payload = {}) {
22
+ window.dispatchEvent(
23
+ new CustomEvent(`${MFE_EVENT_PREFIX}${eventName}`, {
24
+ detail: payload,
25
+ bubbles: false,
26
+ cancelable: false,
27
+ }),
28
+ );
29
+ },
30
+
31
+ /**
32
+ * Subscribe to an event. Returns an unsubscribe function — call it in
33
+ * useEffect cleanup to prevent memory leaks.
34
+ *
35
+ * @param {string} eventName - Event name without prefix
36
+ * @param {function} handler - Called with the event payload object
37
+ * @returns {function} - Unsubscribe / cleanup function
38
+ */
39
+ on(eventName, handler) {
40
+ const listener = event => handler(event.detail);
41
+ window.addEventListener(`${MFE_EVENT_PREFIX}${eventName}`, listener);
42
+ return () =>
43
+ window.removeEventListener(`${MFE_EVENT_PREFIX}${eventName}`, listener);
44
+ },
45
+ };
46
+
47
+ export default MFEEventBus;