bare-events 2.2.0 → 2.2.2
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 +24 -2
- package/lib/errors.js +4 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -3,14 +3,17 @@ const errors = require('./lib/errors')
|
|
|
3
3
|
class EventListener {
|
|
4
4
|
constructor () {
|
|
5
5
|
this.list = []
|
|
6
|
+
this.count = 0
|
|
6
7
|
}
|
|
7
8
|
|
|
8
9
|
append (ctx, name, fn, once) {
|
|
10
|
+
this.count++
|
|
9
11
|
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
10
12
|
this.list.push([fn, once])
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
prepend (ctx, name, fn, once) {
|
|
16
|
+
this.count++
|
|
14
17
|
ctx.emit('newListener', name, fn) // Emit BEFORE adding
|
|
15
18
|
this.list.unshift([fn, once])
|
|
16
19
|
}
|
|
@@ -22,9 +25,11 @@ class EventListener {
|
|
|
22
25
|
if (l[0] === fn) {
|
|
23
26
|
this.list.splice(i, 1)
|
|
24
27
|
|
|
25
|
-
if (this.
|
|
28
|
+
if (this.count === 1) delete ctx._events[name]
|
|
26
29
|
|
|
27
30
|
ctx.emit('removeListener', name, fn) // Emit AFTER removing
|
|
31
|
+
|
|
32
|
+
this.count--
|
|
28
33
|
return
|
|
29
34
|
}
|
|
30
35
|
}
|
|
@@ -34,11 +39,13 @@ class EventListener {
|
|
|
34
39
|
const list = [...this.list]
|
|
35
40
|
this.list = []
|
|
36
41
|
|
|
42
|
+
if (this.count === list.length) delete ctx._events[name]
|
|
43
|
+
|
|
37
44
|
for (let i = list.length - 1; i >= 0; i--) {
|
|
38
45
|
ctx.emit('removeListener', name, list[i][0]) // Emit AFTER removing
|
|
39
46
|
}
|
|
40
47
|
|
|
41
|
-
|
|
48
|
+
this.count -= list.length
|
|
42
49
|
}
|
|
43
50
|
|
|
44
51
|
emit (ctx, name, ...args) {
|
|
@@ -74,6 +81,20 @@ function removeListener (ctx, name, fn) {
|
|
|
74
81
|
return ctx
|
|
75
82
|
}
|
|
76
83
|
|
|
84
|
+
function throwUnhandledError (...args) {
|
|
85
|
+
let err
|
|
86
|
+
|
|
87
|
+
if (args.length > 0) err = args[0]
|
|
88
|
+
|
|
89
|
+
if (err instanceof Error === false) err = errors.UNHANDLED_ERROR(err)
|
|
90
|
+
|
|
91
|
+
if (Error.captureStackTrace) {
|
|
92
|
+
Error.captureStackTrace(err, exports.prototype.emit)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
queueMicrotask(() => { throw err })
|
|
96
|
+
}
|
|
97
|
+
|
|
77
98
|
module.exports = exports = class EventEmitter {
|
|
78
99
|
constructor () {
|
|
79
100
|
this._events = Object.create(null)
|
|
@@ -112,6 +133,7 @@ module.exports = exports = class EventEmitter {
|
|
|
112
133
|
}
|
|
113
134
|
|
|
114
135
|
emit (name, ...args) {
|
|
136
|
+
if (name === 'error' && this._events.error === undefined) throwUnhandledError(...args)
|
|
115
137
|
const e = this._events[name]
|
|
116
138
|
return e === undefined ? false : e.emit(this, name, ...args)
|
|
117
139
|
}
|
package/lib/errors.js
CHANGED
|
@@ -15,4 +15,8 @@ module.exports = class EventEmitterError extends Error {
|
|
|
15
15
|
static OPERATION_ABORTED (cause, msg = 'Operation aborted') {
|
|
16
16
|
return new EventEmitterError(msg, 'OPERATION_ABORTED', EventEmitterError.OPERATION_ABORTED, { cause })
|
|
17
17
|
}
|
|
18
|
+
|
|
19
|
+
static UNHANDLED_ERROR (cause, msg = 'Unhandled error') {
|
|
20
|
+
return new EventEmitterError(msg, 'UNHANDLED_ERROR', EventEmitterError.UNHANDLED_ERROR, { cause })
|
|
21
|
+
}
|
|
18
22
|
}
|