@asaidimu/utils-events 1.2.1 → 1.2.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/index.d.mts CHANGED
@@ -76,18 +76,14 @@ interface EventBus<TEventMap extends Record<string, any>> {
76
76
  * subscription
77
77
  * @returns A function to unsubscribe from the event.
78
78
  */
79
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
80
- __event__: keyof TEventMap;
81
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
79
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
82
80
  /**
83
81
  * Subscribes to an event and automatically unsubscribes after it fires once.
84
82
  * @param eventName - The name of the event to subscribe to.
85
83
  * @param callback - The function to call when the event is emitted.
86
84
  * @returns A function to cancel the one-shot subscription before it fires.
87
85
  */
88
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
89
- __event__: keyof TEventMap;
90
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
86
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
91
87
  /**
92
88
  * Emits an event with a payload to all subscribed listeners.
93
89
  * @param event - An object containing the event name and payload.
@@ -168,18 +164,14 @@ declare class Events<TEventMap extends Record<string, any>> implements EventBus<
168
164
  * @param callback - The function to call when the event is emitted.
169
165
  * @returns A function to unsubscribe from the event.
170
166
  */
171
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
172
- __event__: keyof TEventMap;
173
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
167
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
174
168
  /**
175
169
  * Subscribes to an event and automatically unsubscribes after it fires once.
176
170
  * @param eventName - The name of the event to subscribe to.
177
171
  * @param callback - The function to call when the event is emitted.
178
172
  * @returns A function to cancel the one-shot subscription before it fires.
179
173
  */
180
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
181
- __event__: keyof TEventMap;
182
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
174
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
183
175
  /**
184
176
  * Emits an event with a payload to all subscribed listeners.
185
177
  * @param event - An object containing the event name and payload.
package/index.d.ts CHANGED
@@ -76,18 +76,14 @@ interface EventBus<TEventMap extends Record<string, any>> {
76
76
  * subscription
77
77
  * @returns A function to unsubscribe from the event.
78
78
  */
79
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
80
- __event__: keyof TEventMap;
81
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
79
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
82
80
  /**
83
81
  * Subscribes to an event and automatically unsubscribes after it fires once.
84
82
  * @param eventName - The name of the event to subscribe to.
85
83
  * @param callback - The function to call when the event is emitted.
86
84
  * @returns A function to cancel the one-shot subscription before it fires.
87
85
  */
88
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
89
- __event__: keyof TEventMap;
90
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
86
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
91
87
  /**
92
88
  * Emits an event with a payload to all subscribed listeners.
93
89
  * @param event - An object containing the event name and payload.
@@ -168,18 +164,14 @@ declare class Events<TEventMap extends Record<string, any>> implements EventBus<
168
164
  * @param callback - The function to call when the event is emitted.
169
165
  * @returns A function to unsubscribe from the event.
170
166
  */
171
- subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
172
- __event__: keyof TEventMap;
173
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
167
+ subscribe<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
174
168
  /**
175
169
  * Subscribes to an event and automatically unsubscribes after it fires once.
176
170
  * @param eventName - The name of the event to subscribe to.
177
171
  * @param callback - The function to call when the event is emitted.
178
172
  * @returns A function to cancel the one-shot subscription before it fires.
179
173
  */
180
- once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap] & {
181
- __event__: keyof TEventMap;
182
- }) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
174
+ once<TEventName extends keyof TEventMap | "*">(eventName: TEventName, callback: TEventName extends "*" ? (payload: TEventMap[keyof TEventMap], event: keyof TEventMap) => void : (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions): () => void;
183
175
  /**
184
176
  * Emits an event with a payload to all subscribed listeners.
185
177
  * @param event - An object containing the event name and payload.
package/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(t=>{this._enqueue(e,t)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let n;this._pendingFn=void 0;try{n={status:"ok",value:await e()}}catch(e){n={status:"error",error:e}}for(const e of t)e(n);return n}};function t(t){const n=t??{},s=n.errorHandler??(e=>console.error("EventBus Error:",e)),r=null!=n.batch?.size,i=n.batch?.size,a=n.batch?.delay??16;if(r&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(r&&(a<0||!Number.isFinite(a)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${a}.`);const o=n.broadcast?.channel,c=null!=o&&o.length>0,l=new Map,u=new Map;let d=[];const h=r?new e({delay:a}):null;let f=0,m=0;const _=new Map,g=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(o);return e.onmessage=e=>{const{name:t,payload:n}=e.data;v(t,n)},e};let p=g();const b=(e,t)=>{f++,m+=t,_.set(e,(_.get(e)??0)+1)},v=(e,t)=>{const n=l.get(e);if(n&&n.size>0)for(const{callback:r}of Array.from(n.values()))try{r(t)}catch(n){const r=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),i=Object.assign(r,{eventName:e,payload:t});s(i)}if(u.size>0)for(const{callback:n}of Array.from(u.values()))try{n({...t,__event__:e})}catch(e){const n=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),r=Object.assign(n,{eventName:"*",payload:t});s(r)}},y=(t,n,s)=>{let r,i;void 0!==s&&s>0?(i=new e({delay:s,leading:!1}),r=e=>{i.fire(()=>{n(e)})}):r=n;const a={callback:r,debouncer:i};if("*"===t)return u.set(n,a),()=>{const e=u.get(n);e&&(e.debouncer?.cancel(),u.delete(n))};{l.has(t)||l.set(t,new Map);const e=l.get(t);return e.set(n,a),()=>{const s=e.get(n);s&&(s.debouncer?.cancel(),e.delete(n),0===e.size&&l.delete(t))}}},w=()=>{const e=d;d=[];for(const{name:t,payload:n}of e){const e=performance.now();v(t,n),b(t,performance.now()-e)}};return{subscribe:(e,t,n)=>y(e,t,n?.debounce),once(e,t,n){let s;return s=y(e,e=>{s(),t(e)},n?.debounce),s},emit:({name:e,payload:t})=>{if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(r)return d.push({name:e,payload:t}),d.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void p?.postMessage({name:e,payload:t}));const n=performance.now();v(e,t),b(e,performance.now()-n),p?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,t)=>e+t.size,0)+u.size,eventCounts:new Map(_),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),d=[];for(const e of l.values())for(const{debouncer:t}of e.values())t?.cancel();l.clear();for(const{debouncer:e}of u.values())e?.cancel();u.clear(),f=0,m=0,_.clear(),p?.close(),p=e?.permanent?null:g()}}}exports.Events=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t,n){return this.bus.subscribe(e,t,n)}once(e,t,n){return this.bus.once(e,t,n)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}},exports.createEventBus=t;
1
+ "use strict";var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(t=>{this._enqueue(e,t)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,t){if(this._pendingFn=e,t&&this._pendingResolvers.push(t),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const t of e)t({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,t=this._pendingResolvers.splice(0);let n;this._pendingFn=void 0;try{n={status:"ok",value:await e()}}catch(e){n={status:"error",error:e}}for(const e of t)e(n);return n}};function t(t){const n=t??{},s=n.errorHandler??(e=>console.error("EventBus Error:",e)),r=null!=n.batch?.size,i=n.batch?.size,o=n.batch?.delay??16;if(r&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(r&&(o<0||!Number.isFinite(o)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);const a=n.broadcast?.channel,c=null!=a&&a.length>0,l=new Map,u=new Map;let d=[];const h=r?new e({delay:o}):null;let f=0,m=0;const g=new Map,p=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(a);return e.onmessage=e=>{const{name:t,payload:n}=e.data;v(t,n)},e};let b=p();const _=(e,t)=>{f++,m+=t,g.set(e,(g.get(e)??0)+1)},v=(e,t)=>{const n=l.get(e);if(n&&n.size>0)for(const{callback:r}of Array.from(n.values()))try{r(t)}catch(n){const r=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),i=Object.assign(r,{eventName:e,payload:t});s(i)}if(u.size>0)for(const{callback:n}of Array.from(u.values()))try{n(t&&"object"==typeof t?{...t}:t,e)}catch(e){const n=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),r=Object.assign(n,{eventName:"*",payload:t});s(r)}},y=(t,n,s)=>{let r,i;void 0!==s&&s>0?(i=new e({delay:s,leading:!1}),r=(e,t)=>{i.fire(()=>{void 0!==t?n(e,t):n(e)})}):r=n;const o={callback:r,debouncer:i};if("*"===t)return u.set(n,o),()=>{const e=u.get(n);e&&(e.debouncer?.cancel(),u.delete(n))};{l.has(t)||l.set(t,new Map);const e=l.get(t);return e.set(n,o),()=>{const s=e.get(n);s&&(s.debouncer?.cancel(),e.delete(n),0===e.size&&l.delete(t))}}},w=()=>{const e=d;d=[];for(const{name:t,payload:n}of e){const e=performance.now();v(t,n),_(t,performance.now()-e)}};return{subscribe:(e,t,n)=>y(e,t,n?.debounce),once(e,t,n){let s;return s=y(e,(e,n)=>{s(),n?t(e,n):t(e)},n?.debounce),s},emit({name:e,payload:t}){if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(r)return d.push({name:e,payload:t}),d.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void b?.postMessage({name:e,payload:t}));const n=performance.now();v(e,t),_(e,performance.now()-n),b?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,t)=>e+t.size,0)+u.size,eventCounts:new Map(g),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),d=[];for(const e of l.values())for(const{debouncer:t}of e.values())t?.cancel();l.clear();for(const{debouncer:e}of u.values())e?.cancel();u.clear(),f=0,m=0,g.clear(),b?.close(),b=e?.permanent?null:p()}}}exports.Events=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t,n){return this.bus.subscribe(e,t,n)}once(e,t,n){return this.bus.once(e,t,n)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}},exports.createEventBus=t;
package/index.mjs CHANGED
@@ -1 +1 @@
1
- var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(n=>{this._enqueue(e,n)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,n){if(this._pendingFn=e,n&&this._pendingResolvers.push(n),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const n of e)n({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,n=this._pendingResolvers.splice(0);let t;this._pendingFn=void 0;try{t={status:"ok",value:await e()}}catch(e){t={status:"error",error:e}}for(const e of n)e(t);return t}};function n(n){const t=n??{},r=t.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=t.batch?.size,i=t.batch?.size,a=t.batch?.delay??16;if(s&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(s&&(a<0||!Number.isFinite(a)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${a}.`);const o=t.broadcast?.channel,c=null!=o&&o.length>0,l=new Map,u=new Map;let d=[];const h=s?new e({delay:a}):null;let f=0,m=0;const _=new Map,g=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(o);return e.onmessage=e=>{const{name:n,payload:t}=e.data;v(n,t)},e};let b=g();const p=(e,n)=>{f++,m+=n,_.set(e,(_.get(e)??0)+1)},v=(e,n)=>{const t=l.get(e);if(t&&t.size>0)for(const{callback:s}of Array.from(t.values()))try{s(n)}catch(t){const s=t instanceof Error?t:Object.assign(new Error(String(t)),{cause:t}),i=Object.assign(s,{eventName:e,payload:n});r(i)}if(u.size>0)for(const{callback:t}of Array.from(u.values()))try{t({...n,__event__:e})}catch(e){const t=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),s=Object.assign(t,{eventName:"*",payload:n});r(s)}},y=(n,t,r)=>{let s,i;void 0!==r&&r>0?(i=new e({delay:r,leading:!1}),s=e=>{i.fire(()=>{t(e)})}):s=t;const a={callback:s,debouncer:i};if("*"===n)return u.set(t,a),()=>{const e=u.get(t);e&&(e.debouncer?.cancel(),u.delete(t))};{l.has(n)||l.set(n,new Map);const e=l.get(n);return e.set(t,a),()=>{const r=e.get(t);r&&(r.debouncer?.cancel(),e.delete(t),0===e.size&&l.delete(n))}}},w=()=>{const e=d;d=[];for(const{name:n,payload:t}of e){const e=performance.now();v(n,t),p(n,performance.now()-e)}};return{subscribe:(e,n,t)=>y(e,n,t?.debounce),once(e,n,t){let r;return r=y(e,e=>{r(),n(e)},t?.debounce),r},emit:({name:e,payload:n})=>{if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(s)return d.push({name:e,payload:n}),d.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void b?.postMessage({name:e,payload:n}));const t=performance.now();v(e,n),p(e,performance.now()-t),b?.postMessage({name:e,payload:n})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,n)=>e+n.size,0)+u.size,eventCounts:new Map(_),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),d=[];for(const e of l.values())for(const{debouncer:n}of e.values())n?.cancel();l.clear();for(const{debouncer:e}of u.values())e?.cancel();u.clear(),f=0,m=0,_.clear(),b?.close(),b=e?.permanent?null:g()}}}var t=class{bus;constructor(e){this.bus=n(e)}subscribe(e,n,t){return this.bus.subscribe(e,n,t)}once(e,n,t){return this.bus.once(e,n,t)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};export{t as Events,n as createEventBus};
1
+ var e=class{_delay;_leading;_timer;_pendingFn;_pendingResolvers=[];_leadingFired=!1;constructor(e){this._delay=e?.delay??300,this._leading=e?.leading??!1}do(e){return new Promise(n=>{this._enqueue(e,n)})}fire(e){this._enqueue(e,void 0)}_enqueue(e,n){if(this._pendingFn=e,n&&this._pendingResolvers.push(n),this._leading&&!this._leadingFired)return this._leadingFired=!0,this._fire(),clearTimeout(this._timer),void(this._timer=setTimeout(()=>{this._leadingFired=!1,void 0!==this._pendingFn&&this._fire()},this._delay));clearTimeout(this._timer),this._timer=setTimeout(()=>{this._leadingFired=!1,this._fire()},this._delay)}cancel(){clearTimeout(this._timer),this._timer=void 0,this._pendingFn=void 0,this._leadingFired=!1;const e=this._pendingResolvers.splice(0);for(const n of e)n({status:"cancelled"})}async flush(){return this._pendingFn?(clearTimeout(this._timer),this._timer=void 0,this._leadingFired=!1,this._fire()):null}pending(){return void 0!==this._pendingFn}async _fire(){const e=this._pendingFn,n=this._pendingResolvers.splice(0);let t;this._pendingFn=void 0;try{t={status:"ok",value:await e()}}catch(e){t={status:"error",error:e}}for(const e of n)e(t);return t}};function n(n){const t=n??{},r=t.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=t.batch?.size,i=t.batch?.size,o=t.batch?.delay??16;if(s&&(i<=0||!Number.isFinite(i)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${i}.`);if(s&&(o<0||!Number.isFinite(o)))throw new Error(`EventBus: batch.delay must be a non-negative finite number, got ${o}.`);const a=t.broadcast?.channel,c=null!=a&&a.length>0,l=new Map,d=new Map;let u=[];const h=s?new e({delay:o}):null;let f=0,m=0;const g=new Map,b=()=>{if(!c)return null;if("undefined"==typeof BroadcastChannel)return console.warn("EventBus: BroadcastChannel is not supported in this environment. Cross-tab notifications are disabled."),null;const e=new BroadcastChannel(a);return e.onmessage=e=>{const{name:n,payload:t}=e.data;v(n,t)},e};let p=b();const _=(e,n)=>{f++,m+=n,g.set(e,(g.get(e)??0)+1)},v=(e,n)=>{const t=l.get(e);if(t&&t.size>0)for(const{callback:s}of Array.from(t.values()))try{s(n)}catch(t){const s=t instanceof Error?t:Object.assign(new Error(String(t)),{cause:t}),i=Object.assign(s,{eventName:e,payload:n});r(i)}if(d.size>0)for(const{callback:t}of Array.from(d.values()))try{t(n&&"object"==typeof n?{...n}:n,e)}catch(e){const t=e instanceof Error?e:Object.assign(new Error(String(e)),{cause:e}),s=Object.assign(t,{eventName:"*",payload:n});r(s)}},y=(n,t,r)=>{let s,i;void 0!==r&&r>0?(i=new e({delay:r,leading:!1}),s=(e,n)=>{i.fire(()=>{void 0!==n?t(e,n):t(e)})}):s=t;const o={callback:s,debouncer:i};if("*"===n)return d.set(t,o),()=>{const e=d.get(t);e&&(e.debouncer?.cancel(),d.delete(t))};{l.has(n)||l.set(n,new Map);const e=l.get(n);return e.set(t,o),()=>{const r=e.get(t);r&&(r.debouncer?.cancel(),e.delete(t),0===e.size&&l.delete(n))}}},w=()=>{const e=u;u=[];for(const{name:n,payload:t}of e){const e=performance.now();v(n,t),_(n,performance.now()-e)}};return{subscribe:(e,n,t)=>y(e,n,t?.debounce),once(e,n,t){let r;return r=y(e,(e,t)=>{r(),t?n(e,t):n(e)},t?.debounce),r},emit({name:e,payload:n}){if("*"===e)throw new Error('EventBus: Emitting to wildcard event "*" is forbidden.');if(s)return u.push({name:e,payload:n}),u.length>=i?(h.cancel(),void w()):(h.fire(()=>w()),void p?.postMessage({name:e,payload:n}));const t=performance.now();v(e,n),_(e,performance.now()-t),p?.postMessage({name:e,payload:n})},metrics:()=>({totalEvents:f,activeSubscriptions:Array.from(l.values()).reduce((e,n)=>e+n.size,0)+d.size,eventCounts:new Map(g),averageEmitDuration:f>0?m/f:0}),clear:e=>{h?.cancel(),u=[];for(const e of l.values())for(const{debouncer:n}of e.values())n?.cancel();l.clear();for(const{debouncer:e}of d.values())e?.cancel();d.clear(),f=0,m=0,g.clear(),p?.close(),p=e?.permanent?null:b()}}}var t=class{bus;constructor(e){this.bus=n(e)}subscribe(e,n,t){return this.bus.subscribe(e,n,t)}once(e,n,t){return this.bus.once(e,n,t)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};export{t as Events,n as createEventBus};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-events",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "A lightweight, type-safe event bus implementation for TypeScript applications.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",