bare-events 1.3.0 → 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 +59 -84
- package/lib/errors.js +18 -0
- package/package.json +3 -2
package/index.js
CHANGED
|
@@ -1,138 +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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// very likely the last added
|
|
12
|
-
let l = this.list[this.list.length - 1]
|
|
13
|
-
|
|
14
|
-
if (l[0] === fn) {
|
|
15
|
-
l[1] = true
|
|
16
|
-
return
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
// if not then the first added
|
|
20
|
-
l = this.list[0]
|
|
21
|
-
|
|
22
|
-
if (l[0] === fn) {
|
|
23
|
-
l[1] = true
|
|
24
|
-
return
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// just in case thats wrong due to v complex inheritance...
|
|
28
|
-
for (let i = 1; i < this.list.length - 1; i++) {
|
|
29
|
-
l = this.list[i]
|
|
30
|
-
if (l[0] === fn) {
|
|
31
|
-
l[1] = true
|
|
32
|
-
return
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
append (fn) {
|
|
38
|
-
this.list.push([fn, false])
|
|
8
|
+
append (ctx, name, fn, once) {
|
|
9
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
10
|
+
this.list.push([fn, once])
|
|
39
11
|
}
|
|
40
12
|
|
|
41
|
-
prepend (fn) {
|
|
42
|
-
|
|
13
|
+
prepend (ctx, name, fn, once) {
|
|
14
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
15
|
+
this.list.unshift([fn, once])
|
|
43
16
|
}
|
|
44
17
|
|
|
45
|
-
remove (fn) {
|
|
46
|
-
|
|
47
|
-
if (this.removing === null) this.removing = []
|
|
48
|
-
this.removing.push(fn)
|
|
49
|
-
return
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
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++) {
|
|
53
20
|
const l = this.list[i]
|
|
54
21
|
|
|
55
22
|
if (l[0] === fn) {
|
|
56
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
|
|
57
28
|
return
|
|
58
29
|
}
|
|
59
30
|
}
|
|
60
31
|
}
|
|
61
32
|
|
|
62
|
-
emit (ctx, ...args) {
|
|
63
|
-
|
|
64
|
-
const listeners = this.list.length > 0
|
|
33
|
+
emit (ctx, name, ...args) {
|
|
34
|
+
const list = [...this.list]
|
|
65
35
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const l = this.list[i]
|
|
36
|
+
for (let i = 0, n = list.length; i < n; i++) {
|
|
37
|
+
const l = list[i]
|
|
69
38
|
|
|
70
|
-
|
|
71
|
-
if (l[1] === true) this.list.splice(i--, 1)
|
|
72
|
-
}
|
|
73
|
-
} finally {
|
|
74
|
-
this.emitting = false
|
|
39
|
+
if (l[1] === true) this.remove(ctx, name, l[0])
|
|
75
40
|
|
|
76
|
-
|
|
77
|
-
const fns = this.removing
|
|
78
|
-
this.removing = null
|
|
79
|
-
for (const fn of fns) this.remove(fn)
|
|
80
|
-
}
|
|
41
|
+
l[0].call(ctx, ...args)
|
|
81
42
|
}
|
|
82
43
|
|
|
83
|
-
return
|
|
44
|
+
return list.length > 0
|
|
84
45
|
}
|
|
85
46
|
}
|
|
86
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
|
+
|
|
87
66
|
module.exports = exports = class EventEmitter {
|
|
88
67
|
constructor () {
|
|
89
68
|
this._events = Object.create(null)
|
|
90
69
|
}
|
|
91
70
|
|
|
92
71
|
addListener (name, fn) {
|
|
93
|
-
return this
|
|
72
|
+
return appendListener(this, name, fn, false)
|
|
94
73
|
}
|
|
95
74
|
|
|
96
|
-
|
|
97
|
-
return this
|
|
75
|
+
addOnceListener (name, fn) {
|
|
76
|
+
return appendListener(this, name, fn, true)
|
|
98
77
|
}
|
|
99
78
|
|
|
100
79
|
prependListener (name, fn) {
|
|
101
|
-
|
|
102
|
-
e.prepend(fn)
|
|
103
|
-
this.emit('newListener', name, fn)
|
|
104
|
-
return this
|
|
80
|
+
return prependListener(this, name, fn, false)
|
|
105
81
|
}
|
|
106
82
|
|
|
107
83
|
prependOnceListener (name, fn) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
84
|
+
return prependListener(this, name, fn, true)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
removeListener (name, fn) {
|
|
88
|
+
return removeListener(this, name, fn)
|
|
111
89
|
}
|
|
112
90
|
|
|
113
91
|
on (name, fn) {
|
|
114
|
-
|
|
115
|
-
e.append(fn)
|
|
116
|
-
this.emit('newListener', name, fn)
|
|
117
|
-
return this
|
|
92
|
+
return appendListener(this, name, fn, false)
|
|
118
93
|
}
|
|
119
94
|
|
|
120
95
|
once (name, fn) {
|
|
121
|
-
this
|
|
122
|
-
this._events[name].once(fn)
|
|
123
|
-
return this
|
|
96
|
+
return appendListener(this, name, fn, true)
|
|
124
97
|
}
|
|
125
98
|
|
|
126
99
|
off (name, fn) {
|
|
127
|
-
|
|
128
|
-
if (e !== undefined) e.remove(fn)
|
|
129
|
-
this.emit('removeListener', name, fn)
|
|
130
|
-
return this
|
|
100
|
+
return removeListener(this, name, fn)
|
|
131
101
|
}
|
|
132
102
|
|
|
133
103
|
emit (name, ...args) {
|
|
134
104
|
const e = this._events[name]
|
|
135
|
-
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]
|
|
136
111
|
}
|
|
137
112
|
|
|
138
113
|
listenerCount (name) {
|
|
@@ -158,7 +133,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
158
133
|
|
|
159
134
|
return new Promise((resolve, reject) => {
|
|
160
135
|
if (signal) {
|
|
161
|
-
if (signal.aborted) return
|
|
136
|
+
if (signal.aborted) return abort()
|
|
162
137
|
|
|
163
138
|
signal.addEventListener('abort', abort)
|
|
164
139
|
}
|
|
@@ -170,7 +145,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
170
145
|
})
|
|
171
146
|
|
|
172
147
|
function abort () {
|
|
173
|
-
reject(signal.reason)
|
|
148
|
+
reject(errors.OPERATION_ABORTED(signal.reason))
|
|
174
149
|
}
|
|
175
150
|
})
|
|
176
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