@encatch/event-publisher 1.0.0-beta.1 → 1.0.0-beta.2
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/event-publisher.js
CHANGED
|
@@ -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:show","form:started","form:submit","form:close","form:section:change","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: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
|
@@ -27,7 +27,7 @@ export declare type FormEventPayload = {
|
|
|
27
27
|
sectionId: string;
|
|
28
28
|
timestamp: number;
|
|
29
29
|
};
|
|
30
|
-
'form:
|
|
30
|
+
'form:answered': {
|
|
31
31
|
feedbackConfigurationId: string;
|
|
32
32
|
questionId: string;
|
|
33
33
|
questionType: string;
|
|
@@ -84,7 +84,7 @@ declare class FormEventPublisher extends EventTarget {
|
|
|
84
84
|
|
|
85
85
|
export declare const formEventPublisher: FormEventPublisher;
|
|
86
86
|
|
|
87
|
-
export declare type FormEventType = 'form:show' | 'form:started' | 'form:submit' | 'form:close' | 'form:section:change' | 'form:
|
|
87
|
+
export declare type FormEventType = 'form:show' | 'form:started' | 'form:submit' | 'form:close' | 'form:section:change' | 'form:answered' | 'form:error';
|
|
88
88
|
|
|
89
89
|
export declare type FormIdFilter = string | string[] | ((formId: string) => boolean) | '*' | null | undefined;
|
|
90
90
|
|