bare-events 2.6.1 → 2.8.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 +15 -0
- package/global.js +5 -0
- package/index.d.ts +2 -0
- package/index.js +1 -1
- package/package.json +25 -3
- package/web.d.ts +79 -0
- package/web.js +347 -0
package/global.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as events from './web'
|
|
2
|
+
|
|
3
|
+
type EventConstructor = typeof events.Event
|
|
4
|
+
type CustomEventConstructor = typeof events.CustomEvent
|
|
5
|
+
type EventTargetConstructor = typeof events.EventTarget
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
type Event = events.Event
|
|
9
|
+
type CustomEvent<T = any> = events.CustomEvent<T>
|
|
10
|
+
type EventTarget = events.EventTarget
|
|
11
|
+
|
|
12
|
+
const Event: EventConstructor
|
|
13
|
+
const CustomEvent: CustomEventConstructor
|
|
14
|
+
const EventTarget: EventTargetConstructor
|
|
15
|
+
}
|
package/global.js
ADDED
package/index.d.ts
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-events",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.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": {
|
|
@@ -32,8 +44,18 @@
|
|
|
32
44
|
},
|
|
33
45
|
"homepage": "https://github.com/holepunchto/bare-events#readme",
|
|
34
46
|
"devDependencies": {
|
|
47
|
+
"bare-abort-controller": "^1.0.0",
|
|
35
48
|
"brittle": "^3.3.2",
|
|
36
49
|
"prettier": "^3.4.2",
|
|
37
|
-
"prettier-config-standard": "^7.0.0"
|
|
50
|
+
"prettier-config-standard": "^7.0.0",
|
|
51
|
+
"uncaughts": "^1.1.1"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"bare-abort-controller": "*"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"bare-abort-controller": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
38
60
|
}
|
|
39
61
|
}
|
package/web.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
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 CustomEventOptions<T = any> extends EventOptions {
|
|
29
|
+
detail?: T
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface CustomEvent<T = any> extends Event {
|
|
33
|
+
readonly detail: T
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export class CustomEvent<T = any> {
|
|
37
|
+
constructor(type: string, options?: CustomEventOptions<T>)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface AddEventListenerOptions {
|
|
41
|
+
capture?: boolean
|
|
42
|
+
passive?: boolean
|
|
43
|
+
once?: boolean
|
|
44
|
+
signal?: AbortSignal | null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RemoveEventListenerOptions {
|
|
48
|
+
capture?: boolean
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface EventTarget {
|
|
52
|
+
addEventListener(
|
|
53
|
+
type: string,
|
|
54
|
+
callback: EventListener,
|
|
55
|
+
options?: AddEventListenerOptions | boolean
|
|
56
|
+
): void
|
|
57
|
+
|
|
58
|
+
removeEventListener(
|
|
59
|
+
type: string,
|
|
60
|
+
callback: EventListener,
|
|
61
|
+
options?: RemoveEventListenerOptions | boolean
|
|
62
|
+
): void
|
|
63
|
+
|
|
64
|
+
dispatchEvent(event: Event): boolean
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class EventTarget {
|
|
68
|
+
constructor()
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type EventListener = EventCallback | EventHandler
|
|
72
|
+
|
|
73
|
+
export interface EventCallback {
|
|
74
|
+
(event: Event): void
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface EventHandler {
|
|
78
|
+
handleEvent(event: Event): void
|
|
79
|
+
}
|
package/web.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
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
|
+
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
|
+
exports.Event = Event
|
|
110
|
+
|
|
111
|
+
// https://dom.spec.whatwg.org/#customevent
|
|
112
|
+
exports.CustomEvent = class CustomEvent extends Event {
|
|
113
|
+
constructor(type, options = {}) {
|
|
114
|
+
super(type, options)
|
|
115
|
+
|
|
116
|
+
const { detail = null } = options
|
|
117
|
+
|
|
118
|
+
this._detail = detail
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// https://dom.spec.whatwg.org/#dom-customevent-detail
|
|
122
|
+
get detail() {
|
|
123
|
+
return this._detail
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// https://dom.spec.whatwg.org/#eventtarget
|
|
128
|
+
exports.EventTarget = class EventTarget {
|
|
129
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-eventtarget
|
|
130
|
+
constructor() {
|
|
131
|
+
this._listeners = new Map()
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-addeventlistener
|
|
135
|
+
addEventListener(type, callback = null, options = {}) {
|
|
136
|
+
if (typeof options === 'boolean') options = { capture: options }
|
|
137
|
+
|
|
138
|
+
const {
|
|
139
|
+
capture = false,
|
|
140
|
+
passive = false,
|
|
141
|
+
once = false,
|
|
142
|
+
signal = null
|
|
143
|
+
} = options
|
|
144
|
+
|
|
145
|
+
if (signal !== null && signal.aborted) return
|
|
146
|
+
if (callback === null) return
|
|
147
|
+
|
|
148
|
+
const listener = new EventListener(
|
|
149
|
+
type,
|
|
150
|
+
callback,
|
|
151
|
+
capture,
|
|
152
|
+
passive,
|
|
153
|
+
once,
|
|
154
|
+
signal
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
const listeners = this._listeners.get(type)
|
|
158
|
+
|
|
159
|
+
if (listeners === undefined) this._listeners.set(type, listener)
|
|
160
|
+
else {
|
|
161
|
+
for (const existing of listeners) {
|
|
162
|
+
if (callback === existing.callback && capture === existing.capture) {
|
|
163
|
+
return // Duplicate listener
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
listener.link(listeners)
|
|
168
|
+
|
|
169
|
+
if (signal !== null) {
|
|
170
|
+
signal.addEventListener('abort', onabort)
|
|
171
|
+
|
|
172
|
+
function onabort() {
|
|
173
|
+
listener.unlink()
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-removeeventlistener
|
|
180
|
+
removeEventListener(type, callback = null, options = {}) {
|
|
181
|
+
if (typeof options === 'boolean') options = { capture: options }
|
|
182
|
+
|
|
183
|
+
const { capture = false } = options
|
|
184
|
+
|
|
185
|
+
const listeners = this._listeners.get(type)
|
|
186
|
+
|
|
187
|
+
if (listeners === undefined) return
|
|
188
|
+
|
|
189
|
+
for (const existing of listeners) {
|
|
190
|
+
if (callback === existing.callback && capture === existing.capture) {
|
|
191
|
+
const next = existing.unlink()
|
|
192
|
+
|
|
193
|
+
if (listeners === existing) this._listeners.set(type, next)
|
|
194
|
+
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// https://dom.spec.whatwg.org/#dom-eventtarget-dispatchevent
|
|
201
|
+
dispatchEvent(event) {
|
|
202
|
+
event._target = this
|
|
203
|
+
event._state |= DISPATCH
|
|
204
|
+
|
|
205
|
+
const listeners = this._listeners.get(event.type)
|
|
206
|
+
|
|
207
|
+
try {
|
|
208
|
+
if (listeners === undefined) return true
|
|
209
|
+
|
|
210
|
+
for (const listener of listeners) {
|
|
211
|
+
// https://dom.spec.whatwg.org/#concept-event-listener-inner-invoke
|
|
212
|
+
|
|
213
|
+
if (listener.once) listener.unlink()
|
|
214
|
+
|
|
215
|
+
let callback = listener.callback
|
|
216
|
+
let context = this
|
|
217
|
+
|
|
218
|
+
if (typeof callback === 'object') {
|
|
219
|
+
context = callback
|
|
220
|
+
callback = callback.handleEvent
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
Reflect.apply(callback, context, [event])
|
|
224
|
+
|
|
225
|
+
if (event._state & STOP) break
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return (event._state & CANCELED) === 0
|
|
229
|
+
} finally {
|
|
230
|
+
event._state &= ~DISPATCH
|
|
231
|
+
event._state &= ~STOP
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
toJSON() {
|
|
236
|
+
return {}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
[Symbol.for('bare.inspect')]() {
|
|
240
|
+
return {
|
|
241
|
+
__proto__: { constructor: EventTarget }
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// https://dom.spec.whatwg.org/#concept-event-listener
|
|
247
|
+
class EventListener {
|
|
248
|
+
constructor(type, callback, capture, passive, once, signal) {
|
|
249
|
+
this._type = type
|
|
250
|
+
this._callback = callback
|
|
251
|
+
this._signal = signal
|
|
252
|
+
this._state = 0
|
|
253
|
+
|
|
254
|
+
if (capture) this._state |= CAPTURE
|
|
255
|
+
if (passive) this._state |= PASSIVE
|
|
256
|
+
if (once) this._state |= ONCE
|
|
257
|
+
|
|
258
|
+
this._previous = this
|
|
259
|
+
this._next = this
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
get type() {
|
|
263
|
+
return this._type
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
get callback() {
|
|
267
|
+
return this._callback
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
get capture() {
|
|
271
|
+
return (this._state & CAPTURE) !== 0
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
get passive() {
|
|
275
|
+
return (this._state & PASSIVE) !== 0
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
get once() {
|
|
279
|
+
return (this._state & ONCE) !== 0
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
get removed() {
|
|
283
|
+
return this._previous === this && this._next === this
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
link(listener) {
|
|
287
|
+
const next = this._next
|
|
288
|
+
const previous = listener._previous
|
|
289
|
+
|
|
290
|
+
this._next = listener
|
|
291
|
+
listener._previous = this
|
|
292
|
+
|
|
293
|
+
previous._next = next
|
|
294
|
+
next._previous = previous
|
|
295
|
+
|
|
296
|
+
return listener
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
unlink() {
|
|
300
|
+
if (this.removed) return this
|
|
301
|
+
|
|
302
|
+
const next = this._next
|
|
303
|
+
const previous = this._previous
|
|
304
|
+
|
|
305
|
+
this._next = this
|
|
306
|
+
this._previous = this
|
|
307
|
+
|
|
308
|
+
previous._next = next
|
|
309
|
+
next._previous = previous
|
|
310
|
+
|
|
311
|
+
return next
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
*[Symbol.iterator]() {
|
|
315
|
+
let current = this
|
|
316
|
+
|
|
317
|
+
while (true) {
|
|
318
|
+
const next = current._next
|
|
319
|
+
yield current
|
|
320
|
+
if (next === this) break
|
|
321
|
+
current = next
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
toJSON() {
|
|
326
|
+
return {
|
|
327
|
+
type: this.type,
|
|
328
|
+
capture: this.capture,
|
|
329
|
+
passive: this.passive,
|
|
330
|
+
once: this.once,
|
|
331
|
+
removed: this.removed
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
[Symbol.for('bare.inspect')]() {
|
|
336
|
+
return {
|
|
337
|
+
__proto__: { constructor: EventListener },
|
|
338
|
+
|
|
339
|
+
type: this.type,
|
|
340
|
+
callback: this.callback,
|
|
341
|
+
capture: this.capture,
|
|
342
|
+
passive: this.passive,
|
|
343
|
+
once: this.once,
|
|
344
|
+
removed: this.removed
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|