@encatch/event-publisher 0.0.2 → 1.0.0-beta.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/README.md +1 -1
- package/dist/event-publisher.js +26 -25
- package/dist/event-publisher.umd.cjs +1 -1
- package/dist/index.d.ts +8 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ unsubscribe();
|
|
|
48
48
|
|
|
49
49
|
### Types
|
|
50
50
|
|
|
51
|
-
- `FormEventType`: Event types ('form:
|
|
51
|
+
- `FormEventType`: Event types ('form:show', 'form:submit', 'form:close', etc.)
|
|
52
52
|
- `FormEventPayload`: Payload types for each event
|
|
53
53
|
- `FormIdFilter`: Filter for specific form IDs
|
|
54
54
|
- `SubscriptionOptions`: Options for subscriptions
|
package/dist/event-publisher.js
CHANGED
|
@@ -4,8 +4,8 @@ class d extends EventTarget {
|
|
|
4
4
|
/**
|
|
5
5
|
* Check if a form ID matches the filter
|
|
6
6
|
*/
|
|
7
|
-
matchesFilter(s,
|
|
8
|
-
return !
|
|
7
|
+
matchesFilter(s, e) {
|
|
8
|
+
return !e || e === "*" || e === null || e === void 0 ? !0 : typeof e == "string" ? s === e : Array.isArray(e) ? e.includes(s || "") : typeof e == "function" ? e(s || "") : !1;
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* Get form ID from payload
|
|
@@ -16,29 +16,29 @@ class d extends EventTarget {
|
|
|
16
16
|
/**
|
|
17
17
|
* Publish an event to all matching subscribers
|
|
18
18
|
*/
|
|
19
|
-
publish(s,
|
|
19
|
+
publish(s, e, i) {
|
|
20
20
|
typeof window < "u" && window.dispatchEvent(
|
|
21
21
|
new CustomEvent(s, {
|
|
22
|
-
detail:
|
|
22
|
+
detail: e,
|
|
23
23
|
bubbles: !0
|
|
24
24
|
})
|
|
25
|
-
),
|
|
25
|
+
), i?.usePostMessage !== !1 && this.sendPostMessage(s, e);
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
28
|
* Subscribe to form events with optional form ID filtering
|
|
29
29
|
*/
|
|
30
|
-
subscribe(s,
|
|
30
|
+
subscribe(s, e, i) {
|
|
31
31
|
const n = Symbol("subscription"), t = {
|
|
32
32
|
eventType: s,
|
|
33
|
-
handler:
|
|
34
|
-
filter:
|
|
33
|
+
handler: e,
|
|
34
|
+
filter: i?.formId,
|
|
35
35
|
id: n,
|
|
36
|
-
once:
|
|
36
|
+
once: i?.once
|
|
37
37
|
};
|
|
38
38
|
this.subscriptions.has(s) || this.subscriptions.set(s, []), this.subscriptions.get(s).push(t);
|
|
39
39
|
const r = (o) => {
|
|
40
40
|
const c = o, u = this.getFormIdFromPayload(c.detail);
|
|
41
|
-
this.matchesFilter(u,
|
|
41
|
+
this.matchesFilter(u, i?.formId) && (e(c.detail), i?.once && typeof window < "u" && window.removeEventListener(s, r));
|
|
42
42
|
};
|
|
43
43
|
return typeof window < "u" && window.addEventListener(s, r), () => {
|
|
44
44
|
this.unsubscribe(n), typeof window < "u" && window.removeEventListener(s, r);
|
|
@@ -48,10 +48,10 @@ class d extends EventTarget {
|
|
|
48
48
|
* Unsubscribe by subscription ID
|
|
49
49
|
*/
|
|
50
50
|
unsubscribe(s) {
|
|
51
|
-
for (const [
|
|
52
|
-
const n =
|
|
51
|
+
for (const [e, i] of this.subscriptions.entries()) {
|
|
52
|
+
const n = i.findIndex((t) => t.id === s);
|
|
53
53
|
if (n !== -1) {
|
|
54
|
-
|
|
54
|
+
i.splice(n, 1), i.length === 0 && this.subscriptions.delete(e);
|
|
55
55
|
break;
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -59,20 +59,21 @@ class d extends EventTarget {
|
|
|
59
59
|
/**
|
|
60
60
|
* Subscribe to all form events with optional filtering
|
|
61
61
|
*/
|
|
62
|
-
subscribeAll(s,
|
|
63
|
-
const
|
|
64
|
-
"form:
|
|
62
|
+
subscribeAll(s, e) {
|
|
63
|
+
const i = [
|
|
64
|
+
"form:show",
|
|
65
|
+
"form:started",
|
|
65
66
|
"form:submit",
|
|
66
67
|
"form:close",
|
|
67
68
|
"form:section:change",
|
|
68
69
|
"form:question:answered",
|
|
69
70
|
"form:error"
|
|
70
71
|
], n = [];
|
|
71
|
-
return
|
|
72
|
+
return i.forEach((t) => {
|
|
72
73
|
const r = this.subscribe(
|
|
73
74
|
t,
|
|
74
75
|
(o) => s(t, o),
|
|
75
|
-
|
|
76
|
+
e
|
|
76
77
|
);
|
|
77
78
|
n.push(r);
|
|
78
79
|
}), () => {
|
|
@@ -83,7 +84,7 @@ class d extends EventTarget {
|
|
|
83
84
|
* Get active subscriptions count (for debugging)
|
|
84
85
|
*/
|
|
85
86
|
getSubscriptionCount(s) {
|
|
86
|
-
return s ? this.subscriptions.get(s)?.length || 0 : Array.from(this.subscriptions.values()).reduce((
|
|
87
|
+
return s ? this.subscriptions.get(s)?.length || 0 : Array.from(this.subscriptions.values()).reduce((e, i) => e + i.length, 0);
|
|
87
88
|
}
|
|
88
89
|
/**
|
|
89
90
|
* Clear all subscriptions for an event type (or all events)
|
|
@@ -91,17 +92,17 @@ class d extends EventTarget {
|
|
|
91
92
|
clearSubscriptions(s) {
|
|
92
93
|
s ? this.subscriptions.delete(s) : this.subscriptions.clear();
|
|
93
94
|
}
|
|
94
|
-
sendPostMessage(s,
|
|
95
|
-
const
|
|
95
|
+
sendPostMessage(s, e) {
|
|
96
|
+
const i = {
|
|
96
97
|
type: `encatch:${s}`,
|
|
97
|
-
payload:
|
|
98
|
+
payload: e,
|
|
98
99
|
source: "encatch-form-engine"
|
|
99
100
|
};
|
|
100
|
-
window.parent && window.parent !== window && window.parent.postMessage(
|
|
101
|
-
t.contentWindow && t.contentWindow.postMessage(
|
|
101
|
+
window.parent && window.parent !== window && window.parent.postMessage(i, this.targetOrigin), document.querySelectorAll("iframe").forEach((t) => {
|
|
102
|
+
t.contentWindow && t.contentWindow.postMessage(i, this.targetOrigin);
|
|
102
103
|
}), typeof window.ReactNativeWebView < "u" && window.ReactNativeWebView.postMessage(JSON.stringify({
|
|
103
104
|
action: s.replace("form:", ""),
|
|
104
|
-
data: JSON.stringify(
|
|
105
|
+
data: JSON.stringify(e)
|
|
105
106
|
}));
|
|
106
107
|
}
|
|
107
108
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(o,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis<"u"?globalThis:o||self,r(o.EventPublisher={}))})(this,(function(o){"use strict";class r extends EventTarget{subscriptions=new Map;targetOrigin="*";matchesFilter(e,s){return!s||s==="*"||s===null||s===void 0?!0:typeof s=="string"?e===s:Array.isArray(s)?s.includes(e||""):typeof s=="function"?s(e||""):!1}getFormIdFromPayload(e){return e?.feedbackConfigurationId}publish(e,s,i){typeof window<"u"&&window.dispatchEvent(new CustomEvent(e,{detail:s,bubbles:!0})),i?.usePostMessage!==!1&&this.sendPostMessage(e,s)}subscribe(e,s,i){const n=Symbol("subscription"),t={eventType:e,handler:s,filter:i?.formId,id:n,once:i?.once};this.subscriptions.has(e)||this.subscriptions.set(e,[]),this.subscriptions.get(e).push(t);const u=c=>{const d=c,a=this.getFormIdFromPayload(d.detail);this.matchesFilter(a,i?.formId)&&(s(d.detail),i?.once&&typeof window<"u"&&window.removeEventListener(e,u))};return typeof window<"u"&&window.addEventListener(e,u),()=>{this.unsubscribe(n),typeof window<"u"&&window.removeEventListener(e,u)}}unsubscribe(e){for(const[s,i]of this.subscriptions.entries()){const n=i.findIndex(t=>t.id===e);if(n!==-1){i.splice(n,1),i.length===0&&this.subscriptions.delete(s);break}}}subscribeAll(e,s){const i=["form:
|
|
1
|
+
(function(o,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(o=typeof globalThis<"u"?globalThis:o||self,r(o.EventPublisher={}))})(this,(function(o){"use strict";class r extends EventTarget{subscriptions=new Map;targetOrigin="*";matchesFilter(e,s){return!s||s==="*"||s===null||s===void 0?!0:typeof s=="string"?e===s:Array.isArray(s)?s.includes(e||""):typeof s=="function"?s(e||""):!1}getFormIdFromPayload(e){return e?.feedbackConfigurationId}publish(e,s,i){typeof window<"u"&&window.dispatchEvent(new CustomEvent(e,{detail:s,bubbles:!0})),i?.usePostMessage!==!1&&this.sendPostMessage(e,s)}subscribe(e,s,i){const n=Symbol("subscription"),t={eventType:e,handler:s,filter:i?.formId,id:n,once:i?.once};this.subscriptions.has(e)||this.subscriptions.set(e,[]),this.subscriptions.get(e).push(t);const u=c=>{const d=c,a=this.getFormIdFromPayload(d.detail);this.matchesFilter(a,i?.formId)&&(s(d.detail),i?.once&&typeof window<"u"&&window.removeEventListener(e,u))};return typeof window<"u"&&window.addEventListener(e,u),()=>{this.unsubscribe(n),typeof window<"u"&&window.removeEventListener(e,u)}}unsubscribe(e){for(const[s,i]of this.subscriptions.entries()){const n=i.findIndex(t=>t.id===e);if(n!==-1){i.splice(n,1),i.length===0&&this.subscriptions.delete(s);break}}}subscribeAll(e,s){const i=["form:show","form:started","form:submit","form:close","form:section:change","form:question:answered","form:error"],n=[];return i.forEach(t=>{const u=this.subscribe(t,c=>e(t,c),s);n.push(u)}),()=>{n.forEach(t=>t())}}getSubscriptionCount(e){return e?this.subscriptions.get(e)?.length||0:Array.from(this.subscriptions.values()).reduce((s,i)=>s+i.length,0)}clearSubscriptions(e){e?this.subscriptions.delete(e):this.subscriptions.clear()}sendPostMessage(e,s){const i={type:`encatch:${e}`,payload:s,source:"encatch-form-engine"};window.parent&&window.parent!==window&&window.parent.postMessage(i,this.targetOrigin),document.querySelectorAll("iframe").forEach(t=>{t.contentWindow&&t.contentWindow.postMessage(i,this.targetOrigin)}),typeof window.ReactNativeWebView<"u"&&window.ReactNativeWebView.postMessage(JSON.stringify({action:e.replace("form:",""),data:JSON.stringify(s)}))}}const f=new r;o.formEventPublisher=f,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
export declare type FormEventPayload = {
|
|
2
|
-
'form:
|
|
2
|
+
'form:show': {
|
|
3
|
+
feedbackConfigurationId: string;
|
|
4
|
+
feedbackIdentifier?: string;
|
|
5
|
+
timestamp: number;
|
|
6
|
+
};
|
|
7
|
+
'form:started': {
|
|
3
8
|
feedbackConfigurationId: string;
|
|
4
9
|
feedbackIdentifier?: string;
|
|
5
10
|
timestamp: number;
|
|
@@ -8,6 +13,7 @@ export declare type FormEventPayload = {
|
|
|
8
13
|
feedbackConfigurationId: string;
|
|
9
14
|
feedbackIdentifier?: string;
|
|
10
15
|
response: any;
|
|
16
|
+
isPartialSubmit?: boolean;
|
|
11
17
|
timestamp: number;
|
|
12
18
|
};
|
|
13
19
|
'form:close': {
|
|
@@ -78,7 +84,7 @@ declare class FormEventPublisher extends EventTarget {
|
|
|
78
84
|
|
|
79
85
|
export declare const formEventPublisher: FormEventPublisher;
|
|
80
86
|
|
|
81
|
-
export declare type FormEventType = 'form:
|
|
87
|
+
export declare type FormEventType = 'form:show' | 'form:started' | 'form:submit' | 'form:close' | 'form:section:change' | 'form:question:answered' | 'form:error';
|
|
82
88
|
|
|
83
89
|
export declare type FormIdFilter = string | string[] | ((formId: string) => boolean) | '*' | null | undefined;
|
|
84
90
|
|