signalcore 1.0.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 ADDED
@@ -0,0 +1,201 @@
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
174
+ reason 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] [limonikal]
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
200
+ implied. See the License for the specific language governing
201
+ permissions and limitations under the License.
package/README.md ADDED
@@ -0,0 +1,331 @@
1
+ # signalcore
2
+
3
+ Typed event-driven architecture for TypeScript. Zero runtime overhead. Full type safety.
4
+
5
+ ```ts
6
+ interface AppEvents {
7
+ login: { user: string };
8
+ }
9
+
10
+ const emitter = new Emitter<AppEvents>();
11
+
12
+ emitter.on("login", (t) => console.log(`Welcome, ${t.user}`));
13
+ // ^ action autocompleted, trigger.data fully typed
14
+
15
+ emitter.emit("login", { user: "Alice" });
16
+ // ^ only valid shape compiles
17
+ ```
18
+
19
+ ---
20
+
21
+ ## Why signalcore?
22
+
23
+ Most event libraries treat events as strings with `any` payload. You lose type information the moment you call `emit` or `on`. signalcore fixes this at the core: **every action is permanently linked to its data type**.
24
+
25
+ - **Full type safety** — `emit` with wrong data is a compile error. Handlers receive exactly the type declared for the action. No `any` leaks.
26
+ - **Tree-shakable** — import only what you use. ESM + CJS dual format.
27
+ - **Systematic approach** — not just an event emitter. A complete typed event-driven toolkit:
28
+ - `Emitter` — typed event bus
29
+ - `EventTargetEmitter` — bridge to DOM events
30
+ - `NodeEventEmitter` — bridge to Node.js EventEmitter
31
+ - `Union` — compose multiple emitters into one interface
32
+ - `Middleware` — intercept groups of actions
33
+ - `ProxyEmitter` — reactive objects: `proxy.x = v` → emit
34
+ - `Stor` — reactive store with custom comparators (no emit on equal values)
35
+ - `until` — async helper: `await until(emitter, "ready")`
36
+ - **Runtime-validatable** — every event trigger is `instanceof Trigger`, so you can check it at runtime.
37
+ - **Memory safe** — uses `WeakMap` for `once` wrappers, no timer leaks, no global caches.
38
+ - **Architectural freedom** — no framework lock-in. Works in browser, Node.js, or anywhere TypeScript runs.
39
+
40
+ ---
41
+
42
+ ## Quick example
43
+
44
+ ```ts
45
+ import { Emitter, until } from "signalcore";
46
+
47
+ type Events = {
48
+ ready: { data: string };
49
+ error: { code: number };
50
+ };
51
+
52
+ const bus = new Emitter<Events>();
53
+
54
+ // Subscribe with full autocomplete
55
+ bus.on("ready", ({ data, emitter }) => {
56
+ console.log(data); // string
57
+ emitter.emit("error", { code: 0 }); // typed emit from handler
58
+ });
59
+
60
+ // Async wait for exactly one emission
61
+ const trigger = await until(bus, "ready");
62
+
63
+ // Broadcast
64
+ bus.emit("ready", { data: "hello" });
65
+ ```
66
+
67
+ More examples are available in the [`examples/`](./examples) folder of the repository.
68
+
69
+ ---
70
+
71
+ ## Installation
72
+
73
+ ```sh
74
+ npm install signalcore
75
+ ```
76
+
77
+ ```sh
78
+ yarn add signalcore
79
+ ```
80
+
81
+ ```sh
82
+ pnpm add signalcore
83
+ ```
84
+
85
+ ```ts
86
+ // ESM
87
+ import { Emitter, Union, until } from "signalcore";
88
+
89
+ // CJS
90
+ const { Emitter, Union, until } = require("signalcore");
91
+ ```
92
+
93
+ ---
94
+
95
+ ## Documentation
96
+
97
+ ### Core: `Emitter<ActionTypes>`
98
+
99
+ The foundation. An `Emitter` maps action names to their data types.
100
+
101
+ ```ts
102
+ import { Emitter } from "signalcore";
103
+
104
+ type AppEvents = {
105
+ login: { user: string };
106
+ logout: {};
107
+ error: { code: number; message: string };
108
+ };
109
+
110
+ const e = new Emitter<AppEvents>();
111
+ ```
112
+
113
+ #### Methods
114
+
115
+ | Method | Signature | Description |
116
+ |---|---|---|
117
+ | `emit` | `(action, data) => boolean` | Creates a `Trigger`, calls all handlers. Returns `true` if any handler was called. |
118
+ | `on` | `(action, handler)` | Subscribe to an action. |
119
+ | `once` | `(action, handler)` | Subscribe for one emission, then auto-unsubscribe. |
120
+ | `off` | `(action, handler)` | Unsubscribe a specific handler. Handler is typed to the action. |
121
+ | `offAll` | `(action)` | Remove all handlers for an action. |
122
+ | `clear` | `()` | Remove all handlers for all actions. |
123
+ | `hasListeners` | `(action) => boolean` | Check if an action has subscribers. |
124
+ | `listenerCount` | `(action?) => number` | Count subscribers (for an action or total). |
125
+ | `actions` | `() => (keyof ActionTypes)[]` | List all actions that have subscribers. |
126
+
127
+ #### Error handling
128
+
129
+ One failing handler never breaks the chain. `emit` wraps each handler call in `try-catch`, logs the error to `console.error`, and continues to the next handler.
130
+
131
+ #### The `Trigger` object
132
+
133
+ When an action is emitted, each handler receives a `Trigger` — an intersection of:
134
+
135
+ ```ts
136
+ TriggerClass & ActionData & { emitter: Emitter<ActionTypes> }
137
+ ```
138
+
139
+ - `t instanceof Trigger` — runtime checkable
140
+ - `t.user`, `t.text`, ... — typed action data
141
+ - `t.emitter` — reference to the emitter (non-enumerable, does not appear in `JSON.stringify` or `for..in`)
142
+
143
+ ---
144
+
145
+ ### DOM bridge: `EventTargetEmitter<ActionTypes>`
146
+
147
+ Wraps a native `EventTarget` (DOM element, `window`, `document`) and bridges its events into the typed emitter system.
148
+
149
+ ```ts
150
+ import { EventTargetEmitter } from "signalcore";
151
+
152
+ const clicks = new EventTargetEmitter<{
153
+ click: MouseEvent;
154
+ submit: SubmitEvent;
155
+ }>(document);
156
+
157
+ clicks.on("click", (t) => {
158
+ t.clientX; // MouseEvent data
159
+ t.event; // original Event object
160
+ }, { capture: true });
161
+
162
+ // External signal for cleanup
163
+ const ctrl = new AbortController();
164
+ clicks.on("click", () => {}, { signal: ctrl.signal });
165
+ ctrl.abort();
166
+ ```
167
+
168
+ ---
169
+
170
+ ### Node.js bridge: `NodeEventEmitter<ActionTypes>`
171
+
172
+ Wraps a Node.js `EventEmitter` (or any object with `on`/`once`/`off`) into the typed system. Action data is a tuple of event arguments.
173
+
174
+ ```ts
175
+ import { EventEmitter } from "events";
176
+ import { NodeEventEmitter } from "signalcore";
177
+
178
+ const source = new EventEmitter();
179
+
180
+ const bridge = new NodeEventEmitter<{
181
+ data: [Buffer];
182
+ end: [];
183
+ error: [Error];
184
+ }>(source);
185
+
186
+ bridge.on("data", ([chunk]) => {
187
+ console.log(chunk.toString());
188
+ });
189
+ ```
190
+
191
+ ---
192
+
193
+ ### Composition: `Union<ActionTypes>`
194
+
195
+ Combines multiple emitters with the same `ActionTypes` into one interface. Supports dynamic `add` and `remove`.
196
+
197
+ ```ts
198
+ import { Emitter, Union } from "signalcore";
199
+
200
+ type Events = { tick: number };
201
+
202
+ const a = new Emitter<Events>();
203
+ const b = new Emitter<Events>();
204
+ const union = new Union(a, b);
205
+
206
+ union.on("tick", (t) => console.log(t));
207
+ a.emit("tick", 1); // fires
208
+ b.emit("tick", 2); // fires
209
+
210
+ const c = new Emitter<Events>();
211
+ union.add(c); // replays existing subscriptions onto c
212
+ union.remove(b); // unsubscribes b from all tracked handlers
213
+ ```
214
+
215
+ Union accepts any `EmitterLike` — including other Union instances. Unlimited nesting.
216
+
217
+ ```ts
218
+ const inner = new Union(e1, e2);
219
+ const outer = new Union(inner, e3); // nested Union
220
+ ```
221
+
222
+ ---
223
+
224
+ ### Middleware: `Middleware<ActionTypes, Actions>`
225
+
226
+ Attaches one handler to multiple actions. Useful for logging, metrics, or cross-cutting concerns.
227
+
228
+ ```ts
229
+ import { Emitter, Middleware } from "signalcore";
230
+
231
+ const bus = new Emitter<{ login: {}; logout: {} }>();
232
+
233
+ const logger = new Middleware(bus, ["login", "logout"], (t) => {
234
+ console.log("Action:", t);
235
+ });
236
+
237
+ bus.emit("login", {}); // logs
238
+
239
+ logger.destroy(); // unsubscribes from both actions
240
+ ```
241
+
242
+ ---
243
+
244
+ ### Reactive proxy: `createProxyEmitter<Data>`
245
+
246
+ Turns a plain object into a reactive proxy: property assignment automatically emits an event with `{ from, value }`.
247
+
248
+ ```ts
249
+ import { createProxyEmitter } from "signalcore";
250
+
251
+ const state = createProxyEmitter({ x: 0, y: 0 });
252
+
253
+ state.emitter.on("x", ({ from, value }) => {
254
+ console.log(`${from} → ${value}`);
255
+ });
256
+
257
+ state.x = 10; // emits "x" with { from: 0, value: 10 }
258
+ state.x = 20; // emits "x" with { from: 10, value: 20 }
259
+
260
+ // Properties cannot be deleted (type integrity)
261
+ delete state.x; // no-op, returns false
262
+
263
+ // Technical fields (emitter) are non-enumerable
264
+ JSON.stringify(state); // "{"x":20,"y":0}"
265
+ ```
266
+
267
+ ---
268
+
269
+ ### Reactive store: `createStor<Data>`
270
+
271
+ Like `createProxyEmitter`, but with custom comparators. Does not emit if the new value equals the old one by your comparator.
272
+
273
+ ```ts
274
+ import { createStor } from "signalcore";
275
+
276
+ const store = createStor(
277
+ { user: { name: "Alice" }, count: 0 },
278
+ { user: (a, b) => a.name === b.name },
279
+ );
280
+
281
+ store.emitter.on("user", ({ value }) => console.log("changed:", value.name));
282
+
283
+ store.user = { name: "Alice" }; // silent (comparator says "equal")
284
+ store.user = { name: "Bob" }; // emits
285
+ store.count = 0; // silent (===)
286
+ store.count = 1; // emits
287
+ ```
288
+
289
+ ---
290
+
291
+ ### Async helper: `until(emitter, action)`
292
+
293
+ Returns a `Promise` that resolves with the `Trigger` on the next emission.
294
+
295
+ ```ts
296
+ import { Emitter, until } from "signalcore";
297
+
298
+ async function start() {
299
+ const trigger = await until(bus, "ready");
300
+ console.log(trigger.data);
301
+ }
302
+ ```
303
+
304
+ Combine with `Promise.race` for timeouts:
305
+
306
+ ```ts
307
+ const result = await Promise.race([
308
+ until(bus, "ready"),
309
+ new Promise((_, reject) => setTimeout(() => reject(new Error("timeout")), 5000)),
310
+ ]);
311
+ ```
312
+
313
+ ---
314
+
315
+ ### `EmitterLike<ActionTypes>` interface
316
+
317
+ Any object that satisfies the `EmitterLike` interface — including `Emitter`, `Union`, or custom implementations — can be used wherever an event source is expected. This enables unlimited compositional flexibility.
318
+
319
+ ```ts
320
+ import type { EmitterLike } from "signalcore";
321
+
322
+ function onTick(target: EmitterLike<{ tick: number }>) {
323
+ target.on("tick", (t) => console.log(t));
324
+ }
325
+ ```
326
+
327
+ ---
328
+
329
+ ## License
330
+
331
+ Apache-2.0
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{},t=Symbol(`Get action handlers`),n=Symbol(`Get all handlers`),r=class{handlers=new Map;onceWrappers=new WeakMap;emit(t,n){let r=this.handlers.get(t);if(!r)return!1;let i=Object.assign(new e,n);Object.defineProperty(i,"emitter",{value:this,enumerable:!1,writable:!0,configurable:!0});let a=r.values(),o;for(;!(o=a.next()).done;)try{o.value(i)}catch(e){console.error(e)}return!0}on(e,t){let n=this.handlers.get(e);n||(n=new Set,this.handlers.set(e,n)),n.add(t)}hasListeners(e){let t=this.handlers.get(e);return t?t.size>0:!1}listenerCount(e){if(e!==void 0)return this.handlers.get(e)?.size??0;let t=0,n=this.handlers.values(),r;for(;!(r=n.next()).done;)t+=r.value.size;return t}actions(){return[...this.handlers.keys()]}once(e,t){let n=r=>{this.off(e,n),t(r)};this.onceWrappers.set(t,n),this.on(e,n)}off(e,t){let n=this.handlers.get(e);if(!n||n.delete(t))return;let r=this.onceWrappers.get(t);r&&(n.delete(r),this.onceWrappers.delete(t))}offAll(e){let t=this.handlers.get(e);t&&t.clear()}clear(){this.handlers.clear()}[t](e){return this.handlers.get(e)}[n](){return this.handlers}},i=class extends r{target;handlerAborters=new Map;constructor(e){super(),this.target=e}on(e,t,n){let r=new AbortController,i=typeof n==`object`&&n?.signal;i&&i.addEventListener(`abort`,()=>r.abort(),{once:!0});let a={...typeof n==`object`?n:{},...typeof n==`boolean`?{capture:n}:{},signal:r.signal},o=t=>{super.emit(e,Object.assign({event:t},t))};this.target.addEventListener(e,o,a),r.signal.addEventListener(`abort`,()=>{this.target.removeEventListener(e,o),this.handlerAborters.get(e)?.delete(t),super.off(e,t)});let s=this.handlerAborters.get(e);s||(s=new Map,this.handlerAborters.set(e,s)),s.set(t,r),a.once?super.once(e,t):super.on(e,t)}once(e,t,n){let r=typeof n==`object`?{...n}:typeof n==`boolean`?{capture:n}:{};r.once=!0,this.on(e,t,r)}off(e,t){let n=this.handlerAborters.get(e);if(!n)return;let r=n.get(t);r&&(r.abort(),n.delete(t))}offAll(e){let t=this.handlerAborters.get(e);if(!t)return;let n=t.values(),r;for(;!(r=n.next()).done;)r.value.abort();this.handlerAborters.delete(e),super.offAll(e)}clear(){let e=this.handlerAborters.values(),t;for(;!(t=e.next()).done;){let e=t.value.values(),n;for(;!(n=e.next()).done;)n.value.abort()}this.handlerAborters.clear(),super.clear()}},a=class extends r{source;handlerWrappers=new Map;constructor(e){super(),this.source=e}on(e,t){let n=(...t)=>{super.emit(e,t)},r=this.handlerWrappers.get(e);r||(r=new Map,this.handlerWrappers.set(e,r)),r.set(t,n),this.source.on(e,n),super.on(e,t)}once(e,t){let n=(...n)=>{this.handlerWrappers.get(e)?.delete(t),super.emit(e,n)},r=this.handlerWrappers.get(e);r||(r=new Map,this.handlerWrappers.set(e,r)),r.set(t,n),this.source.once(e,n),super.once(e,t)}off(e,t){let n=this.handlerWrappers.get(e);if(!n)return;let r=n.get(t);r&&(this.source.off(e,r),n.delete(t),super.off(e,t))}offAll(e){let t=this.handlerWrappers.get(e);if(t){for(let n of t.values())this.source.off(e,n);this.handlerWrappers.delete(e),super.offAll(e)}}clear(){for(let[e,t]of this.handlerWrappers)for(let n of t.values())this.source.off(e,n);this.handlerWrappers.clear(),super.clear()}},o=class{emitter=null;handler=null;wrappingActions;destroyed=!1;constructor(e,t,n){this.emitter=e,this.wrappingActions=t,this.handler=n;let r=t.length;for(let e=0;e<r;e++)this.emitter.on(t[e],n)}get actions(){return[...this.wrappingActions]}destroy(){if(this.destroyed)return;this.destroyed=!0;let{emitter:e,handler:t,wrappingActions:n}=this;if(!e||!t)return;let r=n.length;for(let i=0;i<r;i++)e.off(n[i],t);this.emitter=null,this.handler=null}},s=class{emitters;onSubscriptions=new Map;onceSubscriptions=new Map;constructor(...e){this.emitters=[...e]}add(e){this.emitters.push(e);for(let[t,n]of this.onSubscriptions)for(let r of n)e.on(t,r);for(let[t,n]of this.onceSubscriptions)for(let r of n)e.once(t,r)}remove(e){let t=this.emitters.indexOf(e);if(t===-1)return!1;this.emitters.splice(t,1);for(let[t,n]of this.onSubscriptions)for(let r of n)e.off(t,r);for(let[t,n]of this.onceSubscriptions)for(let r of n)e.off(t,r);return!0}on(e,t){let n=this.onSubscriptions.get(e);n||(n=new Set,this.onSubscriptions.set(e,n)),n.add(t);let r=this.emitters,i=r.length;for(let n=0;n<i;n++)r[n].on(e,t)}once(e,t){let n=this.onceSubscriptions.get(e);n||(n=new Set,this.onceSubscriptions.set(e,n)),n.add(t);let r=this.emitters,i=r.length;for(let n=0;n<i;n++)r[n].once(e,t)}off(e,t){this.onSubscriptions.get(e)?.delete(t),this.onceSubscriptions.get(e)?.delete(t);let n=this.emitters,r=n.length;for(let i=0;i<r;i++)n[i].off(e,t)}offAll(e){this.onSubscriptions.delete(e),this.onceSubscriptions.delete(e);let t=this.emitters,n=t.length;for(let r=0;r<n;r++)t[r].offAll(e)}clear(){this.onSubscriptions.clear(),this.onceSubscriptions.clear();let e=this.emitters,t=e.length;for(let n=0;n<t;n++)e[n].clear()}emit(e,t){let n=!1,r=this.emitters,i=r.length;for(let a=0;a<i;a++)r[a].emit(e,t)&&(n=!0);return n}hasListeners(e){if(this.onSubscriptions.has(e)||this.onceSubscriptions.has(e))return!0;let t=this.emitters,n=t.length;for(let r=0;r<n;r++)if(t[r].hasListeners(e))return!0;return!1}listenerCount(e){let t=0,n=this.emitters,r=n.length;for(let i=0;i<r;i++)t+=n[i].listenerCount(e);return t}actions(){let e=this.emitters,t=e.length;if(t===0)return[];let n=new Set;for(let r=0;r<t;r++){let t=e[r].actions(),i=t.length;for(let e=0;e<i;e++)n.add(t[e])}return[...n]}};async function c(e,t){return new Promise(n=>{e.once(t,n)})}function l(e){let t=new r,n=new Proxy(e,{deleteProperty(){return!1},set(e,n,r){let i=n,a=e[i];return e[i]=r,t.emit(i,{from:a,value:r}),!0}});return Object.defineProperty(n,"emitter",{value:t,enumerable:!1,writable:!1}),n}function u(e,t){return e===t}function d(e,t={}){let n=new r,i=new Proxy(e,{deleteProperty(){return!1},set(e,r,i){let a=r,o=e[a];return(t[a]??u)(o,i)?!0:(e[a]=i,n.emit(a,{from:o,value:i}),!0)}});return Object.defineProperties(i,{emitter:{value:n,enumerable:!1,writable:!1},comparators:{value:t,enumerable:!1,writable:!1}}),i}exports.Emitter=r,exports.EventTargetEmitter=i,exports.Middleware=o,exports.NodeEventEmitter=a,exports.Trigger=e,exports.Union=s,exports.createProxyEmitter=l,exports.createStor=d,exports.until=c;
@@ -0,0 +1,2 @@
1
+ export * from './src/index'
2
+ export {}
@@ -0,0 +1,303 @@
1
+ //#region src/classes/sources/Trigger.ts
2
+ var e = class {}, t = Symbol("Get action handlers"), n = Symbol("Get all handlers"), r = class {
3
+ handlers = /* @__PURE__ */ new Map();
4
+ onceWrappers = /* @__PURE__ */ new WeakMap();
5
+ emit(t, n) {
6
+ let r = this.handlers.get(t);
7
+ if (!r) return !1;
8
+ let i = Object.assign(new e(), n);
9
+ Object.defineProperty(i, "emitter", {
10
+ value: this,
11
+ enumerable: !1,
12
+ writable: !0,
13
+ configurable: !0
14
+ });
15
+ let a = r.values(), o;
16
+ for (; !(o = a.next()).done;) try {
17
+ o.value(i);
18
+ } catch (e) {
19
+ console.error(e);
20
+ }
21
+ return !0;
22
+ }
23
+ on(e, t) {
24
+ let n = this.handlers.get(e);
25
+ n || (n = /* @__PURE__ */ new Set(), this.handlers.set(e, n)), n.add(t);
26
+ }
27
+ hasListeners(e) {
28
+ let t = this.handlers.get(e);
29
+ return t ? t.size > 0 : !1;
30
+ }
31
+ listenerCount(e) {
32
+ if (e !== void 0) return this.handlers.get(e)?.size ?? 0;
33
+ let t = 0, n = this.handlers.values(), r;
34
+ for (; !(r = n.next()).done;) t += r.value.size;
35
+ return t;
36
+ }
37
+ actions() {
38
+ return [...this.handlers.keys()];
39
+ }
40
+ once(e, t) {
41
+ let n = (r) => {
42
+ this.off(e, n), t(r);
43
+ };
44
+ this.onceWrappers.set(t, n), this.on(e, n);
45
+ }
46
+ off(e, t) {
47
+ let n = this.handlers.get(e);
48
+ if (!n || n.delete(t)) return;
49
+ let r = this.onceWrappers.get(t);
50
+ r && (n.delete(r), this.onceWrappers.delete(t));
51
+ }
52
+ offAll(e) {
53
+ let t = this.handlers.get(e);
54
+ t && t.clear();
55
+ }
56
+ clear() {
57
+ this.handlers.clear();
58
+ }
59
+ [t](e) {
60
+ return this.handlers.get(e);
61
+ }
62
+ [n]() {
63
+ return this.handlers;
64
+ }
65
+ }, i = class extends r {
66
+ target;
67
+ handlerAborters = /* @__PURE__ */ new Map();
68
+ constructor(e) {
69
+ super(), this.target = e;
70
+ }
71
+ on(e, t, n) {
72
+ let r = new AbortController(), i = typeof n == "object" && n?.signal;
73
+ i && i.addEventListener("abort", () => r.abort(), { once: !0 });
74
+ let a = {
75
+ ...typeof n == "object" ? n : {},
76
+ ...typeof n == "boolean" ? { capture: n } : {},
77
+ signal: r.signal
78
+ }, o = (t) => {
79
+ super.emit(e, Object.assign({ event: t }, t));
80
+ };
81
+ this.target.addEventListener(e, o, a), r.signal.addEventListener("abort", () => {
82
+ this.target.removeEventListener(e, o), this.handlerAborters.get(e)?.delete(t), super.off(e, t);
83
+ });
84
+ let s = this.handlerAborters.get(e);
85
+ s || (s = /* @__PURE__ */ new Map(), this.handlerAborters.set(e, s)), s.set(t, r), a.once ? super.once(e, t) : super.on(e, t);
86
+ }
87
+ once(e, t, n) {
88
+ let r = typeof n == "object" ? { ...n } : typeof n == "boolean" ? { capture: n } : {};
89
+ r.once = !0, this.on(e, t, r);
90
+ }
91
+ off(e, t) {
92
+ let n = this.handlerAborters.get(e);
93
+ if (!n) return;
94
+ let r = n.get(t);
95
+ r && (r.abort(), n.delete(t));
96
+ }
97
+ offAll(e) {
98
+ let t = this.handlerAborters.get(e);
99
+ if (!t) return;
100
+ let n = t.values(), r;
101
+ for (; !(r = n.next()).done;) r.value.abort();
102
+ this.handlerAborters.delete(e), super.offAll(e);
103
+ }
104
+ clear() {
105
+ let e = this.handlerAborters.values(), t;
106
+ for (; !(t = e.next()).done;) {
107
+ let e = t.value.values(), n;
108
+ for (; !(n = e.next()).done;) n.value.abort();
109
+ }
110
+ this.handlerAborters.clear(), super.clear();
111
+ }
112
+ }, a = class extends r {
113
+ source;
114
+ handlerWrappers = /* @__PURE__ */ new Map();
115
+ constructor(e) {
116
+ super(), this.source = e;
117
+ }
118
+ on(e, t) {
119
+ let n = (...t) => {
120
+ super.emit(e, t);
121
+ }, r = this.handlerWrappers.get(e);
122
+ r || (r = /* @__PURE__ */ new Map(), this.handlerWrappers.set(e, r)), r.set(t, n), this.source.on(e, n), super.on(e, t);
123
+ }
124
+ once(e, t) {
125
+ let n = (...n) => {
126
+ this.handlerWrappers.get(e)?.delete(t), super.emit(e, n);
127
+ }, r = this.handlerWrappers.get(e);
128
+ r || (r = /* @__PURE__ */ new Map(), this.handlerWrappers.set(e, r)), r.set(t, n), this.source.once(e, n), super.once(e, t);
129
+ }
130
+ off(e, t) {
131
+ let n = this.handlerWrappers.get(e);
132
+ if (!n) return;
133
+ let r = n.get(t);
134
+ r && (this.source.off(e, r), n.delete(t), super.off(e, t));
135
+ }
136
+ offAll(e) {
137
+ let t = this.handlerWrappers.get(e);
138
+ if (t) {
139
+ for (let n of t.values()) this.source.off(e, n);
140
+ this.handlerWrappers.delete(e), super.offAll(e);
141
+ }
142
+ }
143
+ clear() {
144
+ for (let [e, t] of this.handlerWrappers) for (let n of t.values()) this.source.off(e, n);
145
+ this.handlerWrappers.clear(), super.clear();
146
+ }
147
+ }, o = class {
148
+ emitter = null;
149
+ handler = null;
150
+ wrappingActions;
151
+ destroyed = !1;
152
+ constructor(e, t, n) {
153
+ this.emitter = e, this.wrappingActions = t, this.handler = n;
154
+ let r = t.length;
155
+ for (let e = 0; e < r; e++) this.emitter.on(t[e], n);
156
+ }
157
+ get actions() {
158
+ return [...this.wrappingActions];
159
+ }
160
+ destroy() {
161
+ if (this.destroyed) return;
162
+ this.destroyed = !0;
163
+ let { emitter: e, handler: t, wrappingActions: n } = this;
164
+ if (!e || !t) return;
165
+ let r = n.length;
166
+ for (let i = 0; i < r; i++) e.off(n[i], t);
167
+ this.emitter = null, this.handler = null;
168
+ }
169
+ }, s = class {
170
+ emitters;
171
+ onSubscriptions = /* @__PURE__ */ new Map();
172
+ onceSubscriptions = /* @__PURE__ */ new Map();
173
+ constructor(...e) {
174
+ this.emitters = [...e];
175
+ }
176
+ add(e) {
177
+ this.emitters.push(e);
178
+ for (let [t, n] of this.onSubscriptions) for (let r of n) e.on(t, r);
179
+ for (let [t, n] of this.onceSubscriptions) for (let r of n) e.once(t, r);
180
+ }
181
+ remove(e) {
182
+ let t = this.emitters.indexOf(e);
183
+ if (t === -1) return !1;
184
+ this.emitters.splice(t, 1);
185
+ for (let [t, n] of this.onSubscriptions) for (let r of n) e.off(t, r);
186
+ for (let [t, n] of this.onceSubscriptions) for (let r of n) e.off(t, r);
187
+ return !0;
188
+ }
189
+ on(e, t) {
190
+ let n = this.onSubscriptions.get(e);
191
+ n || (n = /* @__PURE__ */ new Set(), this.onSubscriptions.set(e, n)), n.add(t);
192
+ let r = this.emitters, i = r.length;
193
+ for (let n = 0; n < i; n++) r[n].on(e, t);
194
+ }
195
+ once(e, t) {
196
+ let n = this.onceSubscriptions.get(e);
197
+ n || (n = /* @__PURE__ */ new Set(), this.onceSubscriptions.set(e, n)), n.add(t);
198
+ let r = this.emitters, i = r.length;
199
+ for (let n = 0; n < i; n++) r[n].once(e, t);
200
+ }
201
+ off(e, t) {
202
+ this.onSubscriptions.get(e)?.delete(t), this.onceSubscriptions.get(e)?.delete(t);
203
+ let n = this.emitters, r = n.length;
204
+ for (let i = 0; i < r; i++) n[i].off(e, t);
205
+ }
206
+ offAll(e) {
207
+ this.onSubscriptions.delete(e), this.onceSubscriptions.delete(e);
208
+ let t = this.emitters, n = t.length;
209
+ for (let r = 0; r < n; r++) t[r].offAll(e);
210
+ }
211
+ clear() {
212
+ this.onSubscriptions.clear(), this.onceSubscriptions.clear();
213
+ let e = this.emitters, t = e.length;
214
+ for (let n = 0; n < t; n++) e[n].clear();
215
+ }
216
+ emit(e, t) {
217
+ let n = !1, r = this.emitters, i = r.length;
218
+ for (let a = 0; a < i; a++) r[a].emit(e, t) && (n = !0);
219
+ return n;
220
+ }
221
+ hasListeners(e) {
222
+ if (this.onSubscriptions.has(e) || this.onceSubscriptions.has(e)) return !0;
223
+ let t = this.emitters, n = t.length;
224
+ for (let r = 0; r < n; r++) if (t[r].hasListeners(e)) return !0;
225
+ return !1;
226
+ }
227
+ listenerCount(e) {
228
+ let t = 0, n = this.emitters, r = n.length;
229
+ for (let i = 0; i < r; i++) t += n[i].listenerCount(e);
230
+ return t;
231
+ }
232
+ actions() {
233
+ let e = this.emitters, t = e.length;
234
+ if (t === 0) return [];
235
+ let n = /* @__PURE__ */ new Set();
236
+ for (let r = 0; r < t; r++) {
237
+ let t = e[r].actions(), i = t.length;
238
+ for (let e = 0; e < i; e++) n.add(t[e]);
239
+ }
240
+ return [...n];
241
+ }
242
+ };
243
+ //#endregion
244
+ //#region src/hooks/until.ts
245
+ async function c(e, t) {
246
+ return new Promise((n) => {
247
+ e.once(t, n);
248
+ });
249
+ }
250
+ //#endregion
251
+ //#region src/classes/sources/ProxyEmitter.ts
252
+ function l(e) {
253
+ let t = new r(), n = new Proxy(e, {
254
+ deleteProperty() {
255
+ return !1;
256
+ },
257
+ set(e, n, r) {
258
+ let i = n, a = e[i];
259
+ return e[i] = r, t.emit(i, {
260
+ from: a,
261
+ value: r
262
+ }), !0;
263
+ }
264
+ });
265
+ return Object.defineProperty(n, "emitter", {
266
+ value: t,
267
+ enumerable: !1,
268
+ writable: !1
269
+ }), n;
270
+ }
271
+ //#endregion
272
+ //#region src/classes/sources/Stor.ts
273
+ function u(e, t) {
274
+ return e === t;
275
+ }
276
+ function d(e, t = {}) {
277
+ let n = new r(), i = new Proxy(e, {
278
+ deleteProperty() {
279
+ return !1;
280
+ },
281
+ set(e, r, i) {
282
+ let a = r, o = e[a];
283
+ return (t[a] ?? u)(o, i) ? !0 : (e[a] = i, n.emit(a, {
284
+ from: o,
285
+ value: i
286
+ }), !0);
287
+ }
288
+ });
289
+ return Object.defineProperties(i, {
290
+ emitter: {
291
+ value: n,
292
+ enumerable: !1,
293
+ writable: !1
294
+ },
295
+ comparators: {
296
+ value: t,
297
+ enumerable: !1,
298
+ writable: !1
299
+ }
300
+ }), i;
301
+ }
302
+ //#endregion
303
+ export { r as Emitter, i as EventTargetEmitter, o as Middleware, a as NodeEventEmitter, e as Trigger, s as Union, l as createProxyEmitter, d as createStor, c as until };
@@ -0,0 +1 @@
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.signalcore={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=class{},n=Symbol(`Get action handlers`),r=Symbol(`Get all handlers`),i=class{handlers=new Map;onceWrappers=new WeakMap;emit(e,n){let r=this.handlers.get(e);if(!r)return!1;let i=Object.assign(new t,n);Object.defineProperty(i,"emitter",{value:this,enumerable:!1,writable:!0,configurable:!0});let a=r.values(),o;for(;!(o=a.next()).done;)try{o.value(i)}catch(e){console.error(e)}return!0}on(e,t){let n=this.handlers.get(e);n||(n=new Set,this.handlers.set(e,n)),n.add(t)}hasListeners(e){let t=this.handlers.get(e);return t?t.size>0:!1}listenerCount(e){if(e!==void 0)return this.handlers.get(e)?.size??0;let t=0,n=this.handlers.values(),r;for(;!(r=n.next()).done;)t+=r.value.size;return t}actions(){return[...this.handlers.keys()]}once(e,t){let n=r=>{this.off(e,n),t(r)};this.onceWrappers.set(t,n),this.on(e,n)}off(e,t){let n=this.handlers.get(e);if(!n||n.delete(t))return;let r=this.onceWrappers.get(t);r&&(n.delete(r),this.onceWrappers.delete(t))}offAll(e){let t=this.handlers.get(e);t&&t.clear()}clear(){this.handlers.clear()}[n](e){return this.handlers.get(e)}[r](){return this.handlers}},a=class extends i{target;handlerAborters=new Map;constructor(e){super(),this.target=e}on(e,t,n){let r=new AbortController,i=typeof n==`object`&&n?.signal;i&&i.addEventListener(`abort`,()=>r.abort(),{once:!0});let a={...typeof n==`object`?n:{},...typeof n==`boolean`?{capture:n}:{},signal:r.signal},o=t=>{super.emit(e,Object.assign({event:t},t))};this.target.addEventListener(e,o,a),r.signal.addEventListener(`abort`,()=>{this.target.removeEventListener(e,o),this.handlerAborters.get(e)?.delete(t),super.off(e,t)});let s=this.handlerAborters.get(e);s||(s=new Map,this.handlerAborters.set(e,s)),s.set(t,r),a.once?super.once(e,t):super.on(e,t)}once(e,t,n){let r=typeof n==`object`?{...n}:typeof n==`boolean`?{capture:n}:{};r.once=!0,this.on(e,t,r)}off(e,t){let n=this.handlerAborters.get(e);if(!n)return;let r=n.get(t);r&&(r.abort(),n.delete(t))}offAll(e){let t=this.handlerAborters.get(e);if(!t)return;let n=t.values(),r;for(;!(r=n.next()).done;)r.value.abort();this.handlerAborters.delete(e),super.offAll(e)}clear(){let e=this.handlerAborters.values(),t;for(;!(t=e.next()).done;){let e=t.value.values(),n;for(;!(n=e.next()).done;)n.value.abort()}this.handlerAborters.clear(),super.clear()}},o=class extends i{source;handlerWrappers=new Map;constructor(e){super(),this.source=e}on(e,t){let n=(...t)=>{super.emit(e,t)},r=this.handlerWrappers.get(e);r||(r=new Map,this.handlerWrappers.set(e,r)),r.set(t,n),this.source.on(e,n),super.on(e,t)}once(e,t){let n=(...n)=>{this.handlerWrappers.get(e)?.delete(t),super.emit(e,n)},r=this.handlerWrappers.get(e);r||(r=new Map,this.handlerWrappers.set(e,r)),r.set(t,n),this.source.once(e,n),super.once(e,t)}off(e,t){let n=this.handlerWrappers.get(e);if(!n)return;let r=n.get(t);r&&(this.source.off(e,r),n.delete(t),super.off(e,t))}offAll(e){let t=this.handlerWrappers.get(e);if(t){for(let n of t.values())this.source.off(e,n);this.handlerWrappers.delete(e),super.offAll(e)}}clear(){for(let[e,t]of this.handlerWrappers)for(let n of t.values())this.source.off(e,n);this.handlerWrappers.clear(),super.clear()}},s=class{emitter=null;handler=null;wrappingActions;destroyed=!1;constructor(e,t,n){this.emitter=e,this.wrappingActions=t,this.handler=n;let r=t.length;for(let e=0;e<r;e++)this.emitter.on(t[e],n)}get actions(){return[...this.wrappingActions]}destroy(){if(this.destroyed)return;this.destroyed=!0;let{emitter:e,handler:t,wrappingActions:n}=this;if(!e||!t)return;let r=n.length;for(let i=0;i<r;i++)e.off(n[i],t);this.emitter=null,this.handler=null}},c=class{emitters;onSubscriptions=new Map;onceSubscriptions=new Map;constructor(...e){this.emitters=[...e]}add(e){this.emitters.push(e);for(let[t,n]of this.onSubscriptions)for(let r of n)e.on(t,r);for(let[t,n]of this.onceSubscriptions)for(let r of n)e.once(t,r)}remove(e){let t=this.emitters.indexOf(e);if(t===-1)return!1;this.emitters.splice(t,1);for(let[t,n]of this.onSubscriptions)for(let r of n)e.off(t,r);for(let[t,n]of this.onceSubscriptions)for(let r of n)e.off(t,r);return!0}on(e,t){let n=this.onSubscriptions.get(e);n||(n=new Set,this.onSubscriptions.set(e,n)),n.add(t);let r=this.emitters,i=r.length;for(let n=0;n<i;n++)r[n].on(e,t)}once(e,t){let n=this.onceSubscriptions.get(e);n||(n=new Set,this.onceSubscriptions.set(e,n)),n.add(t);let r=this.emitters,i=r.length;for(let n=0;n<i;n++)r[n].once(e,t)}off(e,t){this.onSubscriptions.get(e)?.delete(t),this.onceSubscriptions.get(e)?.delete(t);let n=this.emitters,r=n.length;for(let i=0;i<r;i++)n[i].off(e,t)}offAll(e){this.onSubscriptions.delete(e),this.onceSubscriptions.delete(e);let t=this.emitters,n=t.length;for(let r=0;r<n;r++)t[r].offAll(e)}clear(){this.onSubscriptions.clear(),this.onceSubscriptions.clear();let e=this.emitters,t=e.length;for(let n=0;n<t;n++)e[n].clear()}emit(e,t){let n=!1,r=this.emitters,i=r.length;for(let a=0;a<i;a++)r[a].emit(e,t)&&(n=!0);return n}hasListeners(e){if(this.onSubscriptions.has(e)||this.onceSubscriptions.has(e))return!0;let t=this.emitters,n=t.length;for(let r=0;r<n;r++)if(t[r].hasListeners(e))return!0;return!1}listenerCount(e){let t=0,n=this.emitters,r=n.length;for(let i=0;i<r;i++)t+=n[i].listenerCount(e);return t}actions(){let e=this.emitters,t=e.length;if(t===0)return[];let n=new Set;for(let r=0;r<t;r++){let t=e[r].actions(),i=t.length;for(let e=0;e<i;e++)n.add(t[e])}return[...n]}};async function l(e,t){return new Promise(n=>{e.once(t,n)})}function u(e){let t=new i,n=new Proxy(e,{deleteProperty(){return!1},set(e,n,r){let i=n,a=e[i];return e[i]=r,t.emit(i,{from:a,value:r}),!0}});return Object.defineProperty(n,"emitter",{value:t,enumerable:!1,writable:!1}),n}function d(e,t){return e===t}function f(e,t={}){let n=new i,r=new Proxy(e,{deleteProperty(){return!1},set(e,r,i){let a=r,o=e[a];return(t[a]??d)(o,i)?!0:(e[a]=i,n.emit(a,{from:o,value:i}),!0)}});return Object.defineProperties(r,{emitter:{value:n,enumerable:!1,writable:!1},comparators:{value:t,enumerable:!1,writable:!1}}),r}e.Emitter=i,e.EventTargetEmitter=a,e.Middleware=s,e.NodeEventEmitter=o,e.Trigger=t,e.Union=c,e.createProxyEmitter=u,e.createStor=f,e.until=l});
@@ -0,0 +1,14 @@
1
+ import { Emitter } from '../sources/Emitter';
2
+ import { Trigger } from '../../types';
3
+ type MiddlewareActionTypes<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>, UsedActions extends (keyof ActionTypes)[]> = ActionTypes[UsedActions[number]];
4
+ type MiddlewareHandler<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>, UsedActions extends (keyof ActionTypes)[]> = (target: Trigger<MiddlewareActionTypes<ActionTypes, UsedActions>, Emitter<ActionTypes>>) => void;
5
+ export declare class Middleware<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>, UsedActions extends (keyof ActionTypes)[]> {
6
+ private emitter;
7
+ private handler;
8
+ private readonly wrappingActions;
9
+ private destroyed;
10
+ constructor(emitter: Emitter<ActionTypes>, actions: UsedActions, handler: MiddlewareHandler<ActionTypes, UsedActions>);
11
+ get actions(): UsedActions[number][];
12
+ destroy(): void;
13
+ }
14
+ export {};
@@ -0,0 +1,18 @@
1
+ import { EmitterLike, TriggerHandler } from '../../types';
2
+ export declare class Union<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>> {
3
+ private readonly emitters;
4
+ private readonly onSubscriptions;
5
+ private readonly onceSubscriptions;
6
+ constructor(...emitters: EmitterLike<ActionTypes>[]);
7
+ add(emitter: EmitterLike<ActionTypes>): void;
8
+ remove(emitter: EmitterLike<ActionTypes>): boolean;
9
+ on<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
10
+ once<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
11
+ off<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
12
+ offAll<Action extends keyof ActionTypes>(action: Action): void;
13
+ clear(): void;
14
+ emit<Action extends keyof ActionTypes>(action: Action, data: ActionTypes[Action]): boolean;
15
+ hasListeners<Action extends keyof ActionTypes>(action: Action): boolean;
16
+ listenerCount<Action extends keyof ActionTypes>(action?: Action): number;
17
+ actions(): (keyof ActionTypes)[];
18
+ }
@@ -0,0 +1,17 @@
1
+ import { TriggerHandler } from '../../types';
2
+ import { getActionHandlers, getAllHandlers } from '../../symbols';
3
+ export declare class Emitter<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>> {
4
+ private readonly handlers;
5
+ private readonly onceWrappers;
6
+ emit<Action extends keyof ActionTypes>(action: Action, data: ActionTypes[Action]): boolean;
7
+ on<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
8
+ hasListeners<Action extends keyof ActionTypes>(action: Action): boolean;
9
+ listenerCount<Action extends keyof ActionTypes>(action?: Action): number;
10
+ actions(): (keyof ActionTypes)[];
11
+ once<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
12
+ off<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
13
+ offAll<Action extends keyof ActionTypes>(action: Action): void;
14
+ clear(): void;
15
+ [getActionHandlers]<Action extends keyof ActionTypes>(action: Action): Set<TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>> | undefined;
16
+ [getAllHandlers](): Map<keyof ActionTypes, Set<TriggerHandler<any, any>>>;
17
+ }
@@ -0,0 +1,12 @@
1
+ import { Emitter } from './Emitter';
2
+ import { TriggerHandler } from '../../types';
3
+ export declare class EventTargetEmitter<ActionTypes extends Record<string, Event>> extends Emitter<ActionTypes> {
4
+ private readonly target;
5
+ private readonly handlerAborters;
6
+ constructor(eventTarget: EventTarget);
7
+ on<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>, options?: AddEventListenerOptions | boolean): void;
8
+ once<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>, options?: AddEventListenerOptions | boolean): void;
9
+ off<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
10
+ offAll<Action extends keyof ActionTypes>(action: Action): void;
11
+ clear(): void;
12
+ }
@@ -0,0 +1,18 @@
1
+ import { Emitter } from './Emitter';
2
+ import { TriggerHandler } from '../../types';
3
+ interface NodeEventSource {
4
+ on(event: string, listener: (...args: any[]) => void): any;
5
+ once(event: string, listener: (...args: any[]) => void): any;
6
+ off(event: string, listener: (...args: any[]) => void): any;
7
+ }
8
+ export declare class NodeEventEmitter<ActionTypes extends Record<string, any[]>> extends Emitter<ActionTypes> {
9
+ private readonly source;
10
+ private readonly handlerWrappers;
11
+ constructor(source: NodeEventSource);
12
+ on<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
13
+ once<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
14
+ off<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], Emitter<ActionTypes>>): void;
15
+ offAll<Action extends keyof ActionTypes>(action: Action): void;
16
+ clear(): void;
17
+ }
18
+ export {};
@@ -0,0 +1,8 @@
1
+ import { Emitter } from './Emitter';
2
+ import { DataEmitting } from '../../types';
3
+ export type ProxyEmitter<Data extends object> = {
4
+ [P in keyof Data]: Data[P];
5
+ } & {
6
+ emitter: Emitter<DataEmitting<Data>>;
7
+ };
8
+ export declare function createProxyEmitter<Data extends object>(initState: Data): ProxyEmitter<Data>;
@@ -0,0 +1,15 @@
1
+ import { Emitter } from './Emitter';
2
+ import { DataEmitting } from '../../types';
3
+ type Comparator<Data extends object, Key extends keyof Data> = (from: Data[Key], to: Data[Key]) => boolean;
4
+ export type Stor<Data extends object> = {
5
+ [P in keyof Data]: Data[P];
6
+ } & {
7
+ emitter: Emitter<DataEmitting<Data>>;
8
+ comparators: Partial<{
9
+ [Key in keyof Data]: Comparator<Data, Key>;
10
+ }>;
11
+ };
12
+ export declare function createStor<Data extends object>(initState: Data, comparators?: Partial<{
13
+ [K in keyof Data]: Comparator<Data, K>;
14
+ }>): Stor<Data>;
15
+ export {};
@@ -0,0 +1,2 @@
1
+ export declare class Trigger {
2
+ }
@@ -0,0 +1 @@
1
+ export { until } from './until';
@@ -0,0 +1,3 @@
1
+ import { Emitter } from '../classes/sources/Emitter';
2
+ import { BaseActionTypes, Trigger } from '../types';
3
+ export declare function until<ActionTypes extends BaseActionTypes<ActionTypes>, Action extends keyof ActionTypes>(emitter: Emitter<ActionTypes>, action: Action): Promise<Trigger<ActionTypes[Action], Emitter<ActionTypes>>>;
@@ -0,0 +1,9 @@
1
+ export { Trigger } from './classes/sources/Trigger';
2
+ export { Emitter } from './classes/sources/Emitter';
3
+ export { EventTargetEmitter } from './classes/sources/EventTargetEmitter';
4
+ export { NodeEventEmitter } from './classes/sources/NodeEventEmitter';
5
+ export { Middleware } from './classes/boundary/Middleware';
6
+ export { Union } from './classes/composition/Union';
7
+ export { until } from './hooks';
8
+ export { createProxyEmitter, type ProxyEmitter } from './classes/sources/ProxyEmitter';
9
+ export { createStor, type Stor } from './classes/sources/Stor';
@@ -0,0 +1,2 @@
1
+ export declare const getActionHandlers: unique symbol;
2
+ export declare const getAllHandlers: unique symbol;
@@ -0,0 +1,21 @@
1
+ import { Trigger as TriggerClass } from './classes/sources/Trigger';
2
+ export type Trigger<ActionData, EmitterThis> = TriggerClass & ActionData & {
3
+ emitter: EmitterThis;
4
+ };
5
+ export type TriggerHandler<ActionData, EmitterThis> = (trigger: Trigger<ActionData, EmitterThis>) => void;
6
+ export type BaseActionTypes<ActionTypes> = Record<keyof ActionTypes, Record<any, any>>;
7
+ export type DataEmitting<Data extends object> = Record<keyof Data, {
8
+ from: Data[keyof Data];
9
+ value: Data[keyof Data];
10
+ }>;
11
+ export type EmitterLike<ActionTypes extends Record<keyof ActionTypes, Record<any, any>>> = {
12
+ emit<Action extends keyof ActionTypes>(action: Action, data: ActionTypes[Action]): boolean;
13
+ on<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
14
+ once<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
15
+ off<Action extends keyof ActionTypes>(action: Action, handler: TriggerHandler<ActionTypes[Action], any>): void;
16
+ offAll<Action extends keyof ActionTypes>(action: Action): void;
17
+ clear(): void;
18
+ hasListeners<Action extends keyof ActionTypes>(action: Action): boolean;
19
+ listenerCount<Action extends keyof ActionTypes>(action?: Action): number;
20
+ actions(): (keyof ActionTypes)[];
21
+ };
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "signalcore",
3
+ "version": "1.0.0",
4
+ "description": "Typed Event-Driven core modules",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/limonikal/signalcore.git"
8
+ },
9
+ "homepage": "https://github.com/limonikal/signalcore",
10
+ "bugs": {
11
+ "url": "https://github.com/limonikal/signalcore/issues"
12
+ },
13
+ "keywords": [
14
+ "typed-event-emitter",
15
+ "event-driven",
16
+ "typescript",
17
+ "type-safe",
18
+ "event-bus",
19
+ "reactive",
20
+ "proxy",
21
+ "middleware",
22
+ "event-target",
23
+ "tree-shakeable",
24
+ "zero-dependencies",
25
+ "tiny"
26
+ ],
27
+ "license": "Apache-2.0",
28
+ "sideEffects": false,
29
+ "scripts": {
30
+ "start": "vite",
31
+ "start.host": "vite --host",
32
+ "build": "vite build",
33
+ "preview": "vite preview",
34
+ "preview.host": "vite preview --host",
35
+ "previewBuild": "vite build & vite preview",
36
+ "prepublishOnly": "vite build"
37
+ },
38
+ "devDependencies": {
39
+ "typescript": "^6.0.3",
40
+ "vite": "^8.0.11",
41
+ "vite-plugin-dts": "^5.0.1"
42
+ },
43
+ "files": [
44
+ "dist",
45
+ "README.md"
46
+ ],
47
+ "main": "./dist/index.cjs.js",
48
+ "module": "./dist/index.esm.js",
49
+ "types": "./dist/index.d.ts",
50
+ "browser": "./dist/index.umd.js",
51
+ "unpkg": "./dist/index.umd.js",
52
+ "jsdelivr": "./dist/index.umd.js",
53
+ "exports": {
54
+ ".": {
55
+ "types": "./dist/index.d.ts",
56
+ "import": "./dist/index.esm.js",
57
+ "require": "./dist/index.cjs.js"
58
+ },
59
+ "./package.json": "./package.json"
60
+ }
61
+ }