bare-events 1.3.0 → 2.1.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 +82 -82
- package/lib/errors.js +18 -0
- package/package.json +4 -3
package/index.js
CHANGED
|
@@ -1,138 +1,124 @@
|
|
|
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
|
-
|
|
8
|
+
append (ctx, name, fn, once) {
|
|
9
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
10
|
+
this.list.push([fn, once])
|
|
11
|
+
}
|
|
10
12
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
prepend (ctx, name, fn, once) {
|
|
14
|
+
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
15
|
+
this.list.unshift([fn, once])
|
|
16
|
+
}
|
|
13
17
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
+
remove (ctx, name, fn) {
|
|
19
|
+
for (let i = 0, n = this.list.length; i < n; i++) {
|
|
20
|
+
const l = this.list[i]
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
if (l[0] === fn) {
|
|
23
|
+
this.list.splice(i, 1)
|
|
21
24
|
|
|
22
|
-
|
|
23
|
-
l[1] = true
|
|
24
|
-
return
|
|
25
|
-
}
|
|
25
|
+
if (this.list.length === 0) delete ctx._events[name]
|
|
26
26
|
|
|
27
|
-
|
|
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
|
|
27
|
+
ctx.emit('removeListener', name, fn) // Emit AFTER removing
|
|
32
28
|
return
|
|
33
29
|
}
|
|
34
30
|
}
|
|
35
31
|
}
|
|
36
32
|
|
|
37
|
-
|
|
38
|
-
this.list
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
prepend (fn) {
|
|
42
|
-
this.list.unshift([fn, false])
|
|
43
|
-
}
|
|
33
|
+
removeAll (ctx, name) {
|
|
34
|
+
const list = [...this.list]
|
|
35
|
+
this.list = []
|
|
44
36
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (this.removing === null) this.removing = []
|
|
48
|
-
this.removing.push(fn)
|
|
49
|
-
return
|
|
37
|
+
for (let i = list.length - 1; i >= 0; i--) {
|
|
38
|
+
ctx.emit('removeListener', name, list[i][0]) // Emit AFTER removing
|
|
50
39
|
}
|
|
51
40
|
|
|
52
|
-
|
|
53
|
-
const l = this.list[i]
|
|
54
|
-
|
|
55
|
-
if (l[0] === fn) {
|
|
56
|
-
this.list.splice(i, 1)
|
|
57
|
-
return
|
|
58
|
-
}
|
|
59
|
-
}
|
|
41
|
+
if (this.list.length === 0) delete ctx._events[name]
|
|
60
42
|
}
|
|
61
43
|
|
|
62
|
-
emit (ctx, ...args) {
|
|
63
|
-
|
|
64
|
-
const listeners = this.list.length > 0
|
|
44
|
+
emit (ctx, name, ...args) {
|
|
45
|
+
const list = [...this.list]
|
|
65
46
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const l = this.list[i]
|
|
47
|
+
for (let i = 0, n = list.length; i < n; i++) {
|
|
48
|
+
const l = list[i]
|
|
69
49
|
|
|
70
|
-
|
|
71
|
-
if (l[1] === true) this.list.splice(i--, 1)
|
|
72
|
-
}
|
|
73
|
-
} finally {
|
|
74
|
-
this.emitting = false
|
|
50
|
+
if (l[1] === true) this.remove(ctx, name, l[0])
|
|
75
51
|
|
|
76
|
-
|
|
77
|
-
const fns = this.removing
|
|
78
|
-
this.removing = null
|
|
79
|
-
for (const fn of fns) this.remove(fn)
|
|
80
|
-
}
|
|
52
|
+
l[0].call(ctx, ...args)
|
|
81
53
|
}
|
|
82
54
|
|
|
83
|
-
return
|
|
55
|
+
return list.length > 0
|
|
84
56
|
}
|
|
85
57
|
}
|
|
86
58
|
|
|
59
|
+
function appendListener (ctx, name, fn, once) {
|
|
60
|
+
const e = ctx._events[name] || (ctx._events[name] = new EventListener())
|
|
61
|
+
e.append(ctx, name, fn, once)
|
|
62
|
+
return ctx
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function prependListener (ctx, name, fn, once) {
|
|
66
|
+
const e = ctx._events[name] || (ctx._events[name] = new EventListener())
|
|
67
|
+
e.prepend(ctx, name, fn, once)
|
|
68
|
+
return ctx
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function removeListener (ctx, name, fn) {
|
|
72
|
+
const e = ctx._events[name]
|
|
73
|
+
if (e !== undefined) e.remove(ctx, name, fn)
|
|
74
|
+
return ctx
|
|
75
|
+
}
|
|
76
|
+
|
|
87
77
|
module.exports = exports = class EventEmitter {
|
|
88
78
|
constructor () {
|
|
89
79
|
this._events = Object.create(null)
|
|
90
80
|
}
|
|
91
81
|
|
|
92
82
|
addListener (name, fn) {
|
|
93
|
-
return this
|
|
83
|
+
return appendListener(this, name, fn, false)
|
|
94
84
|
}
|
|
95
85
|
|
|
96
|
-
|
|
97
|
-
return this
|
|
86
|
+
addOnceListener (name, fn) {
|
|
87
|
+
return appendListener(this, name, fn, true)
|
|
98
88
|
}
|
|
99
89
|
|
|
100
90
|
prependListener (name, fn) {
|
|
101
|
-
|
|
102
|
-
e.prepend(fn)
|
|
103
|
-
this.emit('newListener', name, fn)
|
|
104
|
-
return this
|
|
91
|
+
return prependListener(this, name, fn, false)
|
|
105
92
|
}
|
|
106
93
|
|
|
107
94
|
prependOnceListener (name, fn) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
95
|
+
return prependListener(this, name, fn, true)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
removeListener (name, fn) {
|
|
99
|
+
return removeListener(this, name, fn)
|
|
111
100
|
}
|
|
112
101
|
|
|
113
102
|
on (name, fn) {
|
|
114
|
-
|
|
115
|
-
e.append(fn)
|
|
116
|
-
this.emit('newListener', name, fn)
|
|
117
|
-
return this
|
|
103
|
+
return appendListener(this, name, fn, false)
|
|
118
104
|
}
|
|
119
105
|
|
|
120
106
|
once (name, fn) {
|
|
121
|
-
this
|
|
122
|
-
this._events[name].once(fn)
|
|
123
|
-
return this
|
|
107
|
+
return appendListener(this, name, fn, true)
|
|
124
108
|
}
|
|
125
109
|
|
|
126
110
|
off (name, fn) {
|
|
127
|
-
|
|
128
|
-
if (e !== undefined) e.remove(fn)
|
|
129
|
-
this.emit('removeListener', name, fn)
|
|
130
|
-
return this
|
|
111
|
+
return removeListener(this, name, fn)
|
|
131
112
|
}
|
|
132
113
|
|
|
133
114
|
emit (name, ...args) {
|
|
134
115
|
const e = this._events[name]
|
|
135
|
-
return e === undefined ? false : e.emit(this, ...args)
|
|
116
|
+
return e === undefined ? false : e.emit(this, name, ...args)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
listeners (name) {
|
|
120
|
+
const e = this._events[name]
|
|
121
|
+
return e === undefined ? [] : [...e.list]
|
|
136
122
|
}
|
|
137
123
|
|
|
138
124
|
listenerCount (name) {
|
|
@@ -145,6 +131,20 @@ module.exports = exports = class EventEmitter {
|
|
|
145
131
|
}
|
|
146
132
|
|
|
147
133
|
setMaxListeners (n) {}
|
|
134
|
+
|
|
135
|
+
removeAllListeners (name) {
|
|
136
|
+
if (arguments.length === 0) {
|
|
137
|
+
for (const key of Reflect.ownKeys(this._events)) {
|
|
138
|
+
if (key === 'removeListener') continue
|
|
139
|
+
this.removeAllListeners(key)
|
|
140
|
+
}
|
|
141
|
+
this.removeAllListeners('removeListener')
|
|
142
|
+
} else {
|
|
143
|
+
const e = this._events[name]
|
|
144
|
+
if (e !== undefined) e.removeAll(this, name)
|
|
145
|
+
}
|
|
146
|
+
return this
|
|
147
|
+
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
exports.EventEmitter = exports
|
|
@@ -158,7 +158,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
158
158
|
|
|
159
159
|
return new Promise((resolve, reject) => {
|
|
160
160
|
if (signal) {
|
|
161
|
-
if (signal.aborted) return
|
|
161
|
+
if (signal.aborted) return abort()
|
|
162
162
|
|
|
163
163
|
signal.addEventListener('abort', abort)
|
|
164
164
|
}
|
|
@@ -170,7 +170,7 @@ exports.once = function once (emitter, name, opts = {}) {
|
|
|
170
170
|
})
|
|
171
171
|
|
|
172
172
|
function abort () {
|
|
173
|
-
reject(signal.reason)
|
|
173
|
+
reject(errors.OPERATION_ABORTED(signal.reason))
|
|
174
174
|
}
|
|
175
175
|
})
|
|
176
176
|
}
|
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
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-events",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"description": "Event emitters for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
7
|
-
"index.js"
|
|
7
|
+
"index.js",
|
|
8
|
+
"lib"
|
|
8
9
|
],
|
|
9
10
|
"scripts": {
|
|
10
11
|
"test": "standard && bare test.js"
|
|
11
12
|
},
|
|
12
13
|
"repository": {
|
|
13
14
|
"type": "git",
|
|
14
|
-
"url": "https://github.com/holepunchto/bare-events.git"
|
|
15
|
+
"url": "git+https://github.com/holepunchto/bare-events.git"
|
|
15
16
|
},
|
|
16
17
|
"author": "Holepunch",
|
|
17
18
|
"license": "Apache-2.0",
|