bare-events 2.2.1 → 2.3.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.
Files changed (2) hide show
  1. package/index.js +43 -5
  2. 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.list.length === 0) delete ctx._events[name]
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
- if (this.list.length === 0) delete ctx._events[name]
48
+ this.count -= list.length
42
49
  }
43
50
 
44
51
  emit (ctx, name, ...args) {
@@ -85,9 +92,7 @@ function throwUnhandledError (...args) {
85
92
  Error.captureStackTrace(err, exports.prototype.emit)
86
93
  }
87
94
 
88
- // Make sure that exceptions are reported as normal uncaughts, not
89
- // promise rejections.
90
- setImmediate(() => { throw err })
95
+ queueMicrotask(() => { throw err })
91
96
  }
92
97
 
93
98
  module.exports = exports = class EventEmitter {
@@ -283,3 +288,36 @@ exports.once = function once (emitter, name, opts = {}) {
283
288
  }
284
289
  })
285
290
  }
291
+
292
+ exports.forward = function forward (from, to, ...names) {
293
+ let opts = names.pop()
294
+
295
+ if (typeof opts === 'string') {
296
+ names.push(opts)
297
+ opts = {}
298
+ }
299
+
300
+ const {
301
+ emit = to.emit.bind(to)
302
+ } = opts
303
+
304
+ const listeners = names.map((name) => function onevent (...args) {
305
+ emit(name, ...args)
306
+ })
307
+
308
+ to
309
+ .on('newListener', (name) => {
310
+ const i = names.indexOf(name)
311
+
312
+ if (i !== -1 && to.listenerCount(name) === 0) {
313
+ from.on(name, listeners[i])
314
+ }
315
+ })
316
+ .on('removeListener', (name) => {
317
+ const i = names.indexOf(name)
318
+
319
+ if (i !== -1 && to.listenerCount(name) === 0) {
320
+ from.off(name, listeners[i])
321
+ }
322
+ })
323
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-events",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "description": "Event emitters for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [