bare-events 2.9.2 → 2.10.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 +4 -0
- package/index.d.ts +152 -0
- package/index.js +36 -12
- package/package.json +1 -1
- package/web.d.ts +29 -0
package/README.md
CHANGED
package/index.d.ts
CHANGED
|
@@ -8,59 +8,192 @@ interface EventHandler<in A extends unknown[] = unknown[], out R = unknown> {
|
|
|
8
8
|
(...args: A): R
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* The error class used for emitter-internal errors, such as an unhandled `'error'` event or an
|
|
13
|
+
* aborted `events.on`/`events.once` wait.
|
|
14
|
+
*/
|
|
11
15
|
declare class EventEmitterError extends Error {
|
|
16
|
+
/**
|
|
17
|
+
* Create an `EventEmitterError` wrapping `cause`, used when an `AbortSignal` aborts a pending
|
|
18
|
+
* `events.on` or `events.once` wait.
|
|
19
|
+
* @param cause - The abort reason wrapped by the error.
|
|
20
|
+
* @param msg - An optional custom message for the wrapping error.
|
|
21
|
+
*/
|
|
12
22
|
static OPERATION_ABORTED(cause: Error, msg?: string): EventEmitterError
|
|
23
|
+
/**
|
|
24
|
+
* Create an `EventEmitterError` wrapping `cause`, used when an `'error'` event is emitted with no
|
|
25
|
+
* listeners attached.
|
|
26
|
+
* @param cause - The underlying error wrapped by the error.
|
|
27
|
+
* @param msg - An optional custom message for the wrapping error.
|
|
28
|
+
*/
|
|
13
29
|
static UNHANDLED_ERROR(cause: Error, msg?: string): EventEmitterError
|
|
14
30
|
}
|
|
15
31
|
|
|
32
|
+
/**
|
|
33
|
+
* An emitter of named events to which listener functions can be attached, closely mirroring
|
|
34
|
+
* Node.js's `EventEmitter`.
|
|
35
|
+
*/
|
|
16
36
|
interface EventEmitter<in out M extends Record<keyof M, unknown[]> = EventMap> {
|
|
37
|
+
/**
|
|
38
|
+
* Add `fn` as a listener for `name`, called on every subsequent emit. Returns `this` for
|
|
39
|
+
* chaining.
|
|
40
|
+
* @param name - The event name to listen for.
|
|
41
|
+
* @param fn - The listener function, called with the event's arguments on each emit.
|
|
42
|
+
*/
|
|
17
43
|
addListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
18
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Add `fn` as a listener for `name` that is removed after it fires once. Returns `this` for
|
|
47
|
+
* chaining.
|
|
48
|
+
* @param name - The event name to listen for.
|
|
49
|
+
* @param fn - The listener function, called once with the event's arguments then removed.
|
|
50
|
+
*/
|
|
19
51
|
addOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
20
52
|
|
|
53
|
+
/**
|
|
54
|
+
* Like `addListener`, but adds `fn` to the beginning of the listener list instead of the end.
|
|
55
|
+
* @param name - The event name to listen for.
|
|
56
|
+
* @param fn - The listener function to add to the front of the listener list instead of the end.
|
|
57
|
+
* @returns The emitter itself, for chaining, like `addListener`.
|
|
58
|
+
*/
|
|
21
59
|
prependListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
22
60
|
|
|
61
|
+
/**
|
|
62
|
+
* Like `addOnceListener`, but adds `fn` to the beginning of the listener list instead of the end.
|
|
63
|
+
* @param name - The event name to listen for.
|
|
64
|
+
* @param fn - The listener function to add to the front of the listener list, removed after it
|
|
65
|
+
* fires once.
|
|
66
|
+
* @returns The emitter itself, for chaining, like `addOnceListener`.
|
|
67
|
+
*/
|
|
23
68
|
prependOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
24
69
|
|
|
70
|
+
/**
|
|
71
|
+
* Remove one instance of `fn` from the listeners for `name`. Returns `this` for chaining.
|
|
72
|
+
* @param name - The event name to remove the listener from.
|
|
73
|
+
* @param fn - The listener function to remove.
|
|
74
|
+
*/
|
|
25
75
|
removeListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
26
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Remove all listeners, or only those for `name` if given. Returns `this` for chaining.
|
|
79
|
+
* @param name - If given, remove listeners only for this event name; otherwise remove all
|
|
80
|
+
* listeners for every event.
|
|
81
|
+
*/
|
|
27
82
|
removeAllListeners<E extends keyof M>(name?: E): this
|
|
28
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Alias for `addListener`.
|
|
86
|
+
* @param name - The event name to listen for.
|
|
87
|
+
* @param fn - The listener function, called with the event's arguments on each emit.
|
|
88
|
+
* @returns The emitter itself, for chaining — implemented identically to `addListener`.
|
|
89
|
+
*/
|
|
29
90
|
on<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
30
91
|
|
|
92
|
+
/**
|
|
93
|
+
* Alias for `addOnceListener`.
|
|
94
|
+
* @param name - The event name to listen for.
|
|
95
|
+
* @param fn - The listener function, called once with the event's arguments then removed.
|
|
96
|
+
* @returns The emitter itself, for chaining — implemented identically to `addOnceListener`.
|
|
97
|
+
*/
|
|
31
98
|
once<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
32
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Alias for `removeListener`.
|
|
102
|
+
* @param name - The event name to remove the listener from.
|
|
103
|
+
* @param fn - The listener function to remove.
|
|
104
|
+
* @returns The emitter itself, for chaining — implemented identically to `removeListener`.
|
|
105
|
+
*/
|
|
33
106
|
off<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
34
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Synchronously call each listener registered for `name`, in registration order, with `args`.
|
|
110
|
+
* Returns `true` if there were listeners, `false` otherwise. Emitting `'error'` with no `'error'`
|
|
111
|
+
* listeners throws the error asynchronously instead of calling any listener.
|
|
112
|
+
* @param name - The event name to emit.
|
|
113
|
+
* @param args - Arguments passed to each listener registered for `name`.
|
|
114
|
+
*/
|
|
35
115
|
emit<E extends keyof M>(name: E, ...args: M[E]): boolean
|
|
36
116
|
|
|
117
|
+
/**
|
|
118
|
+
* Return a copy of the listener array for `name`.
|
|
119
|
+
* @param name - The event name to return the listener array for.
|
|
120
|
+
*/
|
|
37
121
|
listeners<E extends keyof M>(name: E): EventHandler<M[E]>[]
|
|
38
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Return a copy of the listener array for `name`, including one-time wrapper listeners as
|
|
125
|
+
* registered.
|
|
126
|
+
* @param name - The event name to return the raw listener array for.
|
|
127
|
+
*/
|
|
39
128
|
rawListeners<E extends keyof M>(name: E): EventHandler<M[E]>[]
|
|
40
129
|
|
|
130
|
+
/** Return an array of the event names that currently have listeners. */
|
|
41
131
|
eventNames(): (keyof M)[]
|
|
42
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Return the number of listeners registered for `name`, or `0` if there are none.
|
|
135
|
+
* @param name - The event name to count listeners for.
|
|
136
|
+
*/
|
|
43
137
|
listenerCount<E extends keyof M>(name: E): number
|
|
44
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Return the maximum number of listeners allowed for this emitter.
|
|
141
|
+
* @returns `EventEmitter.defaultMaxListeners`; bare-events does not track a per-instance limit
|
|
142
|
+
* separately.
|
|
143
|
+
*/
|
|
45
144
|
getMaxListeners(): number
|
|
145
|
+
/**
|
|
146
|
+
* Set the maximum number of listeners for this emitter. Accepted for compatibility, but the
|
|
147
|
+
* limit is not recorded and the call has no effect.
|
|
148
|
+
* @param n - The maximum number of listeners to allow.
|
|
149
|
+
*/
|
|
46
150
|
setMaxListeners(n: number): this
|
|
47
151
|
}
|
|
48
152
|
|
|
49
153
|
declare class EventEmitter<in out M extends Record<keyof M, unknown[]> = EventMap> {}
|
|
50
154
|
|
|
51
155
|
declare namespace EventEmitter {
|
|
156
|
+
/**
|
|
157
|
+
* Iterate the `name` events emitted by `emitter`, yielding the arguments of each emit as an
|
|
158
|
+
* array. Unless `name` is `'error'`, an `'error'` event on `emitter` ends the iteration by
|
|
159
|
+
* rejecting with the emitted error.
|
|
160
|
+
* @param emitter - The emitter to iterate events from.
|
|
161
|
+
* @param name - The event name to iterate.
|
|
162
|
+
* @param opts - Options; `signal` aborts the iteration, rejecting it with an `EventEmitterError`.
|
|
163
|
+
* @returns An async iterator of the emitted arguments, which stops listening once it is returned
|
|
164
|
+
* from or throws.
|
|
165
|
+
* @throws If `opts.signal` has already been aborted.
|
|
166
|
+
*/
|
|
52
167
|
export function on<M extends Record<keyof M, unknown[]>, E extends keyof M>(
|
|
53
168
|
emitter: EventEmitter<M>,
|
|
54
169
|
name: E,
|
|
55
170
|
opts?: { signal?: AbortSignal }
|
|
56
171
|
): AsyncIterableIterator<M[E]>
|
|
57
172
|
|
|
173
|
+
/**
|
|
174
|
+
* Wait for the next `name` event emitted by `emitter`. Unless `name` is `'error'`, an `'error'`
|
|
175
|
+
* event on `emitter` rejects the wait with the emitted error instead.
|
|
176
|
+
* @param emitter - The emitter to wait on.
|
|
177
|
+
* @param name - The event name to wait for.
|
|
178
|
+
* @param opts - Options; `signal` aborts the wait, rejecting the promise with an
|
|
179
|
+
* `EventEmitterError`.
|
|
180
|
+
* @returns A promise resolving with the arguments of the next emit, as an array.
|
|
181
|
+
*/
|
|
58
182
|
export function once<M extends Record<keyof M, unknown[]>, E extends keyof M>(
|
|
59
183
|
emitter: EventEmitter<M>,
|
|
60
184
|
name: E,
|
|
61
185
|
opts?: { signal?: AbortSignal }
|
|
62
186
|
): Promise<M[E]>
|
|
63
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Forward events named in `names` from `from` to `to`, re-emitting them on `to` only while `to`
|
|
190
|
+
* has at least one listener for that event.
|
|
191
|
+
* @param from - The emitter to forward events from.
|
|
192
|
+
* @param to - The emitter to forward events to.
|
|
193
|
+
* @param names - The event name, or array of event names, to forward.
|
|
194
|
+
* @param opts - Options; `emit` overrides how forwarded events are re-emitted on `to` (defaults
|
|
195
|
+
* to `to.emit`).
|
|
196
|
+
*/
|
|
64
197
|
export function forward<
|
|
65
198
|
F extends Record<keyof F, unknown[]>,
|
|
66
199
|
E extends keyof F,
|
|
@@ -72,15 +205,34 @@ declare namespace EventEmitter {
|
|
|
72
205
|
opts?: { emit?: (name: E, ...args: T[E]) => void }
|
|
73
206
|
): void
|
|
74
207
|
|
|
208
|
+
/**
|
|
209
|
+
* Return the number of listeners registered on `emitter` for `name`, by calling the emitter's
|
|
210
|
+
* own `listenerCount` method.
|
|
211
|
+
* @param emitter - The emitter to query.
|
|
212
|
+
* @param name - The event name to count listeners for.
|
|
213
|
+
*/
|
|
75
214
|
export function listenerCount<M extends Record<keyof M, unknown[]>, E extends keyof M>(
|
|
76
215
|
emitter: EventEmitter<M>,
|
|
77
216
|
name: E
|
|
78
217
|
): number
|
|
79
218
|
|
|
219
|
+
/**
|
|
220
|
+
* Return the maximum number of listeners for `emitter`.
|
|
221
|
+
* @param emitter - The emitter to query.
|
|
222
|
+
* @returns `emitter.getMaxListeners()` if the emitter has that method, otherwise
|
|
223
|
+
* `EventEmitter.defaultMaxListeners`.
|
|
224
|
+
*/
|
|
80
225
|
export function getMaxListeners(emitter: EventEmitter<any>): number
|
|
81
226
|
|
|
227
|
+
/**
|
|
228
|
+
* Set the maximum number of listeners on each of `emitters` that has a `setMaxListeners` method.
|
|
229
|
+
* @param n - The maximum number of listeners to allow.
|
|
230
|
+
* @param emitters - The emitters to apply the new limit to; if omitted, sets
|
|
231
|
+
* `EventEmitter.defaultMaxListeners` instead.
|
|
232
|
+
*/
|
|
82
233
|
export function setMaxListeners(n: number, ...emitters: EventEmitter<any>[]): void
|
|
83
234
|
|
|
235
|
+
/** The default max-listeners value used by `getMaxListeners()`. */
|
|
84
236
|
export let defaultMaxListeners: number
|
|
85
237
|
|
|
86
238
|
export { EventEmitter, EventEmitterError as errors, EventMap, EventHandler }
|
package/index.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
const errors = require('./lib/errors')
|
|
2
2
|
|
|
3
|
+
class EventListenerEntry {
|
|
4
|
+
constructor(fn, once) {
|
|
5
|
+
this.fn = fn
|
|
6
|
+
this.once = once
|
|
7
|
+
this.fired = false
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
3
11
|
class EventListener {
|
|
4
12
|
constructor() {
|
|
5
13
|
this.list = []
|
|
@@ -8,29 +16,40 @@ class EventListener {
|
|
|
8
16
|
|
|
9
17
|
append(ctx, name, fn, once) {
|
|
10
18
|
this.count++
|
|
19
|
+
ctx._newListener(name, fn)
|
|
11
20
|
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
12
|
-
|
|
21
|
+
|
|
22
|
+
const l = new EventListenerEntry(fn, once)
|
|
23
|
+
|
|
24
|
+
if (this.list.length === 0) this.list = [l]
|
|
25
|
+
else this.list.push(l)
|
|
13
26
|
}
|
|
14
27
|
|
|
15
28
|
prepend(ctx, name, fn, once) {
|
|
16
29
|
this.count++
|
|
30
|
+
ctx._newListener(name, fn)
|
|
17
31
|
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
18
|
-
|
|
32
|
+
|
|
33
|
+
const l = new EventListenerEntry(fn, once)
|
|
34
|
+
|
|
35
|
+
if (this.list.length === 0) this.list = [l]
|
|
36
|
+
else this.list.unshift(l)
|
|
19
37
|
}
|
|
20
38
|
|
|
21
39
|
remove(ctx, name, fn) {
|
|
22
40
|
for (let i = this.list.length - 1; i >= 0; i--) {
|
|
23
|
-
if (this.list[i]
|
|
41
|
+
if (this.list[i].fn === fn) return this.removeAt(ctx, name, i)
|
|
24
42
|
}
|
|
25
43
|
}
|
|
26
44
|
|
|
27
45
|
removeAt(ctx, name, i) {
|
|
28
|
-
const fn = this.list[i]
|
|
46
|
+
const fn = this.list[i].fn
|
|
29
47
|
|
|
30
48
|
this.list.splice(i, 1)
|
|
31
49
|
|
|
32
50
|
if (this.count === 1) delete ctx._events[name]
|
|
33
51
|
|
|
52
|
+
ctx._removeListener(name, fn)
|
|
34
53
|
ctx.emit('removeListener', name, fn) // Emit AFTER removing
|
|
35
54
|
|
|
36
55
|
this.count--
|
|
@@ -43,7 +62,8 @@ class EventListener {
|
|
|
43
62
|
if (this.count === list.length) delete ctx._events[name]
|
|
44
63
|
|
|
45
64
|
for (let i = list.length - 1; i >= 0; i--) {
|
|
46
|
-
ctx.
|
|
65
|
+
ctx._removeListener(name, list[i].fn)
|
|
66
|
+
ctx.emit('removeListener', name, list[i].fn) // Emit AFTER removing
|
|
47
67
|
}
|
|
48
68
|
|
|
49
69
|
this.count -= list.length
|
|
@@ -55,12 +75,12 @@ class EventListener {
|
|
|
55
75
|
for (let i = 0, n = list.length; i < n; i++) {
|
|
56
76
|
const l = list[i]
|
|
57
77
|
|
|
58
|
-
if (l
|
|
78
|
+
if (l.once) {
|
|
59
79
|
// A reentrant emit may already have fired this listener, in which case
|
|
60
80
|
// it must not be fired again.
|
|
61
|
-
if (l
|
|
81
|
+
if (l.fired) continue
|
|
62
82
|
|
|
63
|
-
l
|
|
83
|
+
l.fired = true
|
|
64
84
|
|
|
65
85
|
// Remove this entry, rather than the first one with the same listener,
|
|
66
86
|
// as the same listener may have been added more than once.
|
|
@@ -69,7 +89,7 @@ class EventListener {
|
|
|
69
89
|
if (j !== -1) this.removeAt(ctx, name, j)
|
|
70
90
|
}
|
|
71
91
|
|
|
72
|
-
Reflect.apply(l
|
|
92
|
+
Reflect.apply(l.fn, ctx, args)
|
|
73
93
|
}
|
|
74
94
|
|
|
75
95
|
return list.length > 0
|
|
@@ -115,9 +135,13 @@ function throwUnhandledError(...args) {
|
|
|
115
135
|
|
|
116
136
|
module.exports = exports = class EventEmitter {
|
|
117
137
|
constructor() {
|
|
118
|
-
this._events =
|
|
138
|
+
this._events = undefined
|
|
119
139
|
}
|
|
120
140
|
|
|
141
|
+
_newListener(name, fn) {}
|
|
142
|
+
|
|
143
|
+
_removeListener(name, fn) {}
|
|
144
|
+
|
|
121
145
|
addListener(name, fn) {
|
|
122
146
|
return appendListener(this, name, fn, false)
|
|
123
147
|
}
|
|
@@ -163,13 +187,13 @@ module.exports = exports = class EventEmitter {
|
|
|
163
187
|
listeners(name) {
|
|
164
188
|
if (this._events === undefined) return []
|
|
165
189
|
const e = this._events[name]
|
|
166
|
-
return e === undefined ? [] : e.list.map((l) => l
|
|
190
|
+
return e === undefined ? [] : e.list.map((l) => l.fn)
|
|
167
191
|
}
|
|
168
192
|
|
|
169
193
|
rawListeners(name) {
|
|
170
194
|
if (this._events === undefined) return []
|
|
171
195
|
const e = this._events[name]
|
|
172
|
-
return e === undefined ? [] : e.list.map((l) => l
|
|
196
|
+
return e === undefined ? [] : e.list.map((l) => l.fn)
|
|
173
197
|
}
|
|
174
198
|
|
|
175
199
|
eventNames() {
|
package/package.json
CHANGED
package/web.d.ts
CHANGED
|
@@ -22,6 +22,10 @@ export interface Event {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export class Event {
|
|
25
|
+
/**
|
|
26
|
+
* @param type - The event's type, exposed as `event.type`.
|
|
27
|
+
* @param options - Options controlling `bubbles`, `cancelable`, and `composed`.
|
|
28
|
+
*/
|
|
25
29
|
constructor(type: string, options?: EventOptions)
|
|
26
30
|
}
|
|
27
31
|
|
|
@@ -34,12 +38,17 @@ export interface CustomEvent<T = any> extends Event {
|
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
export class CustomEvent<T = any> {
|
|
41
|
+
/**
|
|
42
|
+
* @param type - The event's type, exposed as `event.type`.
|
|
43
|
+
* @param options - Options controlling `bubbles`/`cancelable`/`composed` plus the `detail` value.
|
|
44
|
+
*/
|
|
37
45
|
constructor(type: string, options?: CustomEventOptions<T>)
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
export interface AddEventListenerOptions {
|
|
41
49
|
capture?: boolean
|
|
42
50
|
passive?: boolean
|
|
51
|
+
/** Whether the listener is removed after being invoked once. Defaults to `false`. */
|
|
43
52
|
once?: boolean
|
|
44
53
|
signal?: AbortSignal | null
|
|
45
54
|
}
|
|
@@ -49,18 +58,35 @@ export interface RemoveEventListenerOptions {
|
|
|
49
58
|
}
|
|
50
59
|
|
|
51
60
|
export interface EventTarget {
|
|
61
|
+
/**
|
|
62
|
+
* @param type - The event type to listen for.
|
|
63
|
+
* @param callback - The listener function, or an object with a `handleEvent` method.
|
|
64
|
+
* @param options - Options, or a boolean shorthand for `capture`; `once` removes the listener
|
|
65
|
+
* after it fires, `signal` removes it when the given `AbortSignal` aborts.
|
|
66
|
+
*/
|
|
52
67
|
addEventListener(
|
|
53
68
|
type: string,
|
|
54
69
|
callback: EventListener,
|
|
55
70
|
options?: AddEventListenerOptions | boolean
|
|
56
71
|
): void
|
|
57
72
|
|
|
73
|
+
/**
|
|
74
|
+
* @param type - The event type to stop listening for.
|
|
75
|
+
* @param callback - The listener to remove.
|
|
76
|
+
* @param options - Options, or a boolean shorthand for `capture`; must match the `capture` value
|
|
77
|
+
* passed to `addEventListener`.
|
|
78
|
+
*/
|
|
58
79
|
removeEventListener(
|
|
59
80
|
type: string,
|
|
60
81
|
callback: EventListener,
|
|
61
82
|
options?: RemoveEventListenerOptions | boolean
|
|
62
83
|
): void
|
|
63
84
|
|
|
85
|
+
/**
|
|
86
|
+
* @param event - The event to dispatch to this target's listeners.
|
|
87
|
+
* @returns `false` if the event is cancelable and `preventDefault()` was called on it during
|
|
88
|
+
* dispatch, `true` otherwise.
|
|
89
|
+
*/
|
|
64
90
|
dispatchEvent(event: Event): boolean
|
|
65
91
|
}
|
|
66
92
|
|
|
@@ -75,5 +101,8 @@ export interface EventCallback {
|
|
|
75
101
|
}
|
|
76
102
|
|
|
77
103
|
export interface EventHandler {
|
|
104
|
+
/**
|
|
105
|
+
* @param event - The event passed to the handler.
|
|
106
|
+
*/
|
|
78
107
|
handleEvent(event: Event): void
|
|
79
108
|
}
|