bare-events 1.2.6 → 2.0.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/index.js +53 -55
- package/lib/errors.js +18 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,115 +1,113 @@
|
|
|
1
|
+
const errors = require('./lib/errors')
|
|
2
|
+
|
|
1
3
|
class EventListener {
|
|
2
4
|
constructor () {
|
|
3
5
|
this.list = []
|
|
4
|
-
this.emitting = false
|
|
5
|
-
this.removing = null
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
append (fn, once) {
|
|
8
|
+
append (ctx, name, fn, once) {
|
|
9
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
9
10
|
this.list.push([fn, once])
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
prepend (fn, once) {
|
|
13
|
+
prepend (ctx, name, fn, once) {
|
|
14
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
13
15
|
this.list.unshift([fn, once])
|
|
14
16
|
}
|
|
15
17
|
|
|
16
|
-
remove (fn) {
|
|
17
|
-
|
|
18
|
-
if (this.removing === null) this.removing = []
|
|
19
|
-
this.removing.push(fn)
|
|
20
|
-
return
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
for (let i = 0; i < this.list.length; i++) {
|
|
18
|
+
remove (ctx, name, fn) {
|
|
19
|
+
for (let i = 0, n = this.list.length; i < n; i++) {
|
|
24
20
|
const l = this.list[i]
|
|
25
21
|
|
|
26
22
|
if (l[0] === fn) {
|
|
27
23
|
this.list.splice(i, 1)
|
|
24
|
+
|
|
25
|
+
if (this.list.length === 0) delete ctx._events[name]
|
|
26
|
+
|
|
27
|
+
ctx.emit('removeListener', name, fn) // Emit AFTER removing
|
|
28
28
|
return
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
emit (ctx, ...args) {
|
|
34
|
-
|
|
35
|
-
const listeners = this.list.length > 0
|
|
33
|
+
emit (ctx, name, ...args) {
|
|
34
|
+
const list = [...this.list]
|
|
36
35
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const l = this.list[i]
|
|
36
|
+
for (let i = 0, n = list.length; i < n; i++) {
|
|
37
|
+
const l = list[i]
|
|
40
38
|
|
|
41
|
-
|
|
42
|
-
if (l[1] === true) this.list.splice(i--, 1)
|
|
43
|
-
}
|
|
44
|
-
} finally {
|
|
45
|
-
this.emitting = false
|
|
39
|
+
if (l[1] === true) this.remove(ctx, name, l[0])
|
|
46
40
|
|
|
47
|
-
|
|
48
|
-
const fns = this.removing
|
|
49
|
-
this.removing = null
|
|
50
|
-
for (const fn of fns) this.remove(fn)
|
|
51
|
-
}
|
|
41
|
+
l[0].call(ctx, ...args)
|
|
52
42
|
}
|
|
53
43
|
|
|
54
|
-
return
|
|
44
|
+
return list.length > 0
|
|
55
45
|
}
|
|
56
46
|
}
|
|
57
47
|
|
|
48
|
+
function appendListener (ctx, name, fn, once) {
|
|
49
|
+
const e = ctx._events[name] || (ctx._events[name] = new EventListener())
|
|
50
|
+
e.append(ctx, name, fn, once)
|
|
51
|
+
return ctx
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function prependListener (ctx, name, fn, once) {
|
|
55
|
+
const e = ctx._events[name] || (ctx._events[name] = new EventListener())
|
|
56
|
+
e.prepend(ctx, name, fn, once)
|
|
57
|
+
return ctx
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function removeListener (ctx, name, fn) {
|
|
61
|
+
const e = ctx._events[name]
|
|
62
|
+
if (e !== undefined) e.remove(ctx, name, fn)
|
|
63
|
+
return ctx
|
|
64
|
+
}
|
|
65
|
+
|
|
58
66
|
module.exports = exports = class EventEmitter {
|
|
59
67
|
constructor () {
|
|
60
68
|
this._events = Object.create(null)
|
|
61
69
|
}
|
|
62
70
|
|
|
63
71
|
addListener (name, fn) {
|
|
64
|
-
this
|
|
65
|
-
const e = this._events[name] || (this._events[name] = new EventListener())
|
|
66
|
-
e.append(fn, false)
|
|
67
|
-
return this
|
|
72
|
+
return appendListener(this, name, fn, false)
|
|
68
73
|
}
|
|
69
74
|
|
|
70
75
|
addOnceListener (name, fn) {
|
|
71
|
-
this
|
|
72
|
-
const e = this._events[name] || (this._events[name] = new EventListener())
|
|
73
|
-
e.append(fn, true)
|
|
74
|
-
return this
|
|
76
|
+
return appendListener(this, name, fn, true)
|
|
75
77
|
}
|
|
76
78
|
|
|
77
79
|
prependListener (name, fn) {
|
|
78
|
-
this
|
|
79
|
-
const e = this._events[name] || (this._events[name] = new EventListener())
|
|
80
|
-
e.prepend(fn, false)
|
|
81
|
-
return this
|
|
80
|
+
return prependListener(this, name, fn, false)
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
prependOnceListener (name, fn) {
|
|
85
|
-
this
|
|
86
|
-
const e = this._events[name] || (this._events[name] = new EventListener())
|
|
87
|
-
e.prepend(fn, true)
|
|
88
|
-
return this
|
|
84
|
+
return prependListener(this, name, fn, true)
|
|
89
85
|
}
|
|
90
86
|
|
|
91
87
|
removeListener (name, fn) {
|
|
92
|
-
|
|
93
|
-
if (e !== undefined) e.remove(fn)
|
|
94
|
-
this.emit('removeListener', name, fn)
|
|
95
|
-
return this
|
|
88
|
+
return removeListener(this, name, fn)
|
|
96
89
|
}
|
|
97
90
|
|
|
98
91
|
on (name, fn) {
|
|
99
|
-
return this
|
|
92
|
+
return appendListener(this, name, fn, false)
|
|
100
93
|
}
|
|
101
94
|
|
|
102
95
|
once (name, fn) {
|
|
103
|
-
return this
|
|
96
|
+
return appendListener(this, name, fn, true)
|
|
104
97
|
}
|
|
105
98
|
|
|
106
99
|
off (name, fn) {
|
|
107
|
-
return
|
|
100
|
+
return removeListener(this, name, fn)
|
|
108
101
|
}
|
|
109
102
|
|
|
110
103
|
emit (name, ...args) {
|
|
111
104
|
const e = this._events[name]
|
|
112
|
-
return e === undefined ? false : e.emit(this, ...args)
|
|
105
|
+
return e === undefined ? false : e.emit(this, name, ...args)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
listeners (name) {
|
|
109
|
+
const e = this._events[name]
|
|
110
|
+
return e === undefined ? [] : [...e.list]
|
|
113
111
|
}
|
|
114
112
|
|
|
115
113
|
listenerCount (name) {
|
|
@@ -135,7 +133,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
135
133
|
|
|
136
134
|
return new Promise((resolve, reject) => {
|
|
137
135
|
if (signal) {
|
|
138
|
-
if (signal.aborted) return
|
|
136
|
+
if (signal.aborted) return abort()
|
|
139
137
|
|
|
140
138
|
signal.addEventListener('abort', abort)
|
|
141
139
|
}
|
|
@@ -147,7 +145,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
147
145
|
})
|
|
148
146
|
|
|
149
147
|
function abort () {
|
|
150
|
-
reject(signal.reason)
|
|
148
|
+
reject(errors.OPERATION_ABORTED(signal.reason))
|
|
151
149
|
}
|
|
152
150
|
})
|
|
153
151
|
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = class EventEmitterError extends Error {
|
|
2
|
+
constructor (msg, code, fn = EventEmitterError, opts) {
|
|
3
|
+
super(`${code}: ${msg}`, opts)
|
|
4
|
+
this.code = code
|
|
5
|
+
|
|
6
|
+
if (Error.captureStackTrace) {
|
|
7
|
+
Error.captureStackTrace(this, fn)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get name () {
|
|
12
|
+
return 'EventEmitterError'
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static OPERATION_ABORTED (cause, msg = 'Operation aborted') {
|
|
16
|
+
return new EventEmitterError(msg, 'OPERATION_ABORTED ', EventEmitterError.OPERATION_ABORTED, { cause })
|
|
17
|
+
}
|
|
18
|
+
}
|
package/package.json
CHANGED