bare-events 2.6.0 → 2.7.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/global.d.ts +12 -0
- package/global.js +4 -0
- package/index.js +9 -3
- package/package.json +14 -2
- package/web.d.ts +67 -0
- package/web.js +329 -0
package/global.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as events from './web'
|
|
2
|
+
|
|
3
|
+
type EventConstructor = typeof events.Event
|
|
4
|
+
type EventTargetConstructor = typeof events.EventTarget
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
type Event = events.Event
|
|
8
|
+
type EventTarget = events.EventTarget
|
|
9
|
+
|
|
10
|
+
const Event: EventConstructor
|
|
11
|
+
const EventTarget: EventTargetConstructor
|
|
12
|
+
}
|
package/global.js
ADDED
package/index.js
CHANGED
|
@@ -56,7 +56,7 @@ class EventListener {
|
|
|
56
56
|
|
|
57
57
|
if (l[1] === true) this.remove(ctx, name, l[0])
|
|
58
58
|
|
|
59
|
-
l[0]
|
|
59
|
+
Reflect.apply(l[0], ctx, args)
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
return list.length > 0
|
|
@@ -336,14 +336,20 @@ exports.listenerCount = function listenerCount(emitter, name) {
|
|
|
336
336
|
}
|
|
337
337
|
|
|
338
338
|
exports.getMaxListeners = function getMaxListeners(emitter) {
|
|
339
|
-
|
|
339
|
+
if (typeof emitter.getMaxListeners === 'function') {
|
|
340
|
+
return emitter.getMaxListeners()
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
return exports.defaultMaxListeners
|
|
340
344
|
}
|
|
341
345
|
|
|
342
346
|
exports.setMaxListeners = function setMaxListeners(n, ...emitters) {
|
|
343
347
|
if (emitters.length === 0) exports.defaultMaxListeners = n
|
|
344
348
|
else {
|
|
345
349
|
for (const emitter of emitters) {
|
|
346
|
-
emitter.setMaxListeners
|
|
350
|
+
if (typeof emitter.setMaxListeners === 'function') {
|
|
351
|
+
emitter.setMaxListeners(n)
|
|
352
|
+
}
|
|
347
353
|
}
|
|
348
354
|
}
|
|
349
355
|
}
|
package/package.json
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-events",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Event emitters for JavaScript",
|
|
5
5
|
"exports": {
|
|
6
|
+
"./package": "./package.json",
|
|
6
7
|
".": {
|
|
7
8
|
"types": "./index.d.ts",
|
|
8
9
|
"default": "./index.js"
|
|
9
10
|
},
|
|
10
|
-
"./
|
|
11
|
+
"./global": {
|
|
12
|
+
"types": "./global.d.ts",
|
|
13
|
+
"default": "./global.js"
|
|
14
|
+
},
|
|
15
|
+
"./web": {
|
|
16
|
+
"types": "./web.d.ts",
|
|
17
|
+
"default": "./web.js"
|
|
18
|
+
},
|
|
11
19
|
"./errors": "./lib/errors.js"
|
|
12
20
|
},
|
|
13
21
|
"files": [
|
|
14
22
|
"index.js",
|
|
15
23
|
"index.d.ts",
|
|
24
|
+
"global.js",
|
|
25
|
+
"global.d.ts",
|
|
26
|
+
"web.js",
|
|
27
|
+
"web.d.ts",
|
|
16
28
|
"lib"
|
|
17
29
|
],
|
|
18
30
|
"scripts": {
|
package/web.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
interface AbortSignal extends EventTarget {}
|
|
2
|
+
|
|
3
|
+
export interface EventOptions {
|
|
4
|
+
bubbles?: boolean
|
|
5
|
+
cancelable?: boolean
|
|
6
|
+
composed?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Event {
|
|
10
|
+
readonly type: string
|
|
11
|
+
readonly target: EventTarget | null
|
|
12
|
+
readonly currentTarget: EventTarget | null
|
|
13
|
+
readonly bubbles: boolean
|
|
14
|
+
readonly cancelable: boolean
|
|
15
|
+
readonly composed: boolean
|
|
16
|
+
readonly defaultPrevented: boolean
|
|
17
|
+
readonly isTrusted: boolean
|
|
18
|
+
|
|
19
|
+
preventDefault(): void
|
|
20
|
+
stopPropagation(): void
|
|
21
|
+
stopImmediatePropagation(): void
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class Event {
|
|
25
|
+
constructor(type: string, options?: EventOptions)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface AddEventListenerOptions {
|
|
29
|
+
capture?: boolean
|
|
30
|
+
passive?: boolean
|
|
31
|
+
once?: boolean
|
|
32
|
+
signal?: AbortSignal | null
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface RemoveEventListenerOptions {
|
|
36
|
+
capture?: boolean
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface EventTarget {
|
|
40
|
+
addEventListener(
|
|
41
|
+
type: string,
|
|
42
|
+
callback: EventListener,
|
|
43
|
+
options?: AddEventListenerOptions | boolean
|
|
44
|
+
): void
|
|
45
|
+
|
|
46
|
+
removeEventListener(
|
|
47
|
+
type: string,
|
|
48
|
+
callback: EventListener,
|
|
49
|
+
options?: RemoveEventListenerOptions | boolean
|
|
50
|
+
): void
|
|
51
|
+
|
|
52
|
+
dispatchEvent(event: Event): boolean
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export class EventTarget {
|
|
56
|
+
constructor()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type EventListener = EventCallback | EventHandler
|
|
60
|
+
|
|
61
|
+
export interface EventCallback {
|
|
62
|
+
(event: Event): void
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export interface EventHandler {
|
|
66
|
+
handleEvent(event: Event): void
|
|
67
|
+
}
|
package/web.js
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
// Event state
|
|
2
|
+
const BUBBLES = 0x1
|
|
3
|
+
const CANCELABLE = 0x2
|
|
4
|
+
const COMPOSED = 0x4
|
|
5
|
+
const CANCELED = 0x8
|
|
6
|
+
const DISPATCH = 0x10
|
|
7
|
+
const STOP = 0x20
|
|
8
|
+
|
|
9
|
+
// EventTarget state
|
|
10
|
+
const CAPTURE = 0x1
|
|
11
|
+
const PASSIVE = 0x2
|
|
12
|
+
const ONCE = 0x4
|
|
13
|
+
|
|
14
|
+
// https://dom.spec.whatwg.org/#event
|
|
15
|
+
exports.Event = class Event {
|
|
16
|
+
// https://dom.spec.whatwg.org/#dom-event-event
|
|
17
|
+
constructor(type, options = {}) {
|
|
18
|
+
const { bubbles = false, cancelable = false, composed = false } = options
|
|
19
|
+
|
|
20
|
+
this._type = type
|
|
21
|
+
this._target = null
|
|
22
|
+
this._state = 0
|
|
23
|
+
|
|
24
|
+
if (bubbles) this._state |= BUBBLES
|
|
25
|
+
if (cancelable) this._state |= CANCELABLE
|
|
26
|
+
if (composed) this._state |= COMPOSED
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// https://dom.spec.whatwg.org/#dom-event-type
|
|
30
|
+
get type() {
|
|
31
|
+
return this._type
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// https://dom.spec.whatwg.org/#dom-event-target
|
|
35
|
+
get target() {
|
|
36
|
+
return this._target
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// https://dom.spec.whatwg.org/#dom-event-currenttarget
|
|
40
|
+
get currentTarget() {
|
|
41
|
+
return null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// https://dom.spec.whatwg.org/#dom-event-bubbles
|
|
45
|
+
get bubbles() {
|
|
46
|
+
return (this._state & BUBBLES) !== 0
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// https://dom.spec.whatwg.org/#dom-event-cancelable
|
|
50
|
+
get cancelable() {
|
|
51
|
+
return (this._state & CANCELABLE) !== 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// https://dom.spec.whatwg.org/#dom-event-composed
|
|
55
|
+
get composed() {
|
|
56
|
+
return (this._state & COMPOSED) !== 0
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// https://dom.spec.whatwg.org/#dom-event-defaultprevented
|
|
60
|
+
get defaultPrevented() {
|
|
61
|
+
return (this._state & CANCELED) !== 0
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// https://dom.spec.whatwg.org/#dom-event-istrusted
|
|
65
|
+
get isTrusted() {
|
|
66
|
+
return false
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// https://dom.spec.whatwg.org/#dom-event-preventdefault
|
|
70
|
+
preventDefault() {
|
|
71
|
+
if (this._state & CANCELABLE) this._state |= CANCELED
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// https://dom.spec.whatwg.org/#dom-event-stoppropagation
|
|
75
|
+
stopPropagation() {}
|
|
76
|
+
|
|
77
|
+
// https://dom.spec.whatwg.org/#dom-event-stopimmediatepropagation
|
|
78
|
+
stopImmediatePropagation() {
|
|
79
|
+
this._state |= STOP
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
toJSON() {
|
|
83
|
+
return {
|
|
84
|
+
type: this.type,
|
|
85
|
+
target: this.target,
|
|
86
|
+
bubbles: this.bubbles,
|
|
87
|
+
cancelable: this.cancelable,
|
|
88
|
+
composed: this.composed,
|
|
89
|
+
defaultPrevented: this.defaultPrevented,
|
|
90
|
+
isTrusted: this.isTrusted
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
[Symbol.for('bare.inspect')]() {
|
|
95
|
+
return {
|
|
96
|
+
__proto__: { constructor: Event },
|
|
97
|
+
|
|
98
|
+
type: this.type,
|
|
99
|
+
target: this.target,
|
|
100
|
+
bubbles: this.bubbles,
|
|
101
|
+
cancelable: this.cancelable,
|
|
102
|
+
composed: this.composed,
|
|
103
|
+
defaultPrevented: this.defaultPrevented,
|
|
104
|
+
isTrusted: this.isTrusted
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// https://dom.spec.whatwg.org/#eventtarget
|
|
110
|
+
exports.EventTarget = class EventTarget {
|
|
111
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-eventtarget
|
|
112
|
+
constructor() {
|
|
113
|
+
this._listeners = new Map()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
|
|
117
|
+
addEventListener(type, callback = null, options = {}) {
|
|
118
|
+
if (typeof options === 'boolean') options = { capture: options }
|
|
119
|
+
|
|
120
|
+
const {
|
|
121
|
+
capture = false,
|
|
122
|
+
passive = false,
|
|
123
|
+
once = false,
|
|
124
|
+
signal = null
|
|
125
|
+
} = options
|
|
126
|
+
|
|
127
|
+
if (signal !== null && signal.aborted) return
|
|
128
|
+
if (callback === null) return
|
|
129
|
+
|
|
130
|
+
const listener = new EventListener(
|
|
131
|
+
type,
|
|
132
|
+
callback,
|
|
133
|
+
capture,
|
|
134
|
+
passive,
|
|
135
|
+
once,
|
|
136
|
+
signal
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
const listeners = this._listeners.get(type)
|
|
140
|
+
|
|
141
|
+
if (listeners === undefined) this._listeners.set(type, listener)
|
|
142
|
+
else {
|
|
143
|
+
for (const existing of listeners) {
|
|
144
|
+
if (callback === existing.callback && capture === existing.capture) {
|
|
145
|
+
return // Duplicate listener
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
listener.link(listeners)
|
|
150
|
+
|
|
151
|
+
if (signal !== null) {
|
|
152
|
+
signal.addEventListener('abort', onabort)
|
|
153
|
+
|
|
154
|
+
function onabort() {
|
|
155
|
+
listener.unlink()
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
|
|
162
|
+
removeEventListener(type, callback = null, options = {}) {
|
|
163
|
+
if (typeof options === 'boolean') options = { capture: options }
|
|
164
|
+
|
|
165
|
+
const { capture = false } = options
|
|
166
|
+
|
|
167
|
+
const listeners = this._listeners.get(type)
|
|
168
|
+
|
|
169
|
+
if (listeners === undefined) return
|
|
170
|
+
|
|
171
|
+
for (const existing of listeners) {
|
|
172
|
+
if (callback === existing.callback && capture === existing.capture) {
|
|
173
|
+
const next = existing.unlink()
|
|
174
|
+
|
|
175
|
+
if (listeners === existing) this._listeners.set(type, next)
|
|
176
|
+
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
|
|
183
|
+
dispatchEvent(event) {
|
|
184
|
+
event._target = this
|
|
185
|
+
event._state |= DISPATCH
|
|
186
|
+
|
|
187
|
+
const listeners = this._listeners.get(event.type)
|
|
188
|
+
|
|
189
|
+
try {
|
|
190
|
+
if (listeners === undefined) return true
|
|
191
|
+
|
|
192
|
+
for (const listener of listeners) {
|
|
193
|
+
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
|
|
194
|
+
|
|
195
|
+
if (listener.once) listener.unlink()
|
|
196
|
+
|
|
197
|
+
let callback = listener.callback
|
|
198
|
+
let context = this
|
|
199
|
+
|
|
200
|
+
if (typeof callback === 'object') {
|
|
201
|
+
context = callback
|
|
202
|
+
callback = callback.handleEvent
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
Reflect.apply(callback, context, [event])
|
|
206
|
+
|
|
207
|
+
if (event._state & STOP) break
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return (event._state & CANCELED) === 0
|
|
211
|
+
} finally {
|
|
212
|
+
event._state &= ~DISPATCH
|
|
213
|
+
event._state &= ~STOP
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
toJSON() {
|
|
218
|
+
return {}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
[Symbol.for('bare.inspect')]() {
|
|
222
|
+
return {
|
|
223
|
+
__proto__: { constructor: EventTarget }
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// https://dom.spec.whatwg.org/#concept-event-listener
|
|
229
|
+
class EventListener {
|
|
230
|
+
constructor(type, callback, capture, passive, once, signal) {
|
|
231
|
+
this._type = type
|
|
232
|
+
this._callback = callback
|
|
233
|
+
this._signal = signal
|
|
234
|
+
this._state = 0
|
|
235
|
+
|
|
236
|
+
if (capture) this._state |= CAPTURE
|
|
237
|
+
if (passive) this._state |= PASSIVE
|
|
238
|
+
if (once) this._state |= ONCE
|
|
239
|
+
|
|
240
|
+
this._previous = this
|
|
241
|
+
this._next = this
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
get type() {
|
|
245
|
+
return this._type
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
get callback() {
|
|
249
|
+
return this._callback
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
get capture() {
|
|
253
|
+
return (this._state & CAPTURE) !== 0
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
get passive() {
|
|
257
|
+
return (this._state & PASSIVE) !== 0
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
get once() {
|
|
261
|
+
return (this._state & ONCE) !== 0
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
get removed() {
|
|
265
|
+
return this._previous === this && this._next === this
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
link(listener) {
|
|
269
|
+
const next = this._next
|
|
270
|
+
const previous = listener._previous
|
|
271
|
+
|
|
272
|
+
this._next = listener
|
|
273
|
+
listener._previous = this
|
|
274
|
+
|
|
275
|
+
previous._next = next
|
|
276
|
+
next._previous = previous
|
|
277
|
+
|
|
278
|
+
return listener
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
unlink() {
|
|
282
|
+
if (this.removed) return this
|
|
283
|
+
|
|
284
|
+
const next = this._next
|
|
285
|
+
const previous = this._previous
|
|
286
|
+
|
|
287
|
+
this._next = this
|
|
288
|
+
this._previous = this
|
|
289
|
+
|
|
290
|
+
previous._next = next
|
|
291
|
+
next._previous = previous
|
|
292
|
+
|
|
293
|
+
return next
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
*[Symbol.iterator]() {
|
|
297
|
+
let current = this
|
|
298
|
+
|
|
299
|
+
while (true) {
|
|
300
|
+
const next = current._next
|
|
301
|
+
yield current
|
|
302
|
+
if (next === this) break
|
|
303
|
+
current = next
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
toJSON() {
|
|
308
|
+
return {
|
|
309
|
+
type: this.type,
|
|
310
|
+
capture: this.capture,
|
|
311
|
+
passive: this.passive,
|
|
312
|
+
once: this.once,
|
|
313
|
+
removed: this.removed
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
[Symbol.for('bare.inspect')]() {
|
|
318
|
+
return {
|
|
319
|
+
__proto__: { constructor: EventListener },
|
|
320
|
+
|
|
321
|
+
type: this.type,
|
|
322
|
+
callback: this.callback,
|
|
323
|
+
capture: this.capture,
|
|
324
|
+
passive: this.passive,
|
|
325
|
+
once: this.once,
|
|
326
|
+
removed: this.removed
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|