@flare-engine/events 0.4.10 → 0.4.12
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/index.d.mts +4 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@ onHit.emit(10);
|
|
|
27
27
|
`emit()` iterates the handler set **live** (no per-emit snapshot — this is a hot path):
|
|
28
28
|
|
|
29
29
|
- A handler **added** during an emit runs in that **same** emit.
|
|
30
|
-
- A handler **removed** during an emit still runs for that emit (and any nested emit); the removal takes effect once the outermost emit completes.
|
|
30
|
+
- A handler **removed** during an emit still runs for that emit (and any nested emit); the removal takes effect once the outermost emit completes. Exception: a `once()` handler fires at most once, even under a nested emit.
|
|
31
31
|
- `emit()` is **re-entrant** (a handler may call `emit()` again) and **throw-safe**: a throwing handler propagates to the caller but never wedges deferred removal.
|
|
32
32
|
|
|
33
33
|
See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-engineevents--1kb-gzip).
|
package/dist/index.d.mts
CHANGED
|
@@ -10,6 +10,8 @@ type EventMap = Record<string, (...args: never[]) => void>;
|
|
|
10
10
|
* set is iterated live (no per-emit snapshot allocation).
|
|
11
11
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
12
12
|
* emit); the removal takes effect once the outermost emit completes.
|
|
13
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
14
|
+
* fires at most once, even under a nested emit.
|
|
13
15
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
14
16
|
* handler propagates to the caller but never wedges deferred removal.
|
|
15
17
|
*/
|
|
@@ -40,6 +42,8 @@ export declare class EventEmitter<E extends EventMap> {
|
|
|
40
42
|
* set is iterated live (no per-emit snapshot allocation).
|
|
41
43
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
42
44
|
* emit); the removal takes effect once the outermost emit completes.
|
|
45
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
46
|
+
* fires at most once, even under a nested emit.
|
|
43
47
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
44
48
|
* handler propagates to the caller but never wedges deferred removal.
|
|
45
49
|
*/
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";KACY;;KAGA,WAAW,mBAAmB
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";KACY;;KAGA,WAAW,mBAAmB;;;;;;;;;;;;;;qBAe7B,aAAa,UAAU;UAC1B;UACA;UACA;;EAGR,GAAG,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAahD,KAAK,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAelD,IAAI,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE;;;;;EAgB5C,KAAK,gBAAgB,GAAG,OAAO,MAAM,MAAM,WAAW,EAAE;UAkBhD;;EAaR,UAAU,cAAc;;;;;;;;;;;;;;;qBCrFb,OAAO;UACV;UACA;UACA;;EAGR,IAAI,UAAU,OAAO,aAAa;;EAQlC,KAAK,UAAU,OAAO,aAAa;;EAcnC,OAAO,UAAU,OAAO;;;;;EAYxB,QAAQ,MAAM,uBAAuB,OAAO;UAgBpC;;EAQR"}
|
package/dist/index.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ type EventMap = Record<string, (...args: never[]) => void>;
|
|
|
10
10
|
* set is iterated live (no per-emit snapshot allocation).
|
|
11
11
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
12
12
|
* emit); the removal takes effect once the outermost emit completes.
|
|
13
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
14
|
+
* fires at most once, even under a nested emit.
|
|
13
15
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
14
16
|
* handler propagates to the caller but never wedges deferred removal.
|
|
15
17
|
*/
|
|
@@ -40,6 +42,8 @@ export declare class EventEmitter<E extends EventMap> {
|
|
|
40
42
|
* set is iterated live (no per-emit snapshot allocation).
|
|
41
43
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
42
44
|
* emit); the removal takes effect once the outermost emit completes.
|
|
45
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
46
|
+
* fires at most once, even under a nested emit.
|
|
43
47
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
44
48
|
* handler propagates to the caller but never wedges deferred removal.
|
|
45
49
|
*/
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";KACY;;KAGA,WAAW,mBAAmB
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";KACY;;KAGA,WAAW,mBAAmB;;;;;;;;;;;;;;qBAe7B,aAAa,UAAU;UAC1B;UACA;UACA;;EAGR,GAAG,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAahD,KAAK,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAelD,IAAI,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE;;;;;EAgB5C,KAAK,gBAAgB,GAAG,OAAO,MAAM,MAAM,WAAW,EAAE;UAkBhD;;EAaR,UAAU,cAAc;;;;;;;;;;;;;;;qBCrFb,OAAO;UACV;UACA;UACA;;EAGR,IAAI,UAAU,OAAO,aAAa;;EAQlC,KAAK,UAAU,OAAO,aAAa;;EAcnC,OAAO,UAAU,OAAO;;;;;EAYxB,QAAQ,MAAM,uBAAuB,OAAO;UAgBpC;;EAQR"}
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,8 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
8
8
|
* set is iterated live (no per-emit snapshot allocation).
|
|
9
9
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
10
10
|
* emit); the removal takes effect once the outermost emit completes.
|
|
11
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
12
|
+
* fires at most once, even under a nested emit.
|
|
11
13
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
12
14
|
* handler propagates to the caller but never wedges deferred removal.
|
|
13
15
|
*/
|
|
@@ -29,7 +31,10 @@ var EventEmitter = class {
|
|
|
29
31
|
}
|
|
30
32
|
/** Subscribe to an event once. Returns unsubscribe function. */
|
|
31
33
|
once(event, handler) {
|
|
34
|
+
let fired = false;
|
|
32
35
|
const wrapper = ((...args) => {
|
|
36
|
+
if (fired) return;
|
|
37
|
+
fired = true;
|
|
33
38
|
unsub();
|
|
34
39
|
handler(...args);
|
|
35
40
|
});
|
|
@@ -96,6 +101,8 @@ var EventEmitter = class {
|
|
|
96
101
|
* set is iterated live (no per-emit snapshot allocation).
|
|
97
102
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
98
103
|
* emit); the removal takes effect once the outermost emit completes.
|
|
104
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
105
|
+
* fires at most once, even under a nested emit.
|
|
99
106
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
100
107
|
* handler propagates to the caller but never wedges deferred removal.
|
|
101
108
|
*/
|
|
@@ -112,7 +119,10 @@ var Signal = class {
|
|
|
112
119
|
}
|
|
113
120
|
/** Subscribe once. Returns unsubscribe function. */
|
|
114
121
|
once(handler) {
|
|
122
|
+
let fired = false;
|
|
115
123
|
const wrapper = (value) => {
|
|
124
|
+
if (fired) return;
|
|
125
|
+
fired = true;
|
|
116
126
|
unsub();
|
|
117
127
|
handler(value);
|
|
118
128
|
};
|
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 * - `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 const wrapper = ((...args: never[]) => {\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 * - `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 const wrapper = (value: T) => {\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":"
|
|
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/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,KAA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M,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"}
|
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
7
7
|
* set is iterated live (no per-emit snapshot allocation).
|
|
8
8
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
9
9
|
* emit); the removal takes effect once the outermost emit completes.
|
|
10
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
11
|
+
* fires at most once, even under a nested emit.
|
|
10
12
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
11
13
|
* handler propagates to the caller but never wedges deferred removal.
|
|
12
14
|
*/
|
|
@@ -28,7 +30,10 @@ var EventEmitter = class {
|
|
|
28
30
|
}
|
|
29
31
|
/** Subscribe to an event once. Returns unsubscribe function. */
|
|
30
32
|
once(event, handler) {
|
|
33
|
+
let fired = false;
|
|
31
34
|
const wrapper = ((...args) => {
|
|
35
|
+
if (fired) return;
|
|
36
|
+
fired = true;
|
|
32
37
|
unsub();
|
|
33
38
|
handler(...args);
|
|
34
39
|
});
|
|
@@ -95,6 +100,8 @@ var EventEmitter = class {
|
|
|
95
100
|
* set is iterated live (no per-emit snapshot allocation).
|
|
96
101
|
* - A handler **removed** during an emit still runs for that emit (and any nested
|
|
97
102
|
* emit); the removal takes effect once the outermost emit completes.
|
|
103
|
+
* Exception: a `once` handler is guarded independently of deferred removal and
|
|
104
|
+
* fires at most once, even under a nested emit.
|
|
98
105
|
* - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
|
|
99
106
|
* handler propagates to the caller but never wedges deferred removal.
|
|
100
107
|
*/
|
|
@@ -111,7 +118,10 @@ var Signal = class {
|
|
|
111
118
|
}
|
|
112
119
|
/** Subscribe once. Returns unsubscribe function. */
|
|
113
120
|
once(handler) {
|
|
121
|
+
let fired = false;
|
|
114
122
|
const wrapper = (value) => {
|
|
123
|
+
if (fired) return;
|
|
124
|
+
fired = true;
|
|
115
125
|
unsub();
|
|
116
126
|
handler(value);
|
|
117
127
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -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 * - `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 const wrapper = ((...args: never[]) => {\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 * - `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 const wrapper = (value: T) => {\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":"
|
|
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;GAC1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