@flare-engine/events 0.2.1 → 0.3.0

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/LICENSE CHANGED
@@ -1,21 +1,201 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 Grzegorz Otto
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Grzegorz Otto
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ Flare Engine
2
+ Copyright 2026 Grzegorz Otto
package/README.md CHANGED
@@ -13,7 +13,7 @@ bun add @flare-engine/events
13
13
  ```ts
14
14
  import { EventEmitter, Signal } from '@flare-engine/events';
15
15
 
16
- type GameEvents = { 'enemy-killed': { id: number; score: number } };
16
+ type GameEvents = { 'enemy-killed': (e: { id: number; score: number }) => void };
17
17
  const bus = new EventEmitter<GameEvents>();
18
18
  const off = bus.on('enemy-killed', (e) => console.log(e.score));
19
19
 
@@ -22,8 +22,16 @@ onHit.add((dmg) => console.log(dmg));
22
22
  onHit.emit(10);
23
23
  ```
24
24
 
25
- See [API spec](../../docs/design/api-specs.md#flare-engineevents--1kb-gzip).
25
+ ## Emit semantics
26
+
27
+ `emit()` iterates the handler set **live** (no per-emit snapshot — this is a hot path):
28
+
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.
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
+
33
+ See [API spec](https://github.com/grzott/flare-engine/blob/main/docs/design/api-specs.md#flare-engineevents--1kb-gzip).
26
34
 
27
35
  ## License
28
36
 
29
- MIT
37
+ Apache-2.0
package/dist/index.d.mts CHANGED
@@ -1,39 +1,70 @@
1
+ //#region src/emitter.d.ts
1
2
  /** Unsubscribe function returned by on/once. */
2
3
  type Unsubscribe = () => void;
3
4
  /** Map of event names to handler signatures. */
4
5
  type EventMap = Record<string, (...args: never[]) => void>;
5
- /** Type-safe event emitter. */
6
+ /**
7
+ * Type-safe event emitter.
8
+ *
9
+ * Emit semantics (shared with Signal):
10
+ * - A handler **added** during an emit runs in that **same** emit — the listener
11
+ * set is iterated live (no per-emit snapshot allocation).
12
+ * - A handler **removed** during an emit still runs for that emit (and any nested
13
+ * emit); the removal takes effect once the outermost emit completes.
14
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
15
+ * handler propagates to the caller but never wedges deferred removal.
16
+ */
6
17
  declare class EventEmitter<E extends EventMap> {
7
- private listeners;
8
- private iterating;
9
- private pendingRemove;
10
- /** Subscribe to an event. Returns unsubscribe function. */
11
- on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
12
- /** Subscribe to an event once. Returns unsubscribe function. */
13
- once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
14
- /** Unsubscribe a specific handler. */
15
- off<K extends keyof E>(event: K, handler: E[K]): void;
16
- /** Emit an event to all subscribers. Zero-alloc on the hot path. */
17
- emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void;
18
- /** Remove all listeners for a specific event, or all listeners. */
19
- removeAll(event?: keyof E): void;
18
+ private listeners;
19
+ private emitDepth;
20
+ private pendingRemove;
21
+ /** Subscribe to an event. Returns unsubscribe function. */
22
+ on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
23
+ /** Subscribe to an event once. Returns unsubscribe function. */
24
+ once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
25
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
26
+ off<K extends keyof E>(event: K, handler: E[K]): void;
27
+ /**
28
+ * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and
29
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
30
+ */
31
+ emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void;
32
+ private flushPending;
33
+ /** Remove all listeners for a specific event, or all listeners. */
34
+ removeAll(event?: keyof E): void;
20
35
  }
21
-
22
- /** Lightweight single-event emitter. Less overhead than EventEmitter. */
36
+ //#endregion
37
+ //#region src/signal.d.ts
38
+ /**
39
+ * Lightweight single-event emitter. Less overhead than EventEmitter.
40
+ *
41
+ * Emit semantics (shared with EventEmitter):
42
+ * - A handler **added** during an emit runs in that **same** emit — the handler
43
+ * set is iterated live (no per-emit snapshot allocation).
44
+ * - A handler **removed** during an emit still runs for that emit (and any nested
45
+ * emit); the removal takes effect once the outermost emit completes.
46
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
47
+ * handler propagates to the caller but never wedges deferred removal.
48
+ */
23
49
  declare class Signal<T = void> {
24
- private handlers;
25
- private iterating;
26
- private pendingRemove;
27
- /** Subscribe to the signal. Returns unsubscribe function. */
28
- add(handler: (value: T) => void): Unsubscribe;
29
- /** Subscribe once. Returns unsubscribe function. */
30
- once(handler: (value: T) => void): Unsubscribe;
31
- /** Unsubscribe a specific handler. */
32
- remove(handler: (value: T) => void): void;
33
- /** Emit a value to all subscribers. Zero-alloc on the hot path. */
34
- emit(...args: T extends void ? [] : [value: T]): void;
35
- /** Remove all handlers. */
36
- clear(): void;
50
+ private handlers;
51
+ private emitDepth;
52
+ private pendingRemove;
53
+ /** Subscribe to the signal. Returns unsubscribe function. */
54
+ add(handler: (value: T) => void): Unsubscribe;
55
+ /** Subscribe once. Returns unsubscribe function. */
56
+ once(handler: (value: T) => void): Unsubscribe;
57
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
58
+ remove(handler: (value: T) => void): void;
59
+ /**
60
+ * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and
61
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
62
+ */
63
+ emit(...args: T extends void ? [] : [value: T]): void;
64
+ private flushPending;
65
+ /** Remove all handlers. */
66
+ clear(): void;
37
67
  }
38
-
68
+ //#endregion
39
69
  export { EventEmitter, type EventMap, Signal, type Unsubscribe };
70
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";;KACY;;KAGA,WAAW,mBAAmB;;;;;;;;;;;;cAa7B,aAAa,UAAU;UAC1B;UACA;UACA;;EAGR,GAAG,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAahD,KAAK,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAWlD,IAAI,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE;;;;;EAgB5C,KAAK,gBAAgB,GAAG,OAAO,MAAM,MAAM,WAAW,EAAE;UAkBhD;;EAaR,UAAU,cAAc;;;;;;;;;;;;;;;cCjFb,OAAO;UACV;UACA;UACA;;EAGR,IAAI,UAAU,OAAO,aAAa;;EAQlC,KAAK,UAAU,OAAO,aAAa;;EAUnC,OAAO,UAAU,OAAO;;;;;EAYxB,QAAQ,MAAM,uBAAuB,OAAO;UAgBpC;;EAQR"}
package/dist/index.d.ts CHANGED
@@ -1,39 +1,70 @@
1
+ //#region src/emitter.d.ts
1
2
  /** Unsubscribe function returned by on/once. */
2
3
  type Unsubscribe = () => void;
3
4
  /** Map of event names to handler signatures. */
4
5
  type EventMap = Record<string, (...args: never[]) => void>;
5
- /** Type-safe event emitter. */
6
+ /**
7
+ * Type-safe event emitter.
8
+ *
9
+ * Emit semantics (shared with Signal):
10
+ * - A handler **added** during an emit runs in that **same** emit — the listener
11
+ * set is iterated live (no per-emit snapshot allocation).
12
+ * - A handler **removed** during an emit still runs for that emit (and any nested
13
+ * emit); the removal takes effect once the outermost emit completes.
14
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
15
+ * handler propagates to the caller but never wedges deferred removal.
16
+ */
6
17
  declare class EventEmitter<E extends EventMap> {
7
- private listeners;
8
- private iterating;
9
- private pendingRemove;
10
- /** Subscribe to an event. Returns unsubscribe function. */
11
- on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
12
- /** Subscribe to an event once. Returns unsubscribe function. */
13
- once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
14
- /** Unsubscribe a specific handler. */
15
- off<K extends keyof E>(event: K, handler: E[K]): void;
16
- /** Emit an event to all subscribers. Zero-alloc on the hot path. */
17
- emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void;
18
- /** Remove all listeners for a specific event, or all listeners. */
19
- removeAll(event?: keyof E): void;
18
+ private listeners;
19
+ private emitDepth;
20
+ private pendingRemove;
21
+ /** Subscribe to an event. Returns unsubscribe function. */
22
+ on<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
23
+ /** Subscribe to an event once. Returns unsubscribe function. */
24
+ once<K extends keyof E>(event: K, handler: E[K]): Unsubscribe;
25
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
26
+ off<K extends keyof E>(event: K, handler: E[K]): void;
27
+ /**
28
+ * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and
29
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
30
+ */
31
+ emit<K extends keyof E>(event: K, ...args: Parameters<E[K]>): void;
32
+ private flushPending;
33
+ /** Remove all listeners for a specific event, or all listeners. */
34
+ removeAll(event?: keyof E): void;
20
35
  }
21
-
22
- /** Lightweight single-event emitter. Less overhead than EventEmitter. */
36
+ //#endregion
37
+ //#region src/signal.d.ts
38
+ /**
39
+ * Lightweight single-event emitter. Less overhead than EventEmitter.
40
+ *
41
+ * Emit semantics (shared with EventEmitter):
42
+ * - A handler **added** during an emit runs in that **same** emit — the handler
43
+ * set is iterated live (no per-emit snapshot allocation).
44
+ * - A handler **removed** during an emit still runs for that emit (and any nested
45
+ * emit); the removal takes effect once the outermost emit completes.
46
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
47
+ * handler propagates to the caller but never wedges deferred removal.
48
+ */
23
49
  declare class Signal<T = void> {
24
- private handlers;
25
- private iterating;
26
- private pendingRemove;
27
- /** Subscribe to the signal. Returns unsubscribe function. */
28
- add(handler: (value: T) => void): Unsubscribe;
29
- /** Subscribe once. Returns unsubscribe function. */
30
- once(handler: (value: T) => void): Unsubscribe;
31
- /** Unsubscribe a specific handler. */
32
- remove(handler: (value: T) => void): void;
33
- /** Emit a value to all subscribers. Zero-alloc on the hot path. */
34
- emit(...args: T extends void ? [] : [value: T]): void;
35
- /** Remove all handlers. */
36
- clear(): void;
50
+ private handlers;
51
+ private emitDepth;
52
+ private pendingRemove;
53
+ /** Subscribe to the signal. Returns unsubscribe function. */
54
+ add(handler: (value: T) => void): Unsubscribe;
55
+ /** Subscribe once. Returns unsubscribe function. */
56
+ once(handler: (value: T) => void): Unsubscribe;
57
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
58
+ remove(handler: (value: T) => void): void;
59
+ /**
60
+ * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and
61
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
62
+ */
63
+ emit(...args: T extends void ? [] : [value: T]): void;
64
+ private flushPending;
65
+ /** Remove all handlers. */
66
+ clear(): void;
37
67
  }
38
-
68
+ //#endregion
39
69
  export { EventEmitter, type EventMap, Signal, type Unsubscribe };
70
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/emitter.ts","../src/signal.ts"],"mappings":";;KACY;;KAGA,WAAW,mBAAmB;;;;;;;;;;;;cAa7B,aAAa,UAAU;UAC1B;UACA;UACA;;EAGR,GAAG,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAahD,KAAK,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE,KAAK;;EAWlD,IAAI,gBAAgB,GAAG,OAAO,GAAG,SAAS,EAAE;;;;;EAgB5C,KAAK,gBAAgB,GAAG,OAAO,MAAM,MAAM,WAAW,EAAE;UAkBhD;;EAaR,UAAU,cAAc;;;;;;;;;;;;;;;cCjFb,OAAO;UACV;UACA;UACA;;EAGR,IAAI,UAAU,OAAO,aAAa;;EAQlC,KAAK,UAAU,OAAO,aAAa;;EAUnC,OAAO,UAAU,OAAO;;;;;EAYxB,QAAQ,MAAM,uBAAuB,OAAO;UAgBpC;;EAQR"}
package/dist/index.js CHANGED
@@ -1,155 +1,156 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- EventEmitter: () => EventEmitter,
24
- Signal: () => Signal
25
- });
26
- module.exports = __toCommonJS(index_exports);
27
-
28
- // src/emitter.ts
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/emitter.ts
3
+ /**
4
+ * Type-safe event emitter.
5
+ *
6
+ * Emit semantics (shared with Signal):
7
+ * - A handler **added** during an emit runs in that **same** emit — the listener
8
+ * set is iterated live (no per-emit snapshot allocation).
9
+ * - A handler **removed** during an emit still runs for that emit (and any nested
10
+ * emit); the removal takes effect once the outermost emit completes.
11
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
12
+ * handler propagates to the caller but never wedges deferred removal.
13
+ */
29
14
  var EventEmitter = class {
30
- listeners = /* @__PURE__ */ new Map();
31
- iterating = false;
32
- pendingRemove = [];
33
- /** Subscribe to an event. Returns unsubscribe function. */
34
- on(event, handler) {
35
- let set = this.listeners.get(event);
36
- if (!set) {
37
- set = /* @__PURE__ */ new Set();
38
- this.listeners.set(event, set);
39
- }
40
- set.add(handler);
41
- return () => {
42
- this.off(event, handler);
43
- };
44
- }
45
- /** Subscribe to an event once. Returns unsubscribe function. */
46
- once(event, handler) {
47
- const wrapper = ((...args) => {
48
- unsub();
49
- handler(...args);
50
- });
51
- const unsub = this.on(event, wrapper);
52
- return unsub;
53
- }
54
- /** Unsubscribe a specific handler. */
55
- off(event, handler) {
56
- if (this.iterating) {
57
- this.pendingRemove.push({ event, handler });
58
- return;
59
- }
60
- const set = this.listeners.get(event);
61
- if (set) {
62
- set.delete(handler);
63
- if (set.size === 0) this.listeners.delete(event);
64
- }
65
- }
66
- /** Emit an event to all subscribers. Zero-alloc on the hot path. */
67
- emit(event, ...args) {
68
- const set = this.listeners.get(event);
69
- if (!set) return;
70
- this.iterating = true;
71
- for (const handler of set) {
72
- handler(...args);
73
- }
74
- this.iterating = false;
75
- if (this.pendingRemove.length > 0) {
76
- for (let i = 0; i < this.pendingRemove.length; i++) {
77
- const { event: ev, handler: h } = this.pendingRemove[i];
78
- const s = this.listeners.get(ev);
79
- if (s) {
80
- s.delete(h);
81
- if (s.size === 0) this.listeners.delete(ev);
82
- }
83
- }
84
- this.pendingRemove.length = 0;
85
- }
86
- }
87
- /** Remove all listeners for a specific event, or all listeners. */
88
- removeAll(event) {
89
- if (event !== void 0) {
90
- this.listeners.delete(event);
91
- } else {
92
- this.listeners.clear();
93
- }
94
- this.pendingRemove.length = 0;
95
- this.iterating = false;
96
- }
15
+ listeners = /* @__PURE__ */ new Map();
16
+ emitDepth = 0;
17
+ pendingRemove = [];
18
+ /** Subscribe to an event. Returns unsubscribe function. */
19
+ on(event, handler) {
20
+ let set = this.listeners.get(event);
21
+ if (!set) {
22
+ set = /* @__PURE__ */ new Set();
23
+ this.listeners.set(event, set);
24
+ }
25
+ set.add(handler);
26
+ return () => {
27
+ this.off(event, handler);
28
+ };
29
+ }
30
+ /** Subscribe to an event once. Returns unsubscribe function. */
31
+ once(event, handler) {
32
+ const wrapper = ((...args) => {
33
+ unsub();
34
+ handler(...args);
35
+ });
36
+ const unsub = this.on(event, wrapper);
37
+ return unsub;
38
+ }
39
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
40
+ off(event, handler) {
41
+ if (this.emitDepth > 0) {
42
+ this.pendingRemove.push({
43
+ event,
44
+ handler
45
+ });
46
+ return;
47
+ }
48
+ const set = this.listeners.get(event);
49
+ if (set) {
50
+ set.delete(handler);
51
+ if (set.size === 0) this.listeners.delete(event);
52
+ }
53
+ }
54
+ /**
55
+ * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and
56
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
57
+ */
58
+ emit(event, ...args) {
59
+ const set = this.listeners.get(event);
60
+ if (!set) return;
61
+ this.emitDepth++;
62
+ try {
63
+ for (const handler of set) handler(...args);
64
+ } finally {
65
+ this.emitDepth--;
66
+ if (this.emitDepth === 0 && this.pendingRemove.length > 0) this.flushPending();
67
+ }
68
+ }
69
+ flushPending() {
70
+ for (let i = 0; i < this.pendingRemove.length; i++) {
71
+ const { event: ev, handler: h } = this.pendingRemove[i];
72
+ const s = this.listeners.get(ev);
73
+ if (s) {
74
+ s.delete(h);
75
+ if (s.size === 0) this.listeners.delete(ev);
76
+ }
77
+ }
78
+ this.pendingRemove.length = 0;
79
+ }
80
+ /** Remove all listeners for a specific event, or all listeners. */
81
+ removeAll(event) {
82
+ if (event !== void 0) {
83
+ this.listeners.delete(event);
84
+ return;
85
+ }
86
+ this.listeners.clear();
87
+ this.pendingRemove.length = 0;
88
+ }
97
89
  };
98
-
99
- // src/signal.ts
90
+ //#endregion
91
+ //#region src/signal.ts
92
+ /**
93
+ * Lightweight single-event emitter. Less overhead than EventEmitter.
94
+ *
95
+ * Emit semantics (shared with EventEmitter):
96
+ * - A handler **added** during an emit runs in that **same** emit — the handler
97
+ * set is iterated live (no per-emit snapshot allocation).
98
+ * - A handler **removed** during an emit still runs for that emit (and any nested
99
+ * emit); the removal takes effect once the outermost emit completes.
100
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
101
+ * handler propagates to the caller but never wedges deferred removal.
102
+ */
100
103
  var Signal = class {
101
- handlers = /* @__PURE__ */ new Set();
102
- iterating = false;
103
- pendingRemove = [];
104
- /** Subscribe to the signal. Returns unsubscribe function. */
105
- add(handler) {
106
- this.handlers.add(handler);
107
- return () => {
108
- this.remove(handler);
109
- };
110
- }
111
- /** Subscribe once. Returns unsubscribe function. */
112
- once(handler) {
113
- const wrapper = (value) => {
114
- unsub();
115
- handler(value);
116
- };
117
- const unsub = this.add(wrapper);
118
- return unsub;
119
- }
120
- /** Unsubscribe a specific handler. */
121
- remove(handler) {
122
- if (this.iterating) {
123
- this.pendingRemove.push(handler);
124
- } else {
125
- this.handlers.delete(handler);
126
- }
127
- }
128
- /** Emit a value to all subscribers. Zero-alloc on the hot path. */
129
- emit(...args) {
130
- const value = args[0];
131
- this.iterating = true;
132
- for (const handler of this.handlers) {
133
- handler(value);
134
- }
135
- this.iterating = false;
136
- if (this.pendingRemove.length > 0) {
137
- for (let i = 0; i < this.pendingRemove.length; i++) {
138
- this.handlers.delete(this.pendingRemove[i]);
139
- }
140
- this.pendingRemove.length = 0;
141
- }
142
- }
143
- /** Remove all handlers. */
144
- clear() {
145
- this.handlers.clear();
146
- this.pendingRemove.length = 0;
147
- this.iterating = false;
148
- }
104
+ handlers = /* @__PURE__ */ new Set();
105
+ emitDepth = 0;
106
+ pendingRemove = [];
107
+ /** Subscribe to the signal. Returns unsubscribe function. */
108
+ add(handler) {
109
+ this.handlers.add(handler);
110
+ return () => {
111
+ this.remove(handler);
112
+ };
113
+ }
114
+ /** Subscribe once. Returns unsubscribe function. */
115
+ once(handler) {
116
+ const wrapper = (value) => {
117
+ unsub();
118
+ handler(value);
119
+ };
120
+ const unsub = this.add(wrapper);
121
+ return unsub;
122
+ }
123
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
124
+ remove(handler) {
125
+ if (this.emitDepth > 0) this.pendingRemove.push(handler);
126
+ else this.handlers.delete(handler);
127
+ }
128
+ /**
129
+ * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and
130
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
131
+ */
132
+ emit(...args) {
133
+ const value = args[0];
134
+ this.emitDepth++;
135
+ try {
136
+ for (const handler of this.handlers) handler(value);
137
+ } finally {
138
+ this.emitDepth--;
139
+ if (this.emitDepth === 0 && this.pendingRemove.length > 0) this.flushPending();
140
+ }
141
+ }
142
+ flushPending() {
143
+ for (let i = 0; i < this.pendingRemove.length; i++) this.handlers.delete(this.pendingRemove[i]);
144
+ this.pendingRemove.length = 0;
145
+ }
146
+ /** Remove all handlers. */
147
+ clear() {
148
+ this.handlers.clear();
149
+ this.pendingRemove.length = 0;
150
+ }
149
151
  };
150
- // Annotate the CommonJS export names for ESM import in node:
151
- 0 && (module.exports = {
152
- EventEmitter,
153
- Signal
154
- });
152
+ //#endregion
153
+ exports.EventEmitter = EventEmitter;
154
+ exports.Signal = Signal;
155
+
155
156
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/emitter.ts","../src/signal.ts"],"sourcesContent":["export { EventEmitter } from './emitter.js';\nexport type { Unsubscribe, EventMap } from './emitter.js';\nexport { Signal } from './signal.js';\n","/** 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/** Type-safe event emitter. */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private iterating = false;\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. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.iterating) {\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 /** Emit an event to all subscribers. Zero-alloc on the hot path. */\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.iterating = true;\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n this.iterating = false;\n // Flush pending removes (rare path — only when handler unsubscribed during emit)\n if (this.pendingRemove.length > 0) {\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\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 } else {\n this.listeners.clear();\n }\n this.pendingRemove.length = 0;\n this.iterating = false;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/** Lightweight single-event emitter. Less overhead than EventEmitter. */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private iterating = false;\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. */\n remove(handler: (value: T) => void): void {\n if (this.iterating) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /** Emit a value to all subscribers. Zero-alloc on the hot path. */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.iterating = true;\n for (const handler of this.handlers) {\n handler(value);\n }\n this.iterating = false;\n // Flush pending removes (rare path — only when handler removed itself during emit)\n if (this.pendingRemove.length > 0) {\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\n /** Remove all handlers. */\n clear(): void {\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Y;AAEjB,QAAI,KAAK,cAAc,SAAS,GAAG;AACjC,eAAS,IAAI,GAAG,IAAI,KAAK,cAAc,QAAQ,KAAK;AAClD,aAAK,SAAS,OAAO,KAAK,cAAc,CAAC,CAAE;AAAA,MAC7C;AACA,WAAK,cAAc,SAAS;AAAA,IAC9B;AAAA,EACF;AAAA;AAAA,EAGA,QAAc;AACZ,SAAK,SAAS,MAAM;AACpB,SAAK,cAAc,SAAS;AAC5B,SAAK,YAAY;AAAA,EACnB;AACF;","names":[]}
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":";;;;;;;;;;;;;AAiBA,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,O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,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,MAAM,WAAW,UAAa;GAC5B,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"}
package/dist/index.mjs CHANGED
@@ -1,127 +1,154 @@
1
- // src/emitter.ts
1
+ //#region src/emitter.ts
2
+ /**
3
+ * Type-safe event emitter.
4
+ *
5
+ * Emit semantics (shared with Signal):
6
+ * - A handler **added** during an emit runs in that **same** emit — the listener
7
+ * set is iterated live (no per-emit snapshot allocation).
8
+ * - A handler **removed** during an emit still runs for that emit (and any nested
9
+ * emit); the removal takes effect once the outermost emit completes.
10
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
11
+ * handler propagates to the caller but never wedges deferred removal.
12
+ */
2
13
  var EventEmitter = class {
3
- listeners = /* @__PURE__ */ new Map();
4
- iterating = false;
5
- pendingRemove = [];
6
- /** Subscribe to an event. Returns unsubscribe function. */
7
- on(event, handler) {
8
- let set = this.listeners.get(event);
9
- if (!set) {
10
- set = /* @__PURE__ */ new Set();
11
- this.listeners.set(event, set);
12
- }
13
- set.add(handler);
14
- return () => {
15
- this.off(event, handler);
16
- };
17
- }
18
- /** Subscribe to an event once. Returns unsubscribe function. */
19
- once(event, handler) {
20
- const wrapper = ((...args) => {
21
- unsub();
22
- handler(...args);
23
- });
24
- const unsub = this.on(event, wrapper);
25
- return unsub;
26
- }
27
- /** Unsubscribe a specific handler. */
28
- off(event, handler) {
29
- if (this.iterating) {
30
- this.pendingRemove.push({ event, handler });
31
- return;
32
- }
33
- const set = this.listeners.get(event);
34
- if (set) {
35
- set.delete(handler);
36
- if (set.size === 0) this.listeners.delete(event);
37
- }
38
- }
39
- /** Emit an event to all subscribers. Zero-alloc on the hot path. */
40
- emit(event, ...args) {
41
- const set = this.listeners.get(event);
42
- if (!set) return;
43
- this.iterating = true;
44
- for (const handler of set) {
45
- handler(...args);
46
- }
47
- this.iterating = false;
48
- if (this.pendingRemove.length > 0) {
49
- for (let i = 0; i < this.pendingRemove.length; i++) {
50
- const { event: ev, handler: h } = this.pendingRemove[i];
51
- const s = this.listeners.get(ev);
52
- if (s) {
53
- s.delete(h);
54
- if (s.size === 0) this.listeners.delete(ev);
55
- }
56
- }
57
- this.pendingRemove.length = 0;
58
- }
59
- }
60
- /** Remove all listeners for a specific event, or all listeners. */
61
- removeAll(event) {
62
- if (event !== void 0) {
63
- this.listeners.delete(event);
64
- } else {
65
- this.listeners.clear();
66
- }
67
- this.pendingRemove.length = 0;
68
- this.iterating = false;
69
- }
14
+ listeners = /* @__PURE__ */ new Map();
15
+ emitDepth = 0;
16
+ pendingRemove = [];
17
+ /** Subscribe to an event. Returns unsubscribe function. */
18
+ on(event, handler) {
19
+ let set = this.listeners.get(event);
20
+ if (!set) {
21
+ set = /* @__PURE__ */ new Set();
22
+ this.listeners.set(event, set);
23
+ }
24
+ set.add(handler);
25
+ return () => {
26
+ this.off(event, handler);
27
+ };
28
+ }
29
+ /** Subscribe to an event once. Returns unsubscribe function. */
30
+ once(event, handler) {
31
+ const wrapper = ((...args) => {
32
+ unsub();
33
+ handler(...args);
34
+ });
35
+ const unsub = this.on(event, wrapper);
36
+ return unsub;
37
+ }
38
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
39
+ off(event, handler) {
40
+ if (this.emitDepth > 0) {
41
+ this.pendingRemove.push({
42
+ event,
43
+ handler
44
+ });
45
+ return;
46
+ }
47
+ const set = this.listeners.get(event);
48
+ if (set) {
49
+ set.delete(handler);
50
+ if (set.size === 0) this.listeners.delete(event);
51
+ }
52
+ }
53
+ /**
54
+ * Emit an event to all subscribers. Zero-alloc on the hot path; re-entrant and
55
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
56
+ */
57
+ emit(event, ...args) {
58
+ const set = this.listeners.get(event);
59
+ if (!set) return;
60
+ this.emitDepth++;
61
+ try {
62
+ for (const handler of set) handler(...args);
63
+ } finally {
64
+ this.emitDepth--;
65
+ if (this.emitDepth === 0 && this.pendingRemove.length > 0) this.flushPending();
66
+ }
67
+ }
68
+ flushPending() {
69
+ for (let i = 0; i < this.pendingRemove.length; i++) {
70
+ const { event: ev, handler: h } = this.pendingRemove[i];
71
+ const s = this.listeners.get(ev);
72
+ if (s) {
73
+ s.delete(h);
74
+ if (s.size === 0) this.listeners.delete(ev);
75
+ }
76
+ }
77
+ this.pendingRemove.length = 0;
78
+ }
79
+ /** Remove all listeners for a specific event, or all listeners. */
80
+ removeAll(event) {
81
+ if (event !== void 0) {
82
+ this.listeners.delete(event);
83
+ return;
84
+ }
85
+ this.listeners.clear();
86
+ this.pendingRemove.length = 0;
87
+ }
70
88
  };
71
-
72
- // src/signal.ts
89
+ //#endregion
90
+ //#region src/signal.ts
91
+ /**
92
+ * Lightweight single-event emitter. Less overhead than EventEmitter.
93
+ *
94
+ * Emit semantics (shared with EventEmitter):
95
+ * - A handler **added** during an emit runs in that **same** emit — the handler
96
+ * set is iterated live (no per-emit snapshot allocation).
97
+ * - A handler **removed** during an emit still runs for that emit (and any nested
98
+ * emit); the removal takes effect once the outermost emit completes.
99
+ * - `emit` is re-entrant (a handler may emit again) and throw-safe: a throwing
100
+ * handler propagates to the caller but never wedges deferred removal.
101
+ */
73
102
  var Signal = class {
74
- handlers = /* @__PURE__ */ new Set();
75
- iterating = false;
76
- pendingRemove = [];
77
- /** Subscribe to the signal. Returns unsubscribe function. */
78
- add(handler) {
79
- this.handlers.add(handler);
80
- return () => {
81
- this.remove(handler);
82
- };
83
- }
84
- /** Subscribe once. Returns unsubscribe function. */
85
- once(handler) {
86
- const wrapper = (value) => {
87
- unsub();
88
- handler(value);
89
- };
90
- const unsub = this.add(wrapper);
91
- return unsub;
92
- }
93
- /** Unsubscribe a specific handler. */
94
- remove(handler) {
95
- if (this.iterating) {
96
- this.pendingRemove.push(handler);
97
- } else {
98
- this.handlers.delete(handler);
99
- }
100
- }
101
- /** Emit a value to all subscribers. Zero-alloc on the hot path. */
102
- emit(...args) {
103
- const value = args[0];
104
- this.iterating = true;
105
- for (const handler of this.handlers) {
106
- handler(value);
107
- }
108
- this.iterating = false;
109
- if (this.pendingRemove.length > 0) {
110
- for (let i = 0; i < this.pendingRemove.length; i++) {
111
- this.handlers.delete(this.pendingRemove[i]);
112
- }
113
- this.pendingRemove.length = 0;
114
- }
115
- }
116
- /** Remove all handlers. */
117
- clear() {
118
- this.handlers.clear();
119
- this.pendingRemove.length = 0;
120
- this.iterating = false;
121
- }
122
- };
123
- export {
124
- EventEmitter,
125
- Signal
103
+ handlers = /* @__PURE__ */ new Set();
104
+ emitDepth = 0;
105
+ pendingRemove = [];
106
+ /** Subscribe to the signal. Returns unsubscribe function. */
107
+ add(handler) {
108
+ this.handlers.add(handler);
109
+ return () => {
110
+ this.remove(handler);
111
+ };
112
+ }
113
+ /** Subscribe once. Returns unsubscribe function. */
114
+ once(handler) {
115
+ const wrapper = (value) => {
116
+ unsub();
117
+ handler(value);
118
+ };
119
+ const unsub = this.add(wrapper);
120
+ return unsub;
121
+ }
122
+ /** Unsubscribe a specific handler. Deferred to the outermost emit if called during one. */
123
+ remove(handler) {
124
+ if (this.emitDepth > 0) this.pendingRemove.push(handler);
125
+ else this.handlers.delete(handler);
126
+ }
127
+ /**
128
+ * Emit a value to all subscribers. Zero-alloc on the hot path; re-entrant and
129
+ * throw-safe (see the class docstring for the add/remove-during-emit semantics).
130
+ */
131
+ emit(...args) {
132
+ const value = args[0];
133
+ this.emitDepth++;
134
+ try {
135
+ for (const handler of this.handlers) handler(value);
136
+ } finally {
137
+ this.emitDepth--;
138
+ if (this.emitDepth === 0 && this.pendingRemove.length > 0) this.flushPending();
139
+ }
140
+ }
141
+ flushPending() {
142
+ for (let i = 0; i < this.pendingRemove.length; i++) this.handlers.delete(this.pendingRemove[i]);
143
+ this.pendingRemove.length = 0;
144
+ }
145
+ /** Remove all handlers. */
146
+ clear() {
147
+ this.handlers.clear();
148
+ this.pendingRemove.length = 0;
149
+ }
126
150
  };
151
+ //#endregion
152
+ export { EventEmitter, Signal };
153
+
127
154
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"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/** Type-safe event emitter. */\nexport class EventEmitter<E extends EventMap> {\n private listeners = new Map<keyof E, Set<E[keyof E]>>();\n private iterating = false;\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. */\n off<K extends keyof E>(event: K, handler: E[K]): void {\n if (this.iterating) {\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 /** Emit an event to all subscribers. Zero-alloc on the hot path. */\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.iterating = true;\n for (const handler of set) {\n // biome-ignore lint/complexity/noBannedTypes: generic handler call\n (handler as Function)(...args);\n }\n this.iterating = false;\n // Flush pending removes (rare path — only when handler unsubscribed during emit)\n if (this.pendingRemove.length > 0) {\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\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 } else {\n this.listeners.clear();\n }\n this.pendingRemove.length = 0;\n this.iterating = false;\n }\n}\n","import type { Unsubscribe } from './emitter.js';\n\n/** Lightweight single-event emitter. Less overhead than EventEmitter. */\nexport class Signal<T = void> {\n private handlers = new Set<(value: T) => void>();\n private iterating = false;\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. */\n remove(handler: (value: T) => void): void {\n if (this.iterating) {\n this.pendingRemove.push(handler);\n } else {\n this.handlers.delete(handler);\n }\n }\n\n /** Emit a value to all subscribers. Zero-alloc on the hot path. */\n emit(...args: T extends void ? [] : [value: T]): void {\n const value = args[0] as T;\n this.iterating = true;\n for (const handler of this.handlers) {\n handler(value);\n }\n this.iterating = false;\n // Flush pending removes (rare path — only when handler removed itself during emit)\n if (this.pendingRemove.length > 0) {\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\n /** Remove all handlers. */\n clear(): void {\n this.handlers.clear();\n this.pendingRemove.length = 0;\n this.iterating = false;\n }\n}\n"],"mappings":";AAOO,IAAM,eAAN,MAAuC;AAAA,EACpC,YAAY,oBAAI,IAA8B;AAAA,EAC9C,YAAY;AAAA,EACZ,gBAAgE,CAAC;AAAA;AAAA,EAGzE,GAAsB,OAAU,SAA4B;AAC1D,QAAI,MAAM,KAAK,UAAU,IAAI,KAAK;AAClC,QAAI,CAAC,KAAK;AACR,YAAM,oBAAI,IAAI;AACd,WAAK,UAAU,IAAI,OAAO,GAAG;AAAA,IAC/B;AACA,QAAI,IAAI,OAAO;AACf,WAAO,MAAM;AACX,WAAK,IAAI,OAAO,OAAO;AAAA,IACzB;AAAA,EACF;AAAA;AAAA,EAGA,KAAwB,OAAU,SAA4B;AAC5D,UAAM,WAAW,IAAI,SAAkB;AACrC,YAAM;AAEN,MAAC,QAAqB,GAAG,IAAI;AAAA,IAC/B;AACA,UAAM,QAAQ,KAAK,GAAG,OAAO,OAAO;AACpC,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,IAAuB,OAAU,SAAqB;AACpD,QAAI,KAAK,WAAW;AAClB,WAAK,cAAc,KAAK,EAAE,OA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AAC7C,UAAM,UAAU,CAAC,UAAa;AAC5B,YAAM;AACN,cAAQ,KAAK;AAAA,IACf;AACA,UAAM,QAAQ,KAAK,IAAI,OAAO;AAC9B,WAAO;AAAA,EACT;AAAA;AAAA,EAGA,OAAO,SAAmC;AACxC,QAAI,KAAK,WAAW;AAClB,WAAK,cAAc,KAAK,OAAO;AAAA,IACjC,OAAO;AACL,WAAK,SAAS,OAAO,OAAO;AAAA,IAC9B;AAAA,EACF;AAAA;AAAA,EAGA,QAAQ,MAA8C;AACpD,UAAM,QAAQ,KAAK,CAAC;AACpB,SAAK,YAAY;AACjB,eAAW,WAAW,KAAK,UAAU;AACnC,cAAQ,KAAK;AAAA,IACf;AACA,SAAK,YAAY;AAEjB,QAAI,KAAK,cAAc,SAAS,GAAG;AACjC,eAAS,IAAI,GAAG,IAAI,KAAK,cAAc,QAAQ,KAAK;AAClD,aAAK,SAAS,OAAO,KAAK,cAAc,CAAC,CAAE;AAAA,MAC7C;AACA,WAAK,cAAc,SAAS;AAAA,IAC9B;AAAA,EACF;AAAA;AAAA,EAGA,QAAc;AACZ,SAAK,SAAS,MAAM;AACpB,SAAK,cAAc,SAAS;AAC5B,SAAK,YAAY;AAAA,EACnB;AACF;","names":[]}
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":";;;;;;;;;;;;AAiBA,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,MAAM,YAAY,GAAG,SAAkB;GACrC,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,QAAsB,GAAG,IAAI;EAEjC,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,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A,QAAc;EACZ,KAAK,SAAS,MAAM;EACpB,KAAK,cAAc,SAAS;CAC9B;AACF"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@flare-engine/events",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "description": "Type-safe EventEmitter and Signal",
5
5
  "keywords": ["flare-engine", "events", "signal", "emitter"],
6
- "license": "MIT",
6
+ "license": "Apache-2.0",
7
7
  "author": "Grzegorz Otto",
8
8
  "homepage": "https://github.com/grzott/flare-engine#readme",
9
9
  "bugs": {
@@ -19,18 +19,26 @@
19
19
  "types": "dist/index.d.ts",
20
20
  "exports": {
21
21
  ".": {
22
- "types": "./dist/index.d.ts",
23
- "import": "./dist/index.mjs",
24
- "require": "./dist/index.js"
22
+ "import": {
23
+ "types": "./dist/index.d.mts",
24
+ "default": "./dist/index.mjs"
25
+ },
26
+ "require": {
27
+ "types": "./dist/index.d.ts",
28
+ "default": "./dist/index.js"
29
+ }
25
30
  }
26
31
  },
27
- "files": ["dist", "README.md", "LICENSE"],
32
+ "files": ["dist", "README.md", "LICENSE", "NOTICE"],
28
33
  "scripts": {
29
- "build": "tsup",
34
+ "build": "tsdown",
30
35
  "test": "bun test",
31
36
  "lint": "biome check src/",
32
37
  "typecheck": "tsc --noEmit"
33
38
  },
39
+ "engines": {
40
+ "node": ">=20"
41
+ },
34
42
  "publishConfig": {
35
43
  "access": "restricted",
36
44
  "registry": "https://registry.npmjs.org/"