@flare-engine/events 0.4.12 → 0.4.13

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/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"sourcesContent":["/** Unsubscribe function returned by on/once. */\nexport type Unsubscribe = () => void;\n\n/** Map of event names to handler signatures. */\nexport type EventMap = Record<string, (...args: never[]) => void>;\n\n/**\n * Type-safe event emitter.\n *\n * Emit semantics (shared with Signal):\n * - A handler **added** during an emit runs in that **same** emit — the listener\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private emitDepth = 0;\n private pendingRemove: Array<{ event: keyof E; handler: E[keyof E] }> = [];\n\n /** Subscribe to an event. Returns unsubscribe function. */\n on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let set = this.listeners.get(event);\n if (!set) {\n set = new Set();\n this.listeners.set(event, set);\n }\n set.add(handler);\n return () => {\n this.off(event, handler);\n };\n }\n\n /** Subscribe to an event once. Returns unsubscribe function. */\n once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let fired = false;\n const wrapper = ((...args: never[]) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }) as E[K];\n const unsub = this.on(event, wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push({ event, handler });\n return;\n }\n const set = this.listeners.get(event);\n if (set) {\n set.delete(handler);\n if (set.size === 0) this.listeners.delete(event);\n }\n }\n\n /**\n * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void {\n const set = this.listeners.get(event);\n if (!set) return;\n this.emitDepth++;\n try {\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n const { event: ev, handler: h } = this.pendingRemove[i]!;\n const s = this.listeners.get(ev);\n if (s) {\n s.delete(h);\n if (s.size === 0) this.listeners.delete(ev);\n }\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all listeners for a specific event, or all listeners. */\n removeAll(event?: keyof E): void {\n if (event !== undefined) {\n this.listeners.delete(event);\n // Don't clear the shared pendingRemove here: mid-emit it may hold deferred\n // removals for OTHER events. Entries for this event become harmless no-ops\n // at flush (their Set is already gone).\n return;\n }\n this.listeners.clear();\n this.pendingRemove.length = 0;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/**\n * Lightweight single-event emitter. Less overhead than EventEmitter.\n *\n * Emit semantics (shared with EventEmitter):\n * - A handler **added** during an emit runs in that **same** emit — the handler\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private emitDepth = 0;\n private pendingRemove: Array<(value: T) => void> = [];\n\n /** Subscribe to the signal. Returns unsubscribe function. */\n add(handler: (value: T) => void): Unsubscribe {\n this.handlers.add(handler);\n return () => {\n this.remove(handler);\n };\n }\n\n /** Subscribe once. Returns unsubscribe function. */\n once(handler: (value: T) => void): Unsubscribe {\n let fired = false;\n const wrapper = (value: T) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n handler(value);\n };\n const unsub = this.add(wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n remove(handler: (value: T) => void): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /**\n * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.emitDepth++;\n try {\n for (const handler of this.handlers) {\n handler(value);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n this.handlers.delete(this.pendingRemove[i]!);\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all handlers. */\n clear(): void {\n this.handlers.clear();\n this.pendingRemove.length = 0;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,IAAa,eAAb,MAA8C;CAC5C,4BAAoB,IAAI,IAA8B;CACtD,YAAoB;CACpB,gBAAwE,CAAC;;CAGzE,GAAsB,OAAU,SAA4B;EAC1D,IAAI,MAAM,KAAK,UAAU,IAAI,KAAK;EAClC,IAAI,CAAC,KAAK;GACR,sBAAM,IAAI,IAAI;GACd,KAAK,UAAU,IAAI,OAAO,GAAG;EAC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F;EACA,KAAK,UAAU,MAAM;EACrB,KAAK,cAAc,SAAS;CAC9B;AACF;;;;;;;;;;;;;;;AChGA,IAAa,SAAb,MAA8B;CAC5B,2BAAmB,IAAI,IAAwB;CAC/C,YAAoB;CACpB,gBAAmD,CAAC;;CAGpD,IAAI,SAA0C;EAC5C,KAAK,SAAS,IAAI,OAAO;EACzB,aAAa;GACX,KAAK,OAAO,OAAO;EACrB;CACF;;CAGA,KAAK,SAA0C;EAC7C,IAAI,QAAQ;EACZ,MAAM,WAAW,UAAa;GAE5B,IAAI,OAAO;GACX,QAAQ;GACR,MAAM;GACN,QAAQ,KAAK;EACf;EACA,MAAM,QAAQ,KAAK,IAAI,OAAO;EAC9B,OAAO;CACT;;CAGA,OAAO,SAAmC;EACxC,IAAI,KAAK,YAAY,GACnB,KAAK,cAAc,KAAK,OAAO;OAE/B,KAAK,SAAS,OAAO,OAAO;CAEhC;;;;;CAMA,KAAK,GAAG,MAA8C;EACpD,MAAM,QAAQ,KAAK;EACnB,KAAK;EACL,IAAI;GACF,KAAK,MAAM,WAAW,KAAK,UACzB,QAAQ,KAAK;EAEjB,UAAU;GACR,KAAK;GAEL,IAAI,KAAK,cAAc,KAAK,KAAK,cAAc,SAAS,GACtD,KAAK,aAAa;EAEtB;CACF;CAEA,eAA6B;EAC3B,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,cAAc,QAAQ,KAC7C,KAAK,SAAS,OAAO,KAAK,cAAc,EAAG;EAE7C,KAAK,cAAc,SAAS;CAC9B;;CAGA,QAAc;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,cAAc,SAAS;CAC9B;AACF"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"sourcesContent":["/** Unsubscribe function returned by on/once. */\nexport type Unsubscribe = () => void;\n\n/** Map of event names to handler signatures. */\nexport type EventMap = Record<string, (...args: never[]) => void>;\n\n/**\n * Type-safe event emitter.\n *\n * Emit semantics (shared with Signal):\n * - A handler **added** during an emit runs in that **same** emit — the listener\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private emitDepth = 0;\n private pendingRemove: Array<{ event: keyof E; handler: E[keyof E] }> = [];\n\n /** Subscribe to an event. Returns unsubscribe function. */\n on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let set = this.listeners.get(event);\n if (!set) {\n set = new Set();\n this.listeners.set(event, set);\n }\n set.add(handler);\n return () => {\n this.off(event, handler);\n };\n }\n\n /** Subscribe to an event once. Returns unsubscribe function. */\n once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let fired = false;\n const wrapper = ((...args: never[]) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }) as E[K];\n const unsub = this.on(event, wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push({ event, handler });\n return;\n }\n const set = this.listeners.get(event);\n if (set) {\n set.delete(handler);\n if (set.size === 0) this.listeners.delete(event);\n }\n }\n\n /**\n * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void {\n const set = this.listeners.get(event);\n if (!set) return;\n this.emitDepth++;\n try {\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n const { event: ev, handler: h } = this.pendingRemove[i]!;\n const s = this.listeners.get(ev);\n if (s) {\n s.delete(h);\n if (s.size === 0) this.listeners.delete(ev);\n }\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all listeners for a specific event, or all listeners. */\n removeAll(event?: keyof E): void {\n if (event !== undefined) {\n this.listeners.delete(event);\n // Don't clear the shared pendingRemove here: mid-emit it may hold deferred\n // removals for OTHER events. Entries for this event become harmless no-ops\n // at flush (their Set is already gone).\n return;\n }\n this.listeners.clear();\n this.pendingRemove.length = 0;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/**\n * Lightweight single-event emitter. Less overhead than EventEmitter.\n *\n * Emit semantics (shared with EventEmitter):\n * - A handler **added** during an emit runs in that **same** emit — the handler\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private emitDepth = 0;\n private pendingRemove: Array<(value: T) => void> = [];\n\n /** Subscribe to the signal. Returns unsubscribe function. */\n add(handler: (value: T) => void): Unsubscribe {\n this.handlers.add(handler);\n return () => {\n this.remove(handler);\n };\n }\n\n /** Subscribe once. Returns unsubscribe function. */\n once(handler: (value: T) => void): Unsubscribe {\n let fired = false;\n const wrapper = (value: T) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n handler(value);\n };\n const unsub = this.add(wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n remove(handler: (value: T) => void): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /**\n * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.emitDepth++;\n try {\n for (const handler of this.handlers) {\n handler(value);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n this.handlers.delete(this.pendingRemove[i]!);\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all handlers. */\n clear(): void {\n this.handlers.clear();\n this.pendingRemove.length = 0;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,IAAa,eAAb,MAA8C;CACpC,4BAAY,IAAI,IAA8B;CAC9C,YAAY;CACZ,gBAAgE,CAAC;;CAGzE,GAAsB,OAAU,SAA4B;EAC1D,IAAI,MAAM,KAAK,UAAU,IAAI,KAAK;EAClC,IAAI,CAAC,KAAK;GACR,sBAAM,IAAI,IAAI;GACd,KAAK,UAAU,IAAI,OAAO,GAAG;EAC/B;EACA,IAAI,IAAI,OAAO;EACf,aAAa;GACX,KAAK,IAAI,OAAO,OAAO;EACzB;CACF;;CAGA,KAAwB,OAAU,SAA4B;EAC5D,IAAI,QAAQ;EACZ,MAAM,YAAY,GAAG,SAAkB;GAErC,IAAI,OAAO;GACX,QAAQ;GACR,MAAM;GAEL,QAAqB,GAAG,IAAI;EAC/B;EACA,MAAM,QAAQ,KAAK,GAAG,OAAO,OAAO;EACpC,OAAO;CACT;;CAGA,IAAuB,OAAU,SAAqB;EACpD,IAAI,KAAK,YAAY,GAAG;GACtB,KAAK,cAAc,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AAM;GACN,QAAQ,KAAK;EACf;EACA,MAAM,QAAQ,KAAK,IAAI,OAAO;EAC9B,OAAO;CACT;;CAGA,OAAO,SAAmC;EACxC,IAAI,KAAK,YAAY,GACnB,KAAK,cAAc,KAAK,OAAO;OAE/B,KAAK,SAAS,OAAO,OAAO;CAEhC;;;;;CAMA,KAAK,GAAG,MAA8C;EACpD,MAAM,QAAQ,KAAK;EACnB,KAAK;EACL,IAAI;GACF,KAAK,MAAM,WAAW,KAAK,UACzB,QAAQ,KAAK;EAEjB,UAAU;GACR,KAAK;GAEL,IAAI,KAAK,cAAc,KAAK,KAAK,cAAc,SAAS,GACtD,KAAK,aAAa;EAEtB;CACF;CAEQ,eAAqB;EAC3B,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,cAAc,QAAQ,KAC7C,KAAK,SAAS,OAAO,KAAK,cAAc,EAAG;EAE7C,KAAK,cAAc,SAAS;CAC9B;;CAGA,QAAc;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,cAAc,SAAS;CAC9B;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"sourcesContent":["/** Unsubscribe function returned by on/once. */\nexport type Unsubscribe = () => void;\n\n/** Map of event names to handler signatures. */\nexport type EventMap = Record<string, (...args: never[]) => void>;\n\n/**\n * Type-safe event emitter.\n *\n * Emit semantics (shared with Signal):\n * - A handler **added** during an emit runs in that **same** emit — the listener\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private emitDepth = 0;\n private pendingRemove: Array<{ event: keyof E; handler: E[keyof E] }> = [];\n\n /** Subscribe to an event. Returns unsubscribe function. */\n on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let set = this.listeners.get(event);\n if (!set) {\n set = new Set();\n this.listeners.set(event, set);\n }\n set.add(handler);\n return () => {\n this.off(event, handler);\n };\n }\n\n /** Subscribe to an event once. Returns unsubscribe function. */\n once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let fired = false;\n const wrapper = ((...args: never[]) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }) as E[K];\n const unsub = this.on(event, wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push({ event, handler });\n return;\n }\n const set = this.listeners.get(event);\n if (set) {\n set.delete(handler);\n if (set.size === 0) this.listeners.delete(event);\n }\n }\n\n /**\n * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void {\n const set = this.listeners.get(event);\n if (!set) return;\n this.emitDepth++;\n try {\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n const { event: ev, handler: h } = this.pendingRemove[i]!;\n const s = this.listeners.get(ev);\n if (s) {\n s.delete(h);\n if (s.size === 0) this.listeners.delete(ev);\n }\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all listeners for a specific event, or all listeners. */\n removeAll(event?: keyof E): void {\n if (event !== undefined) {\n this.listeners.delete(event);\n // Don't clear the shared pendingRemove here: mid-emit it may hold deferred\n // removals for OTHER events. Entries for this event become harmless no-ops\n // at flush (their Set is already gone).\n return;\n }\n this.listeners.clear();\n this.pendingRemove.length = 0;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/**\n * Lightweight single-event emitter. Less overhead than EventEmitter.\n *\n * Emit semantics (shared with EventEmitter):\n * - A handler **added** during an emit runs in that **same** emit — the handler\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private emitDepth = 0;\n private pendingRemove: Array<(value: T) => void> = [];\n\n /** Subscribe to the signal. Returns unsubscribe function. */\n add(handler: (value: T) => void): Unsubscribe {\n this.handlers.add(handler);\n return () => {\n this.remove(handler);\n };\n }\n\n /** Subscribe once. Returns unsubscribe function. */\n once(handler: (value: T) => void): Unsubscribe {\n let fired = false;\n const wrapper = (value: T) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n handler(value);\n };\n const unsub = this.add(wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n remove(handler: (value: T) => void): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /**\n * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.emitDepth++;\n try {\n for (const handler of this.handlers) {\n handler(value);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n this.handlers.delete(this.pendingRemove[i]!);\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all handlers. */\n clear(): void {\n this.handlers.clear();\n this.pendingRemove.length = 0;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,IAAa,eAAb,MAA8C;CAC5C,4BAAoB,IAAI,IAA8B;CACtD,YAAoB;CACpB,gBAAwE,CAAC;;CAGzE,GAAsB,OAAU,SAA4B;EAC1D,IAAI,MAAM,KAAK,UAAU,IAAI,KAAK;EAClC,IAAI,CAAC,KAAK;GACR,sBAAM,IAAI,IAAI;GACd,KAAK,UAAU,IAAI,OAAO,GAAG;EAC/B;EACA,IAAI,IAAI,OAAO;EACf,aAAa;GACX,KAAK,IAAI,OAAO,OAAO;EACzB;CACF;;CAGA,KAAwB,OAAU,SAA4B;EAC5D,IAAI,QAAQ;EACZ,MAAM,YAAY,GAAG,SAAkB;GAErC,IAAI,OAAO;GACX,QAAQ;GACR,MAAM;GAEN,QAAsB,GAAG,IAAI;EAC/B;EACA,MAAM,QAAQ,KAAK,GAAG,OAAO,OAAO;EACpC,OAAO;CACT;;CAGA,IAAuB,OAAU,SAAqB;EACpD,IAAI,KAAK,YAAY,GAAG;GACtB,KAAK,cAAc,KAAK;IAAE;IAAO;GAAQ,CAAC;GAC1C;EACF;EACA,MAAM,MAAM,KAAK,UAAU,IAAI,KAAK;EACpC,IAAI,KAAK;GACP,IAAI,OAAO,OAAO;GAClB,IAAI,IAAI,SAAS,GAAG,KAAK,UAAU,OAAO,KAAK;EACjD;CACF;;;;;CAMA,KAAwB,OAAU,GAAG,MAA8B;EACjE,MAAM,MAAM,KAAK,UAAU,IAAI,KAAK;EACpC,IAAI,CAAC,KAAK;EACV,KAAK;EACL,IAAI;GACF,KAAK,MAAM,WAAW,KAEpB,QAAs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AEL,IAAI,KAAK,cAAc,KAAK,KAAK,cAAc,SAAS,GACtD,KAAK,aAAa;EAEtB;CACF;CAEA,eAA6B;EAC3B,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,cAAc,QAAQ,KAC7C,KAAK,SAAS,OAAO,KAAK,cAAc,EAAG;EAE7C,KAAK,cAAc,SAAS;CAC9B;;CAGA,QAAc;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,cAAc,SAAS;CAC9B;AACF"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"sourcesContent":["/** Unsubscribe function returned by on/once. */\nexport type Unsubscribe = () => void;\n\n/** Map of event names to handler signatures. */\nexport type EventMap = Record<string, (...args: never[]) => void>;\n\n/**\n * Type-safe event emitter.\n *\n * Emit semantics (shared with Signal):\n * - A handler **added** during an emit runs in that **same** emit — the listener\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private emitDepth = 0;\n private pendingRemove: Array<{ event: keyof E; handler: E[keyof E] }> = [];\n\n /** Subscribe to an event. Returns unsubscribe function. */\n on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let set = this.listeners.get(event);\n if (!set) {\n set = new Set();\n this.listeners.set(event, set);\n }\n set.add(handler);\n return () => {\n this.off(event, handler);\n };\n }\n\n /** Subscribe to an event once. Returns unsubscribe function. */\n once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe {\n let fired = false;\n const wrapper = ((...args: never[]) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }) as E[K];\n const unsub = this.on(event, wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push({ event, handler });\n return;\n }\n const set = this.listeners.get(event);\n if (set) {\n set.delete(handler);\n if (set.size === 0) this.listeners.delete(event);\n }\n }\n\n /**\n * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void {\n const set = this.listeners.get(event);\n if (!set) return;\n this.emitDepth++;\n try {\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n const { event: ev, handler: h } = this.pendingRemove[i]!;\n const s = this.listeners.get(ev);\n if (s) {\n s.delete(h);\n if (s.size === 0) this.listeners.delete(ev);\n }\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all listeners for a specific event, or all listeners. */\n removeAll(event?: keyof E): void {\n if (event !== undefined) {\n this.listeners.delete(event);\n // Don't clear the shared pendingRemove here: mid-emit it may hold deferred\n // removals for OTHER events. Entries for this event become harmless no-ops\n // at flush (their Set is already gone).\n return;\n }\n this.listeners.clear();\n this.pendingRemove.length = 0;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/**\n * Lightweight single-event emitter. Less overhead than EventEmitter.\n *\n * Emit semantics (shared with EventEmitter):\n * - A handler **added** during an emit runs in that **same** emit — the handler\n * set is iterated live (no per-emit snapshot allocation).\n * - A handler **removed** during an emit still runs for that emit (and any nested\n * emit); the removal takes effect once the outermost emit completes.\n * Exception: a `once` handler is guarded independently of deferred removal and\n * fires at most once, even under a nested emit.\n * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing\n * handler propagates to the caller but never wedges deferred removal.\n */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private emitDepth = 0;\n private pendingRemove: Array<(value: T) => void> = [];\n\n /** Subscribe to the signal. Returns unsubscribe function. */\n add(handler: (value: T) => void): Unsubscribe {\n this.handlers.add(handler);\n return () => {\n this.remove(handler);\n };\n }\n\n /** Subscribe once. Returns unsubscribe function. */\n once(handler: (value: T) => void): Unsubscribe {\n let fired = false;\n const wrapper = (value: T) => {\n // Deferred removal keeps the wrapper in the live set until the outermost emit ends.\n if (fired) return;\n fired = true;\n unsub();\n handler(value);\n };\n const unsub = this.add(wrapper);\n return unsub;\n }\n\n /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */\n remove(handler: (value: T) => void): void {\n if (this.emitDepth > 0) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /**\n * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and\n * throw-safe (see the class docstring for the add/remove-during-emit semantics).\n */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.emitDepth++;\n try {\n for (const handler of this.handlers) {\n handler(value);\n }\n } finally {\n this.emitDepth--;\n // Flush deferred removes only when the outermost emit has unwound (rare path).\n if (this.emitDepth === 0 && this.pendingRemove.length > 0) {\n this.flushPending();\n }\n }\n }\n\n private flushPending(): void {\n for (let i = 0; i < this.pendingRemove.length; i++) {\n this.handlers.delete(this.pendingRemove[i]!);\n }\n this.pendingRemove.length = 0;\n }\n\n /** Remove all handlers. */\n clear(): void {\n this.handlers.clear();\n this.pendingRemove.length = 0;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,IAAa,eAAb,MAA8C;CACpC,4BAAY,IAAI,IAA8B;CAC9C,YAAY;CACZ,gBAAgE,CAAC;;CAG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"version": "0.4.12",
3
+ "version": "0.4.13",
4
4
  "description": "Type-safe EventEmitter and Signal",
5
5
  "keywords": ["flare-engine", "events", "signal", "emitter", "react-native", "typescript"],
6
6
  "license": "Apache-2.0",