@asaidimu/utils-events 1.0.3 → 1.1.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 CHANGED
@@ -69,7 +69,7 @@ pnpm add @asaidimu/utils-events
69
69
  ## Quick Start
70
70
 
71
71
  ```typescript
72
- import { createEventBus } from '@asaidimu/utils-events';
72
+ import { createEventBus } from "@asaidimu/utils-events";
73
73
 
74
74
  // 1. Define your event map
75
75
  interface AppEvents {
@@ -82,12 +82,12 @@ interface AppEvents {
82
82
  const bus = createEventBus<AppEvents>();
83
83
 
84
84
  // 3. Subscribe
85
- const unsubscribe = bus.subscribe('userLogin', (payload) => {
85
+ const unsubscribe = bus.subscribe("userLogin", (payload) => {
86
86
  console.log(`Welcome ${payload.name}!`);
87
87
  });
88
88
 
89
89
  // 4. Emit an event
90
- bus.emit({ name: 'userLogin', payload: { userId: '123', name: 'Alice' } });
90
+ bus.emit({ name: "userLogin", payload: { userId: "123", name: "Alice" } });
91
91
  // Logs: "Welcome Alice!"
92
92
 
93
93
  // 5. Unsubscribe when done
@@ -102,20 +102,20 @@ unsubscribe();
102
102
 
103
103
  ```typescript
104
104
  function createEventBus<TEventMap extends Record<string, any>>(
105
- options?: EventBusOptions
106
- ): EventBus<TEventMap>
105
+ options?: EventBusOptions,
106
+ ): EventBus<TEventMap>;
107
107
  ```
108
108
 
109
109
  Creates a new event bus instance.
110
110
 
111
111
  #### Options
112
112
 
113
- | Option | Type | Default | Description |
114
- | -------------------- | ----------------------------- | --------------------------- | --------------------------------------------------------------------------- |
115
- | `batch.size` | `number` | `undefined` | Enables **deferred mode**; flush when queue reaches this size. |
116
- | `batch.delay` | `number` | `1000` (if batching) | Quiet period (ms) before flushing a batch. |
117
- | `errorHandler` | `(error: EventError) => void` | `console.error` | Custom error handler for subscriber callbacks. |
118
- | `broadcast.channel` | `string` | `"event-bus-channel"` | Enables cross-instance broadcast using the given `BroadcastChannel` name. |
113
+ | Option | Type | Default | Description |
114
+ | ------------------- | ----------------------------- | --------------------- | ------------------------------------------------------------------------- |
115
+ | `batch.size` | `number` | `undefined` | Enables **deferred mode**; flush when queue reaches this size. |
116
+ | `batch.delay` | `number` | `1000` (if batching) | Quiet period (ms) before flushing a batch. |
117
+ | `errorHandler` | `(error: EventError) => void` | `console.error` | Custom error handler for subscriber callbacks. |
118
+ | `broadcast.channel` | `string` | `"event-bus-channel"` | Enables cross-instance broadcast using the given `BroadcastChannel` name. |
119
119
 
120
120
  > If `batch.size` is provided, the bus runs in **deferred mode** (events are queued and flushed asynchronously). Otherwise it runs in **synchronous mode** (events are dispatched immediately).
121
121
 
@@ -133,7 +133,7 @@ subscribe<TEventName extends keyof TEventMap>(
133
133
  Registers a permanent listener. Returns an **unsubscribe function**.
134
134
 
135
135
  ```typescript
136
- const off = bus.subscribe('dataUpdate', ({ records }) => {
136
+ const off = bus.subscribe("dataUpdate", ({ records }) => {
137
137
  updateUI(records);
138
138
  });
139
139
 
@@ -155,8 +155,8 @@ once<TEventName extends keyof TEventMap>(
155
155
  Registers a one-time listener that automatically unsubscribes after the first emission. Returns a **cancel function** (to unsubscribe before it fires).
156
156
 
157
157
  ```typescript
158
- bus.once('userLogin', (payload) => {
159
- console.log('First login only');
158
+ bus.once("userLogin", (payload) => {
159
+ console.log("First login only");
160
160
  });
161
161
 
162
162
  // The callback will run at most once.
@@ -178,7 +178,7 @@ emit<TEventName extends keyof TEventMap>(
178
178
  Dispatches an event. In **synchronous mode** all subscribers run immediately. In **deferred mode** the event is queued and flushed according to `batch.size` and `batch.delay`. Cross-instance messages are sent **immediately** even in deferred mode to avoid latency.
179
179
 
180
180
  ```typescript
181
- bus.emit({ name: 'dataUpdate', payload: { records: 42 } });
181
+ bus.emit({ name: "dataUpdate", payload: { records: 42 } });
182
182
  ```
183
183
 
184
184
  ---
@@ -226,9 +226,9 @@ When many events are fired in rapid succession (e.g., keystrokes, scroll handler
226
226
  ```typescript
227
227
  const batchedBus = createEventBus<MyEvents>({
228
228
  batch: {
229
- size: 20, // flush after 20 queued events
230
- delay: 100 // or after 100ms of inactivity
231
- }
229
+ size: 20, // flush after 20 queued events
230
+ delay: 100, // or after 100ms of inactivity
231
+ },
232
232
  });
233
233
  ```
234
234
 
@@ -242,15 +242,15 @@ Enable the `broadcast` option to automatically send every emitted event to other
242
242
 
243
243
  ```typescript
244
244
  const bus = createEventBus<MyEvents>({
245
- broadcast: { channel: 'my-app-events' }
245
+ broadcast: { channel: "my-app-events" },
246
246
  });
247
247
 
248
248
  // In tab A
249
- bus.emit({ name: 'userLogin', payload: { userId: '1' } });
249
+ bus.emit({ name: "userLogin", payload: { userId: "1" } });
250
250
 
251
251
  // In tab B (same origin)
252
- bus.subscribe('userLogin', (payload) => {
253
- console.log('Another tab logged in:', payload.userId);
252
+ bus.subscribe("userLogin", (payload) => {
253
+ console.log("Another tab logged in:", payload.userId);
254
254
  });
255
255
  ```
256
256
 
@@ -265,9 +265,9 @@ const bus = createEventBus<MyEvents>({
265
265
  errorHandler: (err) => {
266
266
  myErrorTracker.capture(err, {
267
267
  eventName: err.eventName,
268
- payload: err.payload
268
+ payload: err.payload,
269
269
  });
270
- }
270
+ },
271
271
  });
272
272
  ```
273
273
 
package/index.d.mts CHANGED
@@ -72,16 +72,18 @@ interface EventBus<TEventMap extends Record<string, any>> {
72
72
  * Subscribes to a specific event by name.
73
73
  * @param eventName - The name of the event to subscribe to.
74
74
  * @param callback - The function to call when the event is emitted.
75
+ * @param options - Extra options to determine the behaviour of the
76
+ * subscription
75
77
  * @returns A function to unsubscribe from the event.
76
78
  */
77
- subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
79
+ subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions) => () => void;
78
80
  /**
79
81
  * Subscribes to an event and automatically unsubscribes after it fires once.
80
82
  * @param eventName - The name of the event to subscribe to.
81
83
  * @param callback - The function to call when the event is emitted.
82
84
  * @returns A function to cancel the one-shot subscription before it fires.
83
85
  */
84
- once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
86
+ once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions) => () => void;
85
87
  /**
86
88
  * Emits an event with a payload to all subscribed listeners.
87
89
  * @param event - An object containing the event name and payload.
@@ -125,6 +127,14 @@ interface EventError extends Error {
125
127
  /** Optional payload that caused the error. */
126
128
  payload?: unknown;
127
129
  }
130
+ interface SubscribeOptions {
131
+ /**
132
+ * Debounce delay in milliseconds. When multiple events arrive in quick
133
+ * succession, the callback runs only after the quiet period ends, using the
134
+ * latest payload. Default = no debouncing.
135
+ */
136
+ debounce?: number;
137
+ }
128
138
 
129
139
  /**
130
140
  * Creates a typed event bus.
@@ -172,4 +182,4 @@ declare class Events<TEventMap extends Record<string, any>> implements EventBus<
172
182
  clear(): void;
173
183
  }
174
184
 
175
- export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, createEventBus };
185
+ export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, type SubscribeOptions, createEventBus };
package/index.d.ts CHANGED
@@ -72,16 +72,18 @@ interface EventBus<TEventMap extends Record<string, any>> {
72
72
  * Subscribes to a specific event by name.
73
73
  * @param eventName - The name of the event to subscribe to.
74
74
  * @param callback - The function to call when the event is emitted.
75
+ * @param options - Extra options to determine the behaviour of the
76
+ * subscription
75
77
  * @returns A function to unsubscribe from the event.
76
78
  */
77
- subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
79
+ subscribe: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions) => () => void;
78
80
  /**
79
81
  * Subscribes to an event and automatically unsubscribes after it fires once.
80
82
  * @param eventName - The name of the event to subscribe to.
81
83
  * @param callback - The function to call when the event is emitted.
82
84
  * @returns A function to cancel the one-shot subscription before it fires.
83
85
  */
84
- once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void) => () => void;
86
+ once: <TEventName extends keyof TEventMap>(eventName: TEventName, callback: (payload: TEventMap[TEventName]) => void, options?: SubscribeOptions) => () => void;
85
87
  /**
86
88
  * Emits an event with a payload to all subscribed listeners.
87
89
  * @param event - An object containing the event name and payload.
@@ -125,6 +127,14 @@ interface EventError extends Error {
125
127
  /** Optional payload that caused the error. */
126
128
  payload?: unknown;
127
129
  }
130
+ interface SubscribeOptions {
131
+ /**
132
+ * Debounce delay in milliseconds. When multiple events arrive in quick
133
+ * succession, the callback runs only after the quiet period ends, using the
134
+ * latest payload. Default = no debouncing.
135
+ */
136
+ debounce?: number;
137
+ }
128
138
 
129
139
  /**
130
140
  * Creates a typed event bus.
@@ -172,4 +182,4 @@ declare class Events<TEventMap extends Record<string, any>> implements EventBus<
172
182
  clear(): void;
173
183
  }
174
184
 
175
- export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, createEventBus };
185
+ export { type DebouncerResult, type EventBus, type EventBusOptions, type EventError, type EventMetrics, Events, type SubscribeOptions, createEventBus };
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??{},i=n.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=n.batch?.size,r=n.batch?.size,a=n.batch?.delay??16;if(s&&(r<=0||!Number.isFinite(r)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${r}.`);if(s&&(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;let u=[];const d=s?new e({delay:a}):null;let h=0,m=0;const _=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(o);return e.onmessage=e=>{const{name:t,payload:n}=e.data;v(t,n)},e};let g=p();const f=(e,t)=>{h++,m+=t,_.set(e,(_.get(e)??0)+1)},v=(e,t)=>{const n=l.get(e);if(n&&0!==n.size)for(const s of Array.from(n))try{s(t)}catch(n){const s=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),r=Object.assign(s,{eventName:e,payload:t});i(r)}},b=(e,t)=>{l.has(e)||l.set(e,new Set);const n=l.get(e);return n.add(t),()=>{n.delete(t),0===n.size&&l.delete(e)}},y=()=>{const e=u;u=[];for(const{name:t,payload:n}of e){const e=performance.now();v(t,n),f(t,performance.now()-e)}};return{subscribe:(e,t)=>b(e,t),once:(e,t)=>{let n;return n=b(e,(e=>{n(),t(e)})),n},emit:({name:e,payload:t})=>{if(s)return u.push({name:e,payload:t}),u.length>=r?(d.cancel(),void y()):(d.fire((()=>y())),void g?.postMessage({name:e,payload:t}));const n=performance.now();v(e,t),f(e,performance.now()-n),g?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:h,activeSubscriptions:Array.from(l.values()).reduce(((e,t)=>e+t.size),0),eventCounts:new Map(_),averageEmitDuration:h>0?m/h:0}),clear:()=>{d?.cancel(),u=[],l.clear(),h=0,m=0,_.clear(),g?.close(),g=p()}}}exports.Events=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t){return this.bus.subscribe(e,t)}once(e,t){return this.bus.once(e,t)}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)),i=null!=n.batch?.size,r=n.batch?.size,a=n.batch?.delay??16;if(i&&(r<=0||!Number.isFinite(r)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${r}.`);if(i&&(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;let u=[];const d=i?new e({delay:a}):null;let h=0,m=0;const _=new Map,f=()=>{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;b(t,n)},e};let g=f();const p=(e,t)=>{h++,m+=t,_.set(e,(_.get(e)??0)+1)},b=(e,t)=>{const n=l.get(e);if(n&&0!==n.size)for(const{callback:i}of Array.from(n.values()))try{i(t)}catch(n){const i=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),r=Object.assign(i,{eventName:e,payload:t});s(r)}},v=(t,n,s)=>{l.has(t)||l.set(t,new Map);const i=l.get(t);let r,a;return void 0!==s&&s>0?(a=new e({delay:s,leading:!1}),r=e=>{a.fire((()=>{n(e)}))}):r=n,i.set(n,{callback:r,debouncer:a}),()=>{const e=i.get(n);e&&(e.debouncer?.cancel(),i.delete(n),0===i.size&&l.delete(t))}},y=()=>{const e=u;u=[];for(const{name:t,payload:n}of e){const e=performance.now();b(t,n),p(t,performance.now()-e)}};return{subscribe:(e,t,n)=>v(e,t,n?.debounce),once:(e,t,n)=>{let s;return s=v(e,(e=>{s(),t(e)}),n?.debounce),s},emit:({name:e,payload:t})=>{if(i)return u.push({name:e,payload:t}),u.length>=r?(d.cancel(),void y()):(d.fire((()=>y())),void g?.postMessage({name:e,payload:t}));const n=performance.now();b(e,t),p(e,performance.now()-n),g?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:h,activeSubscriptions:Array.from(l.values()).reduce(((e,t)=>e+t.size),0),eventCounts:new Map(_),averageEmitDuration:h>0?m/h:0}),clear:()=>{d?.cancel(),u=[];for(const e of l.values())for(const{debouncer:t}of e.values())t?.cancel();l.clear(),h=0,m=0,_.clear(),g?.close(),g=f()}}}exports.Events=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t){return this.bus.subscribe(e,t)}once(e,t){return this.bus.once(e,t)}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((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??{},i=n.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=n.batch?.size,r=n.batch?.size,a=n.batch?.delay??16;if(s&&(r<=0||!Number.isFinite(r)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${r}.`);if(s&&(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;let d=[];const u=s?new e({delay:a}):null;let h=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;b(t,n)},e};let p=g();const f=(e,t)=>{h++,m+=t,_.set(e,(_.get(e)??0)+1)},b=(e,t)=>{const n=l.get(e);if(n&&0!==n.size)for(const s of Array.from(n))try{s(t)}catch(n){const s=n instanceof Error?n:Object.assign(new Error(String(n)),{cause:n}),r=Object.assign(s,{eventName:e,payload:t});i(r)}},v=(e,t)=>{l.has(e)||l.set(e,new Set);const n=l.get(e);return n.add(t),()=>{n.delete(t),0===n.size&&l.delete(e)}},y=()=>{const e=d;d=[];for(const{name:t,payload:n}of e){const e=performance.now();b(t,n),f(t,performance.now()-e)}};return{subscribe:(e,t)=>v(e,t),once:(e,t)=>{let n;return n=v(e,(e=>{n(),t(e)})),n},emit:({name:e,payload:t})=>{if(s)return d.push({name:e,payload:t}),d.length>=r?(u.cancel(),void y()):(u.fire((()=>y())),void p?.postMessage({name:e,payload:t}));const n=performance.now();b(e,t),f(e,performance.now()-n),p?.postMessage({name:e,payload:t})},metrics:()=>({totalEvents:h,activeSubscriptions:Array.from(l.values()).reduce(((e,t)=>e+t.size),0),eventCounts:new Map(_),averageEmitDuration:h>0?m/h:0}),clear:()=>{u?.cancel(),d=[],l.clear(),h=0,m=0,_.clear(),p?.close(),p=g()}}}var n=class{bus;constructor(e){this.bus=t(e)}subscribe(e,t){return this.bus.subscribe(e,t)}once(e,t){return this.bus.once(e,t)}emit(e){return this.bus.emit(e)}metrics(){return this.bus.metrics()}clear(){return this.bus.clear()}};export{n as Events,t 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??{},i=t.errorHandler??(e=>console.error("EventBus Error:",e)),s=null!=t.batch?.size,r=t.batch?.size,a=t.batch?.delay??16;if(s&&(r<=0||!Number.isFinite(r)))throw new Error(`EventBus: batch.size must be a positive finite number, got ${r}.`);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;let u=[];const d=s?new e({delay:a}):null;let h=0,m=0;const _=new Map,f=()=>{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;b(n,t)},e};let g=f();const p=(e,n)=>{h++,m+=n,_.set(e,(_.get(e)??0)+1)},b=(e,n)=>{const t=l.get(e);if(t&&0!==t.size)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}),r=Object.assign(s,{eventName:e,payload:n});i(r)}},v=(n,t,i)=>{l.has(n)||l.set(n,new Map);const s=l.get(n);let r,a;return void 0!==i&&i>0?(a=new e({delay:i,leading:!1}),r=e=>{a.fire((()=>{t(e)}))}):r=t,s.set(t,{callback:r,debouncer:a}),()=>{const e=s.get(t);e&&(e.debouncer?.cancel(),s.delete(t),0===s.size&&l.delete(n))}},y=()=>{const e=u;u=[];for(const{name:n,payload:t}of e){const e=performance.now();b(n,t),p(n,performance.now()-e)}};return{subscribe:(e,n,t)=>v(e,n,t?.debounce),once:(e,n,t)=>{let i;return i=v(e,(e=>{i(),n(e)}),t?.debounce),i},emit:({name:e,payload:n})=>{if(s)return u.push({name:e,payload:n}),u.length>=r?(d.cancel(),void y()):(d.fire((()=>y())),void g?.postMessage({name:e,payload:n}));const t=performance.now();b(e,n),p(e,performance.now()-t),g?.postMessage({name:e,payload:n})},metrics:()=>({totalEvents:h,activeSubscriptions:Array.from(l.values()).reduce(((e,n)=>e+n.size),0),eventCounts:new Map(_),averageEmitDuration:h>0?m/h:0}),clear:()=>{d?.cancel(),u=[];for(const e of l.values())for(const{debouncer:n}of e.values())n?.cancel();l.clear(),h=0,m=0,_.clear(),g?.close(),g=f()}}}var t=class{bus;constructor(e){this.bus=n(e)}subscribe(e,n){return this.bus.subscribe(e,n)}once(e,n){return this.bus.once(e,n)}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.0.3",
3
+ "version": "1.1.1",
4
4
  "description": "A lightweight, type-safe event bus implementation for TypeScript applications.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",