bare-events 2.2.2 → 2.3.1

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 +28 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -288,3 +288,31 @@ exports.once = function once (emitter, name, opts = {}) {
288
288
  }
289
289
  })
290
290
  }
291
+
292
+ exports.forward = function forward (from, to, names, opts = {}) {
293
+ if (typeof names === 'string') names = [names]
294
+
295
+ const {
296
+ emit = to.emit.bind(to)
297
+ } = opts
298
+
299
+ const listeners = names.map((name) => function onevent (...args) {
300
+ emit(name, ...args)
301
+ })
302
+
303
+ to
304
+ .on('newListener', (name) => {
305
+ const i = names.indexOf(name)
306
+
307
+ if (i !== -1 && to.listenerCount(name) === 0) {
308
+ from.on(name, listeners[i])
309
+ }
310
+ })
311
+ .on('removeListener', (name) => {
312
+ const i = names.indexOf(name)
313
+
314
+ if (i !== -1 && to.listenerCount(name) === 0) {
315
+ from.off(name, listeners[i])
316
+ }
317
+ })
318
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-events",
3
- "version": "2.2.2",
3
+ "version": "2.3.1",
4
4
  "description": "Event emitters for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [