@marianmeres/widget-provider 1.0.2 → 1.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.
package/dist/mod.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { provideWidget, resolveAllowedOrigins, isOriginAllowed, resolveAnimateConfig, } from "./widget-provider.js";
2
- export type { WidgetProviderOptions, WidgetProviderApi, WidgetState, WidgetMessage, StylePreset, StyleOverrides, AnimatePreset, MessageHandler, Unsubscribe, } from "./types.js";
1
+ export { isOriginAllowed, provideWidget, resolveAllowedOrigins, resolveAnimateConfig, } from "./widget-provider.js";
2
+ export type { AnimatePreset, MessageHandler, StyleOverrides, StylePreset, Unsubscribe, WidgetMessage, WidgetProviderApi, WidgetProviderOptions, WidgetState, } from "./types.js";
3
3
  export { MSG_PREFIX } from "./types.js";
4
- export { STYLE_PRESETS, IFRAME_BASE, ANIMATE_PRESETS, type AnimateConfig, } from "./style-presets.js";
4
+ export { ANIMATE_PRESETS, type AnimateConfig, IFRAME_BASE, STYLE_PRESETS, } from "./style-presets.js";
package/dist/mod.js CHANGED
@@ -1,3 +1,3 @@
1
- export { provideWidget, resolveAllowedOrigins, isOriginAllowed, resolveAnimateConfig, } from "./widget-provider.js";
1
+ export { isOriginAllowed, provideWidget, resolveAllowedOrigins, resolveAnimateConfig, } from "./widget-provider.js";
2
2
  export { MSG_PREFIX } from "./types.js";
3
- export { STYLE_PRESETS, IFRAME_BASE, ANIMATE_PRESETS, } from "./style-presets.js";
3
+ export { ANIMATE_PRESETS, IFRAME_BASE, STYLE_PRESETS, } from "./style-presets.js";
@@ -1,6 +1,9 @@
1
1
  import { createStore } from "@marianmeres/store";
2
2
  import { ANIMATE_PRESETS, applyIframeBaseStyles, applyPreset, STYLE_PRESETS, TRIGGER_BASE, } from "./style-presets.js";
3
3
  import { MSG_PREFIX, } from "./types.js";
4
+ import { createClog } from "@marianmeres/clog";
5
+ import { createPubSub } from "@marianmeres/pubsub";
6
+ const clog = createClog("widget-provider");
4
7
  const DEFAULT_TRIGGER_ICON = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>`;
5
8
  /**
6
9
  * Resolve the list of allowed origins for postMessage validation.
@@ -83,7 +86,11 @@ export function provideWidget(options) {
83
86
  }
84
87
  }
85
88
  // messaging
86
- const messageHandlers = new Map();
89
+ const pubsub = createPubSub({
90
+ onError: (error) => {
91
+ clog.warn("message handler error:", error);
92
+ },
93
+ });
87
94
  function handleMessage(event) {
88
95
  if (!isOriginAllowed(event.origin, origins))
89
96
  return;
@@ -125,17 +132,7 @@ export function provideWidget(options) {
125
132
  exitNativeFullscreen();
126
133
  break;
127
134
  }
128
- const handlers = messageHandlers.get(data.type);
129
- if (handlers) {
130
- for (const h of handlers) {
131
- try {
132
- h(data.payload);
133
- }
134
- catch (e) {
135
- console.warn("[widget-provider] message handler error:", e);
136
- }
137
- }
138
- }
135
+ pubsub.publish(data.type, data.payload);
139
136
  }
140
137
  globalThis.addEventListener("message", handleMessage);
141
138
  // append to DOM
@@ -243,7 +240,7 @@ export function provideWidget(options) {
243
240
  if (state.get().destroyed)
244
241
  return;
245
242
  globalThis.removeEventListener("message", handleMessage);
246
- messageHandlers.clear();
243
+ pubsub.unsubscribeAll();
247
244
  iframe.src = "about:blank";
248
245
  container.remove();
249
246
  triggerEl?.remove();
@@ -261,13 +258,7 @@ export function provideWidget(options) {
261
258
  }
262
259
  function onMessage(type, handler) {
263
260
  const prefixedType = `${MSG_PREFIX}${type}`;
264
- if (!messageHandlers.has(prefixedType)) {
265
- messageHandlers.set(prefixedType, new Set());
266
- }
267
- messageHandlers.get(prefixedType).add(handler);
268
- return () => {
269
- messageHandlers.get(prefixedType)?.delete(handler);
270
- };
261
+ return pubsub.subscribe(prefixedType, handler);
271
262
  }
272
263
  return {
273
264
  show,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marianmeres/widget-provider",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "main": "dist/mod.js",
6
6
  "types": "dist/mod.d.ts",
@@ -13,6 +13,8 @@
13
13
  "author": "Marian Meres",
14
14
  "license": "MIT",
15
15
  "dependencies": {
16
+ "@marianmeres/clog": "^3.15.2",
17
+ "@marianmeres/pubsub": "^2.4.6",
16
18
  "@marianmeres/store": "^2.4.4"
17
19
  },
18
20
  "repository": {