bare-events 2.9.1 → 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 +185 -29
- package/index.js +77 -34
- package/lib/errors.js +7 -10
- package/package.json +1 -1
- package/web.d.ts +29 -0
- package/web.js +66 -2
package/README.md
CHANGED
package/index.d.ts
CHANGED
|
@@ -8,75 +8,231 @@ 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
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
32
|
+
/**
|
|
33
|
+
* An emitter of named events to which listener functions can be attached, closely mirroring
|
|
34
|
+
* Node.js's `EventEmitter`.
|
|
35
|
+
*/
|
|
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
|
+
*/
|
|
43
|
+
addListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
51
|
+
addOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
59
|
+
prependListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
68
|
+
prependOnceListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
75
|
+
removeListener<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
+
*/
|
|
90
|
+
on<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
98
|
+
once<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
+
*/
|
|
106
|
+
off<E extends keyof M>(name: E, fn: EventHandler<M[E]>): this
|
|
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
|
|
|
37
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Return a copy of the listener array for `name`.
|
|
119
|
+
* @param name - The event name to return the listener array for.
|
|
120
|
+
*/
|
|
121
|
+
listeners<E extends keyof M>(name: E): EventHandler<M[E]>[]
|
|
38
122
|
|
|
39
|
-
|
|
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
|
+
*/
|
|
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
|
|
46
|
-
|
|
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
|
+
*/
|
|
150
|
+
setMaxListeners(n: number): this
|
|
47
151
|
}
|
|
48
152
|
|
|
49
|
-
declare class EventEmitter<in out M extends
|
|
153
|
+
declare class EventEmitter<in out M extends Record<keyof M, unknown[]> = EventMap> {}
|
|
50
154
|
|
|
51
155
|
declare namespace EventEmitter {
|
|
52
|
-
|
|
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
|
+
*/
|
|
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
|
|
|
58
|
-
|
|
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
|
+
*/
|
|
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
|
|
|
64
|
-
|
|
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
|
+
*/
|
|
197
|
+
export function forward<
|
|
198
|
+
F extends Record<keyof F, unknown[]>,
|
|
199
|
+
E extends keyof F,
|
|
200
|
+
T extends Record<keyof T, unknown[]> & Pick<F, E>
|
|
201
|
+
>(
|
|
65
202
|
from: EventEmitter<F>,
|
|
66
203
|
to: EventEmitter<T>,
|
|
67
204
|
names: E | E[],
|
|
68
205
|
opts?: { emit?: (name: E, ...args: T[E]) => void }
|
|
69
206
|
): void
|
|
70
207
|
|
|
71
|
-
|
|
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
|
+
*/
|
|
214
|
+
export function listenerCount<M extends Record<keyof M, unknown[]>, E extends keyof M>(
|
|
72
215
|
emitter: EventEmitter<M>,
|
|
73
216
|
name: E
|
|
74
217
|
): number
|
|
75
218
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
+
*/
|
|
225
|
+
export function getMaxListeners(emitter: EventEmitter<any>): number
|
|
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
|
+
*/
|
|
233
|
+
export function setMaxListeners(n: number, ...emitters: EventEmitter<any>[]): void
|
|
234
|
+
|
|
235
|
+
/** The default max-listeners value used by `getMaxListeners()`. */
|
|
80
236
|
export let defaultMaxListeners: number
|
|
81
237
|
|
|
82
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,31 +16,43 @@ 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
|
-
for (let i =
|
|
23
|
-
|
|
40
|
+
for (let i = this.list.length - 1; i >= 0; i--) {
|
|
41
|
+
if (this.list[i].fn === fn) return this.removeAt(ctx, name, i)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
24
44
|
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
removeAt(ctx, name, i) {
|
|
46
|
+
const fn = this.list[i].fn
|
|
27
47
|
|
|
28
|
-
|
|
48
|
+
this.list.splice(i, 1)
|
|
29
49
|
|
|
30
|
-
|
|
50
|
+
if (this.count === 1) delete ctx._events[name]
|
|
31
51
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
52
|
+
ctx._removeListener(name, fn)
|
|
53
|
+
ctx.emit('removeListener', name, fn) // Emit AFTER removing
|
|
54
|
+
|
|
55
|
+
this.count--
|
|
36
56
|
}
|
|
37
57
|
|
|
38
58
|
removeAll(ctx, name) {
|
|
@@ -42,7 +62,8 @@ class EventListener {
|
|
|
42
62
|
if (this.count === list.length) delete ctx._events[name]
|
|
43
63
|
|
|
44
64
|
for (let i = list.length - 1; i >= 0; i--) {
|
|
45
|
-
ctx.
|
|
65
|
+
ctx._removeListener(name, list[i].fn)
|
|
66
|
+
ctx.emit('removeListener', name, list[i].fn) // Emit AFTER removing
|
|
46
67
|
}
|
|
47
68
|
|
|
48
69
|
this.count -= list.length
|
|
@@ -54,9 +75,21 @@ class EventListener {
|
|
|
54
75
|
for (let i = 0, n = list.length; i < n; i++) {
|
|
55
76
|
const l = list[i]
|
|
56
77
|
|
|
57
|
-
if (l
|
|
78
|
+
if (l.once) {
|
|
79
|
+
// A reentrant emit may already have fired this listener, in which case
|
|
80
|
+
// it must not be fired again.
|
|
81
|
+
if (l.fired) continue
|
|
58
82
|
|
|
59
|
-
|
|
83
|
+
l.fired = true
|
|
84
|
+
|
|
85
|
+
// Remove this entry, rather than the first one with the same listener,
|
|
86
|
+
// as the same listener may have been added more than once.
|
|
87
|
+
const j = this.list.indexOf(l)
|
|
88
|
+
|
|
89
|
+
if (j !== -1) this.removeAt(ctx, name, j)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
Reflect.apply(l.fn, ctx, args)
|
|
60
93
|
}
|
|
61
94
|
|
|
62
95
|
return list.length > 0
|
|
@@ -89,10 +122,10 @@ function throwUnhandledError(...args) {
|
|
|
89
122
|
|
|
90
123
|
if (args.length > 0) err = args[0]
|
|
91
124
|
|
|
92
|
-
if (err instanceof Error === false)
|
|
125
|
+
if (err instanceof Error === false) {
|
|
126
|
+
err = errors.UNHANDLED_ERROR(err)
|
|
93
127
|
|
|
94
|
-
|
|
95
|
-
Error.captureStackTrace(err, exports.prototype.emit)
|
|
128
|
+
if (Error.captureStackTrace) Error.captureStackTrace(err, exports.prototype.emit)
|
|
96
129
|
}
|
|
97
130
|
|
|
98
131
|
queueMicrotask(() => {
|
|
@@ -102,9 +135,13 @@ function throwUnhandledError(...args) {
|
|
|
102
135
|
|
|
103
136
|
module.exports = exports = class EventEmitter {
|
|
104
137
|
constructor() {
|
|
105
|
-
this._events =
|
|
138
|
+
this._events = undefined
|
|
106
139
|
}
|
|
107
140
|
|
|
141
|
+
_newListener(name, fn) {}
|
|
142
|
+
|
|
143
|
+
_removeListener(name, fn) {}
|
|
144
|
+
|
|
108
145
|
addListener(name, fn) {
|
|
109
146
|
return appendListener(this, name, fn, false)
|
|
110
147
|
}
|
|
@@ -138,7 +175,7 @@ module.exports = exports = class EventEmitter {
|
|
|
138
175
|
}
|
|
139
176
|
|
|
140
177
|
emit(name, ...args) {
|
|
141
|
-
if (name === 'error' && this._events
|
|
178
|
+
if (name === 'error' && (this._events === undefined || this._events.error === undefined)) {
|
|
142
179
|
throwUnhandledError(...args)
|
|
143
180
|
}
|
|
144
181
|
|
|
@@ -150,13 +187,13 @@ module.exports = exports = class EventEmitter {
|
|
|
150
187
|
listeners(name) {
|
|
151
188
|
if (this._events === undefined) return []
|
|
152
189
|
const e = this._events[name]
|
|
153
|
-
return e === undefined ? [] :
|
|
190
|
+
return e === undefined ? [] : e.list.map((l) => l.fn)
|
|
154
191
|
}
|
|
155
192
|
|
|
156
193
|
rawListeners(name) {
|
|
157
194
|
if (this._events === undefined) return []
|
|
158
195
|
const e = this._events[name]
|
|
159
|
-
return e === undefined ? [] : e.list.map((l) => l
|
|
196
|
+
return e === undefined ? [] : e.list.map((l) => l.fn)
|
|
160
197
|
}
|
|
161
198
|
|
|
162
199
|
eventNames() {
|
|
@@ -174,9 +211,13 @@ module.exports = exports = class EventEmitter {
|
|
|
174
211
|
return EventEmitter.defaultMaxListeners
|
|
175
212
|
}
|
|
176
213
|
|
|
177
|
-
setMaxListeners(n) {
|
|
214
|
+
setMaxListeners(n) {
|
|
215
|
+
return this
|
|
216
|
+
}
|
|
178
217
|
|
|
179
218
|
removeAllListeners(name) {
|
|
219
|
+
if (this._events === undefined) return this
|
|
220
|
+
|
|
180
221
|
if (arguments.length === 0) {
|
|
181
222
|
for (const key of Reflect.ownKeys(this._events)) {
|
|
182
223
|
if (key === 'removeListener') continue
|
|
@@ -257,21 +298,17 @@ exports.on = function on(emitter, name, opts = {}) {
|
|
|
257
298
|
}
|
|
258
299
|
|
|
259
300
|
function onerror(err) {
|
|
260
|
-
emitter.off(name, onevent).off('error', onerror)
|
|
261
|
-
|
|
262
301
|
if (promises.length) {
|
|
263
302
|
promises.shift().reject(err)
|
|
264
303
|
} else {
|
|
265
304
|
error = err
|
|
266
305
|
}
|
|
267
306
|
|
|
268
|
-
return
|
|
307
|
+
return onclose()
|
|
269
308
|
}
|
|
270
309
|
|
|
271
310
|
function onabort() {
|
|
272
|
-
signal.
|
|
273
|
-
|
|
274
|
-
onerror(errors.OPERATION_ABORTED(signal.reason))
|
|
311
|
+
return onerror(errors.OPERATION_ABORTED(signal.reason))
|
|
275
312
|
}
|
|
276
313
|
|
|
277
314
|
function onclose() {
|
|
@@ -283,9 +320,11 @@ exports.on = function on(emitter, name, opts = {}) {
|
|
|
283
320
|
|
|
284
321
|
done = true
|
|
285
322
|
|
|
286
|
-
|
|
323
|
+
const result = { done: true }
|
|
324
|
+
|
|
325
|
+
while (promises.length) promises.shift().resolve(result)
|
|
287
326
|
|
|
288
|
-
return Promise.resolve(
|
|
327
|
+
return Promise.resolve(result)
|
|
289
328
|
}
|
|
290
329
|
}
|
|
291
330
|
|
|
@@ -316,19 +355,19 @@ exports.once = function once(emitter, name, opts = {}) {
|
|
|
316
355
|
|
|
317
356
|
if (name !== 'error') emitter.off('error', onerror)
|
|
318
357
|
|
|
358
|
+
if (signal) signal.removeEventListener('abort', onabort)
|
|
359
|
+
|
|
319
360
|
reject(err)
|
|
320
361
|
}
|
|
321
362
|
|
|
322
363
|
function onabort() {
|
|
323
|
-
signal.removeEventListener('abort', onabort)
|
|
324
|
-
|
|
325
364
|
onerror(errors.OPERATION_ABORTED(signal.reason))
|
|
326
365
|
}
|
|
327
366
|
})
|
|
328
367
|
}
|
|
329
368
|
|
|
330
369
|
exports.forward = function forward(from, to, names, opts = {}) {
|
|
331
|
-
if (
|
|
370
|
+
if (Array.isArray(names) === false) names = [names]
|
|
332
371
|
|
|
333
372
|
const { emit = to.emit.bind(to) } = opts
|
|
334
373
|
|
|
@@ -339,6 +378,10 @@ exports.forward = function forward(from, to, names, opts = {}) {
|
|
|
339
378
|
}
|
|
340
379
|
)
|
|
341
380
|
|
|
381
|
+
for (let i = 0, n = names.length; i < n; i++) {
|
|
382
|
+
if (to.listenerCount(names[i]) > 0) from.on(names[i], listeners[i])
|
|
383
|
+
}
|
|
384
|
+
|
|
342
385
|
to.on('newListener', (name) => {
|
|
343
386
|
const i = names.indexOf(name)
|
|
344
387
|
|
package/lib/errors.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
module.exports = class EventEmitterError extends Error {
|
|
2
|
-
constructor(msg,
|
|
2
|
+
constructor(msg, fn = EventEmitterError, opts = {}) {
|
|
3
|
+
const { code = fn.name } = opts
|
|
4
|
+
|
|
3
5
|
super(`${code}: ${msg}`, opts)
|
|
6
|
+
|
|
4
7
|
this.code = code
|
|
5
8
|
|
|
6
|
-
if (Error.captureStackTrace)
|
|
7
|
-
Error.captureStackTrace(this, fn)
|
|
8
|
-
}
|
|
9
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, fn)
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
get name() {
|
|
@@ -13,14 +14,10 @@ module.exports = class EventEmitterError extends Error {
|
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
static OPERATION_ABORTED(cause, msg = 'Operation aborted') {
|
|
16
|
-
return new EventEmitterError(msg,
|
|
17
|
-
cause
|
|
18
|
-
})
|
|
17
|
+
return new EventEmitterError(msg, EventEmitterError.OPERATION_ABORTED, { cause })
|
|
19
18
|
}
|
|
20
19
|
|
|
21
20
|
static UNHANDLED_ERROR(cause, msg = 'Unhandled error') {
|
|
22
|
-
return new EventEmitterError(msg,
|
|
23
|
-
cause
|
|
24
|
-
})
|
|
21
|
+
return new EventEmitterError(msg, EventEmitterError.UNHANDLED_ERROR, { cause })
|
|
25
22
|
}
|
|
26
23
|
}
|
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
|
}
|
package/web.js
CHANGED
|
@@ -5,6 +5,7 @@ const COMPOSED = 0x4
|
|
|
5
5
|
const CANCELED = 0x8
|
|
6
6
|
const DISPATCH = 0x10
|
|
7
7
|
const STOP = 0x20
|
|
8
|
+
const IN_PASSIVE = 0x40
|
|
8
9
|
|
|
9
10
|
// EventTarget state
|
|
10
11
|
const CAPTURE = 0x1
|
|
@@ -12,6 +13,13 @@ const PASSIVE = 0x2
|
|
|
12
13
|
const ONCE = 0x4
|
|
13
14
|
const REMOVED = 0x8
|
|
14
15
|
|
|
16
|
+
// https://webidl.spec.whatwg.org/#invalidstateerror
|
|
17
|
+
class InvalidStateError extends Error {
|
|
18
|
+
get name() {
|
|
19
|
+
return 'InvalidStateError'
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
15
23
|
// https://dom.spec.whatwg.org/#event
|
|
16
24
|
class Event {
|
|
17
25
|
// https://dom.spec.whatwg.org/#dom-event-event
|
|
@@ -20,6 +28,7 @@ class Event {
|
|
|
20
28
|
|
|
21
29
|
this._type = type
|
|
22
30
|
this._target = null
|
|
31
|
+
this._currentTarget = null
|
|
23
32
|
this._state = 0
|
|
24
33
|
|
|
25
34
|
if (bubbles) this._state |= BUBBLES
|
|
@@ -39,7 +48,7 @@ class Event {
|
|
|
39
48
|
|
|
40
49
|
// https://dom.spec.whatwg.org/#dom-event-currenttarget
|
|
41
50
|
get currentTarget() {
|
|
42
|
-
return
|
|
51
|
+
return this._currentTarget
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
// https://dom.spec.whatwg.org/#dom-event-bubbles
|
|
@@ -69,6 +78,7 @@ class Event {
|
|
|
69
78
|
|
|
70
79
|
// https://dom.spec.whatwg.org/#dom-event-preventdefault
|
|
71
80
|
preventDefault() {
|
|
81
|
+
if (this._state & IN_PASSIVE) return
|
|
72
82
|
if (this._state & CANCELABLE) this._state |= CANCELED
|
|
73
83
|
}
|
|
74
84
|
|
|
@@ -123,6 +133,34 @@ exports.CustomEvent = class CustomEvent extends Event {
|
|
|
123
133
|
get detail() {
|
|
124
134
|
return this._detail
|
|
125
135
|
}
|
|
136
|
+
|
|
137
|
+
toJSON() {
|
|
138
|
+
return {
|
|
139
|
+
type: this.type,
|
|
140
|
+
target: this.target,
|
|
141
|
+
bubbles: this.bubbles,
|
|
142
|
+
cancelable: this.cancelable,
|
|
143
|
+
composed: this.composed,
|
|
144
|
+
defaultPrevented: this.defaultPrevented,
|
|
145
|
+
isTrusted: this.isTrusted,
|
|
146
|
+
detail: this.detail
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
[Symbol.for('bare.inspect')]() {
|
|
151
|
+
return {
|
|
152
|
+
__proto__: { constructor: CustomEvent },
|
|
153
|
+
|
|
154
|
+
type: this.type,
|
|
155
|
+
target: this.target,
|
|
156
|
+
bubbles: this.bubbles,
|
|
157
|
+
cancelable: this.cancelable,
|
|
158
|
+
composed: this.composed,
|
|
159
|
+
defaultPrevented: this.defaultPrevented,
|
|
160
|
+
isTrusted: this.isTrusted,
|
|
161
|
+
detail: this.detail
|
|
162
|
+
}
|
|
163
|
+
}
|
|
126
164
|
}
|
|
127
165
|
|
|
128
166
|
// https://dom.spec.whatwg.org/#eventtarget
|
|
@@ -159,6 +197,9 @@ exports.EventTarget = class EventTarget {
|
|
|
159
197
|
|
|
160
198
|
if (signal !== null) {
|
|
161
199
|
const self = this
|
|
200
|
+
|
|
201
|
+
listener._abort = onabort
|
|
202
|
+
|
|
162
203
|
signal.addEventListener('abort', onabort)
|
|
163
204
|
|
|
164
205
|
function onabort() {
|
|
@@ -187,7 +228,12 @@ exports.EventTarget = class EventTarget {
|
|
|
187
228
|
|
|
188
229
|
// https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
|
|
189
230
|
dispatchEvent(event) {
|
|
231
|
+
if (event._state & DISPATCH) {
|
|
232
|
+
throw new InvalidStateError('Event is already being dispatched')
|
|
233
|
+
}
|
|
234
|
+
|
|
190
235
|
event._target = this
|
|
236
|
+
event._currentTarget = this
|
|
191
237
|
event._state |= DISPATCH
|
|
192
238
|
|
|
193
239
|
const listeners = this._listeners.get(event.type)
|
|
@@ -212,13 +258,25 @@ exports.EventTarget = class EventTarget {
|
|
|
212
258
|
callback = callback.handleEvent
|
|
213
259
|
}
|
|
214
260
|
|
|
215
|
-
|
|
261
|
+
if (listener.passive) event._state |= IN_PASSIVE
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
Reflect.apply(callback, context, [event])
|
|
265
|
+
} catch (err) {
|
|
266
|
+
// https://html.spec.whatwg.org/#report-the-exception
|
|
267
|
+
queueMicrotask(() => {
|
|
268
|
+
throw err
|
|
269
|
+
})
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
event._state &= ~IN_PASSIVE
|
|
216
273
|
|
|
217
274
|
if (event._state & STOP) break
|
|
218
275
|
}
|
|
219
276
|
|
|
220
277
|
return (event._state & CANCELED) === 0
|
|
221
278
|
} finally {
|
|
279
|
+
event._currentTarget = null
|
|
222
280
|
event._state &= ~DISPATCH
|
|
223
281
|
event._state &= ~STOP
|
|
224
282
|
}
|
|
@@ -237,6 +295,11 @@ exports.EventTarget = class EventTarget {
|
|
|
237
295
|
_unlink(type, listener) {
|
|
238
296
|
if (listener.removed) return
|
|
239
297
|
|
|
298
|
+
if (listener._abort !== null) {
|
|
299
|
+
listener._signal.removeEventListener('abort', listener._abort)
|
|
300
|
+
listener._abort = null
|
|
301
|
+
}
|
|
302
|
+
|
|
240
303
|
const head = this._listeners.get(type)
|
|
241
304
|
const next = listener.unlink()
|
|
242
305
|
|
|
@@ -253,6 +316,7 @@ class EventListener {
|
|
|
253
316
|
this._type = type
|
|
254
317
|
this._callback = callback
|
|
255
318
|
this._signal = signal
|
|
319
|
+
this._abort = null
|
|
256
320
|
this._state = 0
|
|
257
321
|
|
|
258
322
|
if (capture) this._state |= CAPTURE
|