event-emission 0.2.0 → 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/README.md +158 -25
- package/dist/event-emission.d.ts +63 -14
- package/dist/event-emission.d.ts.map +1 -1
- package/dist/factory.d.ts +1 -1
- package/dist/factory.d.ts.map +1 -1
- package/dist/index.cjs +614 -291
- package/dist/index.cjs.map +8 -9
- package/dist/index.d.ts +1 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +589 -284
- package/dist/index.js.map +8 -9
- package/dist/interoperability.cjs +1719 -0
- package/dist/interoperability.cjs.map +15 -0
- package/dist/{interop.d.ts → interoperability.d.ts} +2 -1
- package/dist/interoperability.d.ts.map +1 -0
- package/dist/interoperability.js +1669 -0
- package/dist/interoperability.js.map +15 -0
- package/dist/observable.cjs +286 -0
- package/dist/observable.cjs.map +11 -0
- package/dist/observable.js +253 -0
- package/dist/observable.js.map +11 -0
- package/dist/observe.cjs +344 -0
- package/dist/observe.cjs.map +10 -0
- package/dist/observe.d.ts +6 -1
- package/dist/observe.d.ts.map +1 -1
- package/dist/observe.js +313 -0
- package/dist/observe.js.map +10 -0
- package/dist/symbols.d.ts +1 -1
- package/dist/types.cjs +35 -0
- package/dist/types.cjs.map +9 -0
- package/dist/types.d.ts +73 -25
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +3 -0
- package/dist/types.js.map +9 -0
- package/package.json +25 -1
- package/src/event-emission.ts +140 -21
- package/src/factory.ts +686 -230
- package/src/index.ts +4 -33
- package/src/{interop.ts → interoperability.ts} +34 -6
- package/src/observe.ts +54 -17
- package/src/symbols.ts +1 -1
- package/src/types.ts +115 -33
- package/dist/interop.d.ts.map +0 -1
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ A lightweight, zero-dependency, type-safe event system with DOM EventTarget ergo
|
|
|
9
9
|
- **Typed events** - Event maps keep payloads and event names in sync
|
|
10
10
|
- **DOM compatible** - `EmissionEvent` is a superset of the built-in `Event`
|
|
11
11
|
- **Familiar API** - `addEventListener`, `removeEventListener`, `dispatchEvent`
|
|
12
|
+
- **Node.js compatible** - `emit`, `on`, `off`, `addListener`, `removeListener`, `prependListener`, and more
|
|
12
13
|
- **TC39 Observable** - Fully compliant `Observable` implementation (passes all `es-observable-tests`)
|
|
13
14
|
- **Async iteration** - `for await...of` over events with backpressure options
|
|
14
15
|
- **Wildcard listeners** - Listen to `*` or namespaced `user:*` patterns
|
|
@@ -59,17 +60,38 @@ events.dispatchEvent({
|
|
|
59
60
|
## Core concepts
|
|
60
61
|
|
|
61
62
|
- **Event map**: a TypeScript type that maps event names to payload types.
|
|
62
|
-
- **Event shape**: `{ type: string; detail: Payload }` for
|
|
63
|
+
- **Event shape**: `{ type: string; detail: Payload; bubbles?: boolean; cancelable?: boolean; composed?: boolean }` for dispatch.
|
|
63
64
|
- **Unsubscribe**: `addEventListener` returns a function to remove the listener.
|
|
64
65
|
|
|
65
66
|
## API overview
|
|
66
67
|
|
|
68
|
+
Core (`event-emission`):
|
|
69
|
+
|
|
67
70
|
- `createEventTarget<E>(options?)`
|
|
68
71
|
- `createEventTarget(target, { observe: true, ... })`
|
|
69
72
|
- `EventEmission<E>` base class
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
+
|
|
74
|
+
Optional subpaths:
|
|
75
|
+
|
|
76
|
+
- `Observable<T>` compliant implementation (`event-emission/observable`)
|
|
77
|
+
- Interoperability: `fromEventTarget`, `forwardToEventTarget`, `pipe` (`event-emission/interoperability`)
|
|
78
|
+
- Observe utilities: `isObserved`, `getOriginal`, `setupEventForwarding` (`event-emission/observe`)
|
|
79
|
+
- Types-only exports (`event-emission/types`)
|
|
80
|
+
|
|
81
|
+
## Subpath exports
|
|
82
|
+
|
|
83
|
+
To keep the core entrypoint lean, optional features are exposed via subpath exports:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { Observable } from 'event-emission/observable';
|
|
87
|
+
import { getOriginal, isObserved, setupEventForwarding } from 'event-emission/observe';
|
|
88
|
+
import {
|
|
89
|
+
forwardToEventTarget,
|
|
90
|
+
fromEventTarget,
|
|
91
|
+
pipe,
|
|
92
|
+
} from 'event-emission/interoperability';
|
|
93
|
+
import type { EventTargetLike } from 'event-emission/types';
|
|
94
|
+
```
|
|
73
95
|
|
|
74
96
|
## createEventTarget
|
|
75
97
|
|
|
@@ -117,11 +139,29 @@ state.user.name = 'Grace'; // Triggers 'update' and 'update:user.name'
|
|
|
117
139
|
|
|
118
140
|
**Observe options:**
|
|
119
141
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
|
123
|
-
|
|
|
124
|
-
| `
|
|
142
|
+
Deep observation is enabled by default.
|
|
143
|
+
|
|
144
|
+
| Option | Type | Default | Description |
|
|
145
|
+
| --------------- | ------------------------------- | -------- | ------------------------------------------------------------------ |
|
|
146
|
+
| `observe` | `boolean` | `false` | Enable property change observation |
|
|
147
|
+
| `deep` | `boolean` | `true` | Observe nested objects |
|
|
148
|
+
| `cloneStrategy` | `'shallow' \| 'deep' \| 'path'` | `'path'` | How to clone previous state |
|
|
149
|
+
| `deepClone` | `<T>(value: T) => T` | - | Optional deep clone fallback when `structuredClone` is unavailable |
|
|
150
|
+
|
|
151
|
+
Note: `cloneStrategy: 'deep'` uses `structuredClone` by default, or `deepClone` if provided.
|
|
152
|
+
|
|
153
|
+
Example fallback:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const state = createEventTarget(
|
|
157
|
+
{ count: 0, user: { name: 'Ada' } },
|
|
158
|
+
{
|
|
159
|
+
observe: true,
|
|
160
|
+
cloneStrategy: 'deep',
|
|
161
|
+
deepClone: (value) => JSON.parse(JSON.stringify(value)),
|
|
162
|
+
},
|
|
163
|
+
);
|
|
164
|
+
```
|
|
125
165
|
|
|
126
166
|
**Update event details:**
|
|
127
167
|
|
|
@@ -130,9 +170,21 @@ state.user.name = 'Grace'; // Triggers 'update' and 'update:user.name'
|
|
|
130
170
|
|
|
131
171
|
## Event listeners
|
|
132
172
|
|
|
173
|
+
### `on(type, listener)`
|
|
174
|
+
|
|
175
|
+
When called with a listener function (or `EventListenerObject`), `on` behaves like Node.js `addListener`—it registers the listener and returns an unsubscribe function.
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
const unsubscribe = events.on('message', (event) => {
|
|
179
|
+
console.log(event.detail.text);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
unsubscribe(); // Remove the listener
|
|
183
|
+
```
|
|
184
|
+
|
|
133
185
|
### `on(type, options?)`
|
|
134
186
|
|
|
135
|
-
|
|
187
|
+
When called without a listener (or with an options object / boolean), `on` creates an `Observable` for a specific event type. This follows the `ObservableEventTarget` proposal, allowing for powerful composition.
|
|
136
188
|
|
|
137
189
|
```typescript
|
|
138
190
|
const clicks = button.on('click', { passive: true });
|
|
@@ -146,6 +198,7 @@ clicks.subscribe((event) => {
|
|
|
146
198
|
|
|
147
199
|
| Option | Type | Default | Description |
|
|
148
200
|
| -------------- | ------------- | ------- | -------------------------------------------------------------------------------------- |
|
|
201
|
+
| `capture` | `boolean` | `false` | If true, listen during the capture phase |
|
|
149
202
|
| `receiveError` | `boolean` | `false` | If true, listen for "error" events and forward them to the observer's error method |
|
|
150
203
|
| `handler` | `Function` | `null` | Optional function to run stateful actions (like `preventDefault()`) before dispatching |
|
|
151
204
|
| `once` | `boolean` | `false` | If true, the observable completes after the first event is dispatched |
|
|
@@ -164,18 +217,22 @@ const unsubscribe = events.addEventListener('message', (event) => {
|
|
|
164
217
|
unsubscribe();
|
|
165
218
|
```
|
|
166
219
|
|
|
167
|
-
**Options:**
|
|
220
|
+
**Options:** (or pass `true`/`false` for capture)
|
|
221
|
+
|
|
222
|
+
| Option | Type | Description |
|
|
223
|
+
| --------- | ------------- | -------------------------------------------- |
|
|
224
|
+
| `capture` | `boolean` | Listen during the capture phase |
|
|
225
|
+
| `once` | `boolean` | Remove listener after first invocation |
|
|
226
|
+
| `passive` | `boolean` | Listener will not call `preventDefault()` |
|
|
227
|
+
| `signal` | `AbortSignal` | Abort signal to remove listener when aborted |
|
|
168
228
|
|
|
169
|
-
|
|
170
|
-
| -------- | ------------- | -------------------------------------------- |
|
|
171
|
-
| `once` | `boolean` | Remove listener after first invocation |
|
|
172
|
-
| `signal` | `AbortSignal` | Abort signal to remove listener when aborted |
|
|
229
|
+
Note: `preventDefault()` only affects events dispatched with `cancelable: true`. `dispatchEvent` returns `false` when a cancelable event is prevented.
|
|
173
230
|
|
|
174
231
|
### `once(type, listener, options?)`
|
|
175
232
|
|
|
176
233
|
Adds a one-time listener.
|
|
177
234
|
|
|
178
|
-
### `removeEventListener(type, listener)`
|
|
235
|
+
### `removeEventListener(type, listener, options?)`
|
|
179
236
|
|
|
180
237
|
Removes a specific listener.
|
|
181
238
|
|
|
@@ -197,6 +254,70 @@ events.addWildcardListener('user:*', (event) => {
|
|
|
197
254
|
|
|
198
255
|
Wildcard events include `{ type: pattern, originalType, detail }`.
|
|
199
256
|
|
|
257
|
+
## Node.js EventEmitter compatibility
|
|
258
|
+
|
|
259
|
+
Every event target also exposes the familiar Node.js `EventEmitter` interface, so you can use whichever style fits your codebase.
|
|
260
|
+
|
|
261
|
+
### `emit(type, detail)`
|
|
262
|
+
|
|
263
|
+
Dispatches an event and returns `true` if listeners were registered for that type, `false` otherwise.
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
events.on('message', (event) => {
|
|
267
|
+
console.log(event.detail.text);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
events.emit('message', { text: 'hello' }); // true
|
|
271
|
+
events.emit('unknown', undefined); // false
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### `off(type, listener)`
|
|
275
|
+
|
|
276
|
+
Removes a listener. Alias for `removeEventListener(type, listener)`.
|
|
277
|
+
|
|
278
|
+
### `addListener(type, listener)` / `removeListener(type, listener)`
|
|
279
|
+
|
|
280
|
+
Aliases for `addEventListener` and `removeEventListener`.
|
|
281
|
+
|
|
282
|
+
### `prependListener(type, listener)`
|
|
283
|
+
|
|
284
|
+
Adds a listener at the **beginning** of the listener list. Returns an unsubscribe function.
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
events.addEventListener('data', () => console.log('second'));
|
|
288
|
+
events.prependListener('data', () => console.log('first'));
|
|
289
|
+
|
|
290
|
+
events.emit('data', value); // logs "first", then "second"
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### `prependOnceListener(type, listener)`
|
|
294
|
+
|
|
295
|
+
Like `prependListener`, but the listener is removed after its first invocation.
|
|
296
|
+
|
|
297
|
+
### `listeners(type)`
|
|
298
|
+
|
|
299
|
+
Returns an array of the listener functions registered for the given type.
|
|
300
|
+
|
|
301
|
+
### `rawListeners(type)`
|
|
302
|
+
|
|
303
|
+
Like `listeners`, but once-listeners return a wrapper function with a `.listener` property pointing to the original callback.
|
|
304
|
+
|
|
305
|
+
### `listenerCount(type)`
|
|
306
|
+
|
|
307
|
+
Returns the number of listeners registered for the given type.
|
|
308
|
+
|
|
309
|
+
### `eventNames()`
|
|
310
|
+
|
|
311
|
+
Returns an array of event type strings that currently have registered listeners.
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
events.on('foo', () => {});
|
|
315
|
+
events.on('bar', () => {});
|
|
316
|
+
|
|
317
|
+
events.eventNames(); // ['foo', 'bar']
|
|
318
|
+
events.listenerCount('foo'); // 1
|
|
319
|
+
```
|
|
320
|
+
|
|
200
321
|
## Async iteration
|
|
201
322
|
|
|
202
323
|
You can use `for await...of` to consume events. This is great for stream-processing events with backpressure.
|
|
@@ -266,7 +387,7 @@ observable
|
|
|
266
387
|
A fully compliant implementation of the TC39 Observable proposal.
|
|
267
388
|
|
|
268
389
|
```typescript
|
|
269
|
-
import { Observable } from 'event-emission';
|
|
390
|
+
import { Observable } from 'event-emission/observable';
|
|
270
391
|
|
|
271
392
|
// Create from items
|
|
272
393
|
const numbers = Observable.of(1, 2, 3);
|
|
@@ -316,7 +437,7 @@ class UserService extends EventEmission<{
|
|
|
316
437
|
### `fromEventTarget(domTarget, eventTypes, options?)`
|
|
317
438
|
|
|
318
439
|
```typescript
|
|
319
|
-
import { fromEventTarget } from 'event-emission';
|
|
440
|
+
import { fromEventTarget } from 'event-emission/interoperability';
|
|
320
441
|
|
|
321
442
|
type ButtonEvents = {
|
|
322
443
|
click: MouseEvent;
|
|
@@ -336,7 +457,8 @@ events.destroy();
|
|
|
336
457
|
### `forwardToEventTarget(source, domTarget, options?)`
|
|
337
458
|
|
|
338
459
|
```typescript
|
|
339
|
-
import { createEventTarget
|
|
460
|
+
import { createEventTarget } from 'event-emission';
|
|
461
|
+
import { forwardToEventTarget } from 'event-emission/interoperability';
|
|
340
462
|
|
|
341
463
|
const events = createEventTarget<{ custom: { value: number } }>();
|
|
342
464
|
const element = document.getElementById('target');
|
|
@@ -351,7 +473,8 @@ unsubscribe();
|
|
|
351
473
|
### `pipe(source, target, options?)`
|
|
352
474
|
|
|
353
475
|
```typescript
|
|
354
|
-
import { createEventTarget
|
|
476
|
+
import { createEventTarget } from 'event-emission';
|
|
477
|
+
import { pipe } from 'event-emission/interoperability';
|
|
355
478
|
|
|
356
479
|
const componentEvents = createEventTarget<{ ready: void }>();
|
|
357
480
|
const appBus = createEventTarget<{ ready: void }>();
|
|
@@ -361,7 +484,7 @@ const unsubscribe = pipe(componentEvents, appBus);
|
|
|
361
484
|
unsubscribe();
|
|
362
485
|
```
|
|
363
486
|
|
|
364
|
-
Note: `pipe(source, target)`
|
|
487
|
+
Note: Both `pipe(source, target)` and `events.pipe(target)` forward all events via a wildcard listener. Use a map function to transform events or return `null` to filter.
|
|
365
488
|
|
|
366
489
|
## React integration
|
|
367
490
|
|
|
@@ -451,26 +574,36 @@ const state = useObservable(store);
|
|
|
451
574
|
|
|
452
575
|
Checks if an object is an observed proxy.
|
|
453
576
|
|
|
577
|
+
```typescript
|
|
578
|
+
import { isObserved } from 'event-emission/observe';
|
|
579
|
+
```
|
|
580
|
+
|
|
454
581
|
### `getOriginal(proxy)`
|
|
455
582
|
|
|
456
583
|
Returns the original unproxied object.
|
|
457
584
|
|
|
585
|
+
```typescript
|
|
586
|
+
import { getOriginal } from 'event-emission/observe';
|
|
587
|
+
```
|
|
588
|
+
|
|
458
589
|
## TypeScript types
|
|
459
590
|
|
|
460
591
|
```typescript
|
|
461
592
|
import type {
|
|
462
593
|
EmissionEvent,
|
|
463
594
|
EventTargetLike,
|
|
464
|
-
Observable,
|
|
465
595
|
ObservableLike,
|
|
466
596
|
Observer,
|
|
467
597
|
Subscription,
|
|
468
598
|
WildcardEvent,
|
|
469
599
|
AddEventListenerOptionsLike,
|
|
600
|
+
} from 'event-emission/types';
|
|
601
|
+
|
|
602
|
+
import type {
|
|
470
603
|
ObservableEventMap,
|
|
471
604
|
PropertyChangeDetail,
|
|
472
605
|
ArrayMutationDetail,
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
} from 'event-emission';
|
|
606
|
+
} from 'event-emission/observe';
|
|
607
|
+
|
|
608
|
+
import type { Subscriber, SubscriptionObserver } from 'event-emission/observable';
|
|
476
609
|
```
|
package/dist/event-emission.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SymbolObservable } from './symbols';
|
|
2
|
-
import type { AddEventListenerOptionsLike, EmissionEvent, EventsIteratorOptions, EventTargetLike, ObservableLike, Observer, OnOptions, Subscription, WildcardEvent } from './types';
|
|
2
|
+
import type { AddEventListenerOptionsLike, DispatchEventInput, DOMEventLike, EmissionEvent, EventListenerLike, EventListenerOptionsLike, EventsIteratorOptions, EventTargetLike, ObservableLike, Observer, OnOptions, Subscription, WildcardEvent } from './types';
|
|
3
3
|
/**
|
|
4
4
|
* Abstract base class for typed event emitters with DOM EventTarget
|
|
5
5
|
* and TC39 Observable compatibility.
|
|
@@ -7,7 +7,7 @@ import type { AddEventListenerOptionsLike, EmissionEvent, EventsIteratorOptions,
|
|
|
7
7
|
* Extend this class to create custom event emitters with typed events.
|
|
8
8
|
* The class provides:
|
|
9
9
|
* - DOM EventTarget compatible API (addEventListener, removeEventListener, dispatchEvent)
|
|
10
|
-
* - TC39 Observable
|
|
10
|
+
* - TC39 Observable interoperability (subscribe, Symbol.observable)
|
|
11
11
|
* - Async iteration support (events() method)
|
|
12
12
|
* - Wildcard listeners for namespaced events
|
|
13
13
|
* - Lifecycle management (complete(), completed)
|
|
@@ -39,7 +39,7 @@ import type { AddEventListenerOptionsLike, EmissionEvent, EventsIteratorOptions,
|
|
|
39
39
|
* });
|
|
40
40
|
* ```
|
|
41
41
|
*
|
|
42
|
-
* @example TC39 Observable
|
|
42
|
+
* @example TC39 Observable interoperability
|
|
43
43
|
* ```typescript
|
|
44
44
|
* const service = new UserService();
|
|
45
45
|
*
|
|
@@ -67,7 +67,7 @@ import type { AddEventListenerOptionsLike, EmissionEvent, EventsIteratorOptions,
|
|
|
67
67
|
export declare abstract class EventEmission<E extends Record<string, any>> {
|
|
68
68
|
#private;
|
|
69
69
|
/**
|
|
70
|
-
* Returns this observable for Symbol.observable
|
|
70
|
+
* Returns this observable for Symbol.observable interoperability.
|
|
71
71
|
*/
|
|
72
72
|
[SymbolObservable]: () => ObservableLike<EmissionEvent<E[keyof E]>>;
|
|
73
73
|
constructor();
|
|
@@ -75,25 +75,30 @@ export declare abstract class EventEmission<E extends Record<string, any>> {
|
|
|
75
75
|
* Adds an event listener for the specified event type.
|
|
76
76
|
* Returns an unsubscribe function for convenience.
|
|
77
77
|
*/
|
|
78
|
-
addEventListener<K extends keyof E & string>(type: K, listener:
|
|
78
|
+
addEventListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>> | null, options?: AddEventListenerOptionsLike | boolean): () => void;
|
|
79
79
|
/**
|
|
80
80
|
* Removes an event listener for the specified event type.
|
|
81
81
|
*/
|
|
82
|
-
removeEventListener<K extends keyof E & string>(type: K, listener:
|
|
82
|
+
removeEventListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>> | null, options?: EventListenerOptionsLike | boolean): void;
|
|
83
83
|
/**
|
|
84
84
|
* Dispatches an event to all registered listeners.
|
|
85
85
|
* Returns false if the emitter has been completed, true otherwise.
|
|
86
86
|
*/
|
|
87
|
-
dispatchEvent<K extends keyof E & string>(event:
|
|
87
|
+
dispatchEvent<K extends keyof E & string>(event: DispatchEventInput<E[K], K> | DOMEventLike): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Adds a listener for the specified event type (Node.js-style).
|
|
90
|
+
* Returns an unsubscribe function.
|
|
91
|
+
*/
|
|
92
|
+
on<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): () => void;
|
|
88
93
|
/**
|
|
89
94
|
* Returns an observable for the specified event type.
|
|
90
95
|
*/
|
|
91
|
-
on<K extends keyof E & string>(type: K, options?: OnOptions | boolean): ObservableLike<EmissionEvent<E[K]>>;
|
|
96
|
+
on<K extends keyof E & string>(type: K, options?: OnOptions | boolean): ObservableLike<EmissionEvent<E[K], K>>;
|
|
92
97
|
/**
|
|
93
98
|
* Adds a one-time listener for the specified event type.
|
|
94
99
|
* Returns an unsubscribe function.
|
|
95
100
|
*/
|
|
96
|
-
once<K extends keyof E & string>(type: K, listener:
|
|
101
|
+
once<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>> | null, options?: Omit<AddEventListenerOptionsLike, 'once'> | boolean): () => void;
|
|
97
102
|
/**
|
|
98
103
|
* Removes all listeners, or those of the specified event type.
|
|
99
104
|
*/
|
|
@@ -104,10 +109,54 @@ export declare abstract class EventEmission<E extends Record<string, any>> {
|
|
|
104
109
|
clear(): void;
|
|
105
110
|
/**
|
|
106
111
|
* Pipe events from this emitter to another target.
|
|
107
|
-
*
|
|
108
|
-
|
|
112
|
+
* Forwards all events. If mapFn returns null, the event is skipped.
|
|
113
|
+
*/
|
|
114
|
+
pipe<T extends Record<string, any>>(target: EventTargetLike<T>, mapFn?: <K extends keyof E & string>(event: EmissionEvent<E[K], K>) => DispatchEventInput<T[keyof T & string], keyof T & string> | null): () => void;
|
|
115
|
+
/**
|
|
116
|
+
* Emits an event with the given type and detail.
|
|
117
|
+
* Returns true if the event type had registered listeners, false otherwise.
|
|
118
|
+
*/
|
|
119
|
+
emit<K extends keyof E & string>(type: K, detail: E[K]): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Removes a listener for the specified event type.
|
|
122
|
+
*/
|
|
123
|
+
off<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Adds a listener for the specified event type.
|
|
126
|
+
* Returns an unsubscribe function.
|
|
127
|
+
*/
|
|
128
|
+
addListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): () => void;
|
|
129
|
+
/**
|
|
130
|
+
* Removes a listener for the specified event type.
|
|
131
|
+
*/
|
|
132
|
+
removeListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): void;
|
|
133
|
+
/**
|
|
134
|
+
* Adds a listener at the beginning of the listener list for the specified type.
|
|
135
|
+
* Returns an unsubscribe function.
|
|
136
|
+
*/
|
|
137
|
+
prependListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): () => void;
|
|
138
|
+
/**
|
|
139
|
+
* Adds a one-time listener at the beginning of the listener list.
|
|
140
|
+
* Returns an unsubscribe function.
|
|
141
|
+
*/
|
|
142
|
+
prependOnceListener<K extends keyof E & string>(type: K, listener: EventListenerLike<EmissionEvent<E[K], K>>): () => void;
|
|
143
|
+
/**
|
|
144
|
+
* Returns an array of listener functions for the specified event type.
|
|
145
|
+
*/
|
|
146
|
+
listeners<K extends keyof E & string>(type: K): Array<EventListenerLike<EmissionEvent<E[K], K>>>;
|
|
147
|
+
/**
|
|
148
|
+
* Returns an array of listener functions for the specified event type.
|
|
149
|
+
* Once-listeners return a wrapper with a `.listener` property pointing to the original.
|
|
150
|
+
*/
|
|
151
|
+
rawListeners<K extends keyof E & string>(type: K): Array<EventListenerLike<EmissionEvent<E[K], K>>>;
|
|
152
|
+
/**
|
|
153
|
+
* Returns the number of listeners for the specified event type.
|
|
154
|
+
*/
|
|
155
|
+
listenerCount<K extends keyof E & string>(type: K): number;
|
|
156
|
+
/**
|
|
157
|
+
* Returns an array of event type names that have registered listeners.
|
|
109
158
|
*/
|
|
110
|
-
|
|
159
|
+
eventNames(): Array<string>;
|
|
111
160
|
/**
|
|
112
161
|
* Adds a wildcard listener that receives events matching the pattern.
|
|
113
162
|
* Use '*' for all events, or 'namespace:*' for namespaced events.
|
|
@@ -124,7 +173,7 @@ export declare abstract class EventEmission<E extends Record<string, any>> {
|
|
|
124
173
|
/**
|
|
125
174
|
* Subscribes an observer to events of a specific type (typed).
|
|
126
175
|
*/
|
|
127
|
-
subscribe<K extends keyof E & string>(type: K, observerOrNext?: Observer<EmissionEvent<E[K]>> | ((value: EmissionEvent<E[K]>) => void), error?: (err: unknown) => void, completeHandler?: () => void): Subscription;
|
|
176
|
+
subscribe<K extends keyof E & string>(type: K, observerOrNext?: Observer<EmissionEvent<E[K], K>> | ((value: EmissionEvent<E[K], K>) => void), error?: (err: unknown) => void, completeHandler?: () => void): Subscription;
|
|
128
177
|
/**
|
|
129
178
|
* Returns an observable that emits all events.
|
|
130
179
|
*/
|
|
@@ -152,6 +201,6 @@ export declare abstract class EventEmission<E extends Record<string, any>> {
|
|
|
152
201
|
* }
|
|
153
202
|
* ```
|
|
154
203
|
*/
|
|
155
|
-
events<K extends keyof E & string>(type: K, options?: EventsIteratorOptions): AsyncIterableIterator<EmissionEvent<E[K]>>;
|
|
204
|
+
events<K extends keyof E & string>(type: K, options?: EventsIteratorOptions): AsyncIterableIterator<EmissionEvent<E[K], K>>;
|
|
156
205
|
}
|
|
157
206
|
//# sourceMappingURL=event-emission.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"event-emission.d.ts","sourceRoot":"","sources":["../src/event-emission.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CreateEventTargetOptions with proxy observation settings.
|
|
16
16
|
*
|
|
17
17
|
* @property observe - Must be true to enable observation mode.
|
|
18
|
-
* @property deep - If true, nested objects are also observed (default:
|
|
18
|
+
* @property deep - If true, nested objects are also observed (default: true).
|
|
19
19
|
* @property cloneStrategy - Strategy for cloning previous state: 'shallow', 'deep', or 'path'.
|
|
20
20
|
*/
|
|
21
21
|
export interface CreateEventTargetObserveOptions extends CreateEventTargetOptions, ObserveOptions {
|
package/dist/factory.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACpB,MAAM,WAAW,CAAC;AAEnB,OAAO,KAAK,EAKV,eAAe,EAOhB,MAAM,SAAS,CAAC;AAyCjB;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB;IACvC,+EAA+E;IAC/E,eAAe,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC1D;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,+BACf,SAAQ,wBAAwB,EAAE,cAAc;IAChD,+CAA+C;IAC/C,OAAO,EAAE,IAAI,CAAC;CACf;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AAEH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAC7D,IAAI,CAAC,EAAE,wBAAwB,GAC9B,eAAe,CAAC,CAAC,CAAC,CAAC;AAEtB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,SAAS,MAAM,EAChD,MAAM,EAAE,CAAC,EACT,IAAI,EAAE,+BAA+B,GACpC,CAAC,GAAG,eAAe,CAAC,kBAAkB,CAAC,CAAC,CAAC,CAAC,CAAC"}
|