Comparing version 2.0.0 to 2.0.1
76
index.js
@@ -0,46 +1,56 @@ | ||
'use strict' | ||
function propagate(events, source, dest) { | ||
if (arguments.length < 3) { | ||
dest = source; | ||
source = events; | ||
events = undefined; | ||
dest = source | ||
source = events | ||
events = undefined | ||
} | ||
// events should be an array or object | ||
var eventsIsObject = typeof events === 'object' | ||
if (events && !eventsIsObject) events = [events]; | ||
const eventsIsObject = typeof events === 'object' | ||
if (events && !eventsIsObject) events = [events] | ||
if (eventsIsObject) { | ||
return explicitPropagate(events, source, dest); | ||
return explicitPropagate(events, source, dest) | ||
} | ||
var oldEmit = source.emit; | ||
const shouldPropagate = eventName => | ||
events === undefined || events.includes(eventName) | ||
source.emit = function(eventType) { | ||
oldEmit.apply(source, arguments); | ||
const oldEmit = source.emit | ||
if (! events || ~events.indexOf(eventType)) { | ||
dest.emit.apply(dest, arguments); | ||
// Returns true if the event had listeners, false otherwise. | ||
// https://nodejs.org/api/events.html#events_emitter_emit_eventname_args | ||
source.emit = (eventName, ...args) => { | ||
const oldEmitHadListeners = oldEmit.call(source, eventName, ...args) | ||
let destEmitHadListeners = false | ||
if (shouldPropagate(eventName)) { | ||
destEmitHadListeners = dest.emit(eventName, ...args) | ||
} | ||
return oldEmitHadListeners || destEmitHadListeners | ||
} | ||
function end() { | ||
source.emit = oldEmit; | ||
source.emit = oldEmit | ||
} | ||
return { | ||
end: end | ||
}; | ||
}; | ||
end, | ||
} | ||
} | ||
module.exports = propagate; | ||
module.exports = propagate | ||
function explicitPropagate(events, source, dest) { | ||
var eventsIn; | ||
var eventsOut; | ||
let eventsIn | ||
let eventsOut | ||
if (Array.isArray(events)) { | ||
eventsIn = events; | ||
eventsOut = events; | ||
eventsIn = events | ||
eventsOut = events | ||
} else { | ||
eventsIn = Object.keys(events); | ||
eventsOut = eventsIn.map(function (key) { | ||
eventsIn = Object.keys(events) | ||
eventsOut = eventsIn.map(function(key) { | ||
return events[key] | ||
@@ -50,27 +60,27 @@ }) | ||
var listeners = eventsOut.map(function(event) { | ||
const listeners = eventsOut.map(function(event) { | ||
return function() { | ||
var args = Array.prototype.slice.call(arguments); | ||
args.unshift(event); | ||
dest.emit.apply(dest, args); | ||
const args = Array.prototype.slice.call(arguments) | ||
args.unshift(event) | ||
dest.emit.apply(dest, args) | ||
} | ||
}); | ||
}) | ||
listeners.forEach(register); | ||
listeners.forEach(register) | ||
return { | ||
end: end | ||
}; | ||
end, | ||
} | ||
function register(listener, i) { | ||
source.on(eventsIn[i], listener); | ||
source.on(eventsIn[i], listener) | ||
} | ||
function unregister(listener, i) { | ||
source.removeListener(eventsIn[i], listener); | ||
source.removeListener(eventsIn[i], listener) | ||
} | ||
function end() { | ||
listeners.forEach(unregister); | ||
listeners.forEach(unregister) | ||
} | ||
} |
The MIT License | ||
Copyright (c) 2015 Pedro Teixeira | ||
Copyright (c) 2015-2019 Pedro Teixeira and other contributors | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
@@ -13,10 +12,8 @@ of this software and associated documentation files (the "Software"), to deal | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
@@ -26,4 +23,1 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
THE SOFTWARE. | ||
{ | ||
"name": "propagate", | ||
"description": "Propagate events", | ||
"version": "2.0.0", | ||
"description": "Propagate events from one event emitter into another", | ||
"keywords": [ | ||
"event", | ||
"events", | ||
"emitter", | ||
"eventemitter", | ||
"propagation" | ||
], | ||
"version": "2.0.1", | ||
"author": "Pedro Teixeira <pedro.teixeira@gmail.com>", | ||
@@ -11,15 +18,18 @@ "contributors": [ | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/nock/propagate.git" | ||
}, | ||
"bugs": { | ||
"url": "http://github.com/nock/propagate/issues" | ||
}, | ||
"repository": "nock/propagate", | ||
"homepage": "http://github.com/nock/propagate#readme", | ||
"bugs": "http://github.com/nock/propagate/issues", | ||
"engines": { | ||
"node": ">= 8" | ||
}, | ||
"main": "./index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint-config-prettier": "^4.1.0", | ||
"eslint-config-standard": "^12.0.0", | ||
"eslint-plugin-import": "^2.16.0", | ||
"eslint-plugin-node": "^8.0.1", | ||
"eslint-plugin-promise": "^4.1.1", | ||
"eslint-plugin-standard": "^4.0.0", | ||
"prettier": "^1.16.4", | ||
"semantic-release": "16.0.0-beta.18", | ||
@@ -29,4 +39,10 @@ "tap": "^12.6.1" | ||
"scripts": { | ||
"test": "tap --coverage tests/index.js", | ||
"coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html" | ||
"lint": "eslint \"**/*.js\"", | ||
"unit": "tap tests/index.js", | ||
"coverage": "tap --coverage-report=html && open coverage/lcov-report/index.html", | ||
"prettier": "prettier --write \"**/*.@(js|md|yml|json)\"", | ||
"prettier:check": "prettier --check \"**/*.@(js|md|yml|json)\"", | ||
"pretest": "npm run lint", | ||
"test": "npm run -s unit", | ||
"posttest": "npm run -s prettier:check" | ||
}, | ||
@@ -38,8 +54,3 @@ "nyc": { | ||
}, | ||
"tags": [ | ||
"event", | ||
"emitter", | ||
"propagation" | ||
], | ||
"license": "MIT" | ||
} |
# propagate | ||
[![Build Status](https://travis-ci.org/nock/propagate.svg?branch=master)](https://travis-ci.org/nock/propagate) | ||
[![npm](https://img.shields.io/npm/v/nock.svg?style=flat-square)][npmjs] | ||
[![Build Status](https://img.shields.io/travis/nock/propagate/master.svg?style=flat-square)][build] | ||
[![Coverage](https://img.shields.io/coveralls/github/nock/propagate.svg?style=flat-square)][coverage] | ||
Propagate events from one event emitter into another | ||
[npmjs]: https://www.npmjs.com/package/propagate | ||
[build]: https://travis-ci.org/nock/propagate | ||
[coverage]: https://coveralls.io/github/nock/propagate | ||
Propagate events from one event emitter into another. | ||
## Install | ||
@@ -16,11 +22,11 @@ | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
propagate(ee1, ee2); | ||
var ee1 = new EventEmitter() | ||
var ee2 = new EventEmitter() | ||
propagate(ee1, ee2) | ||
ee2.on('event', function(a, b) { | ||
console.log('got propagated event', a, b); | ||
}); | ||
ee2.on('event', function(a, b) { | ||
console.log('got propagated event', a, b) | ||
}) | ||
ee1.emit('event', 'a', 'b'); | ||
ee1.emit('event', 'a', 'b') | ||
``` | ||
@@ -33,9 +39,9 @@ | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var p = propagate(ee1, ee2); | ||
var ee1 = new EventEmitter() | ||
var ee2 = new EventEmitter() | ||
var p = propagate(ee1, ee2) | ||
// ... | ||
// ... | ||
p.end(); | ||
p.end() | ||
``` | ||
@@ -46,5 +52,5 @@ | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var p = propagate(['event1', 'event2'], ee1, ee2); | ||
var ee1 = new EventEmitter() | ||
var ee2 = new EventEmitter() | ||
var p = propagate(['event1', 'event2'], ee1, ee2) | ||
``` | ||
@@ -55,8 +61,12 @@ | ||
```javascript | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var p = propagate({ | ||
'event1': 'other-event1', | ||
'event2': 'other-event2' | ||
}, ee1, ee2); | ||
var ee1 = new EventEmitter() | ||
var ee2 = new EventEmitter() | ||
var p = propagate( | ||
{ | ||
event1: 'other-event1', | ||
event2: 'other-event2', | ||
}, | ||
ee1, | ||
ee2 | ||
) | ||
``` | ||
@@ -66,2 +76,2 @@ | ||
MIT | ||
MIT |
@@ -1,140 +0,168 @@ | ||
var test = require('tap').test; | ||
var EventEmitter = require('events').EventEmitter; | ||
var propagate = require('..'); | ||
'use strict' | ||
test('propagates events', function(t) { | ||
t.plan(12); | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
propagate(ee1, ee2); | ||
const { test } = require('tap') | ||
const { EventEmitter } = require('events') | ||
const propagate = require('..') | ||
ee2.on('event-1', function(a, b, c) { | ||
t.equal(a, 'a'); | ||
t.equal(b, 'b'); | ||
t.equal(c, undefined); | ||
}); | ||
test('propagates events', t => { | ||
t.plan(16) | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
propagate(ee1, ee2) | ||
ee2.on('event-2', function(a, b, c) { | ||
t.equal(a, 'c'); | ||
t.equal(b, 'd'); | ||
t.equal(c, undefined); | ||
}); | ||
ee2.on('event-1', (a, b, c) => { | ||
t.equal(a, 'a') | ||
t.equal(b, 'b') | ||
t.equal(c, undefined) | ||
}) | ||
ee1.emit('event-1', 'a', 'b'); | ||
ee1.emit('event-1', 'a', 'b'); | ||
ee1.emit('event-2', 'c', 'd'); | ||
ee1.emit('event-2', 'c', 'd'); | ||
ee2.on('event-2', (a, b, c) => { | ||
t.equal(a, 'c') | ||
t.equal(b, 'd') | ||
t.equal(c, undefined) | ||
}) | ||
}); | ||
t.true(ee1.emit('event-1', 'a', 'b')) | ||
t.true(ee1.emit('event-1', 'a', 'b')) | ||
t.true(ee1.emit('event-2', 'c', 'd')) | ||
t.true(ee1.emit('event-2', 'c', 'd')) | ||
}) | ||
test('propagates can end', function(t) { | ||
t.plan(1); | ||
test('propagates can end', t => { | ||
t.plan(3) | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var prop = propagate(ee1, ee2); | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
const prop = propagate(ee1, ee2) | ||
ee2.on('event', function() { | ||
t.ok('true', 'propagated'); | ||
}); | ||
ee2.on('event', () => { | ||
t.ok('true', 'propagated') | ||
}) | ||
ee1.emit('event'); | ||
prop.end(); | ||
ee1.emit('event'); | ||
}); | ||
t.true(ee1.emit('event')) | ||
prop.end() | ||
t.false(ee1.emit('event')) | ||
}) | ||
test('after propagation old one still emits', function(t) { | ||
t.plan(2); | ||
test('after propagation old one still emits', t => { | ||
t.plan(4) | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
var prop = propagate(ee1, ee2); | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
const prop = propagate(ee1, ee2) | ||
ee1.on('event', function() { | ||
t.ok('true', 'propagated'); | ||
}); | ||
ee1.on('event', () => { | ||
t.ok('true', 'propagated') | ||
}) | ||
ee1.emit('event'); | ||
prop.end(); | ||
ee1.emit('event'); | ||
}); | ||
t.true(ee1.emit('event')) | ||
prop.end() | ||
t.true(ee1.emit('event')) | ||
}) | ||
test('emit on source before destination', function (t) { | ||
t.plan(1); | ||
test('emit on source before destination', t => { | ||
t.plan(2) | ||
var source = new EventEmitter(); | ||
var dest = new EventEmitter(); | ||
const source = new EventEmitter() | ||
const dest = new EventEmitter() | ||
// Set up test case for "propagate all" | ||
// `count` should have been incremented by handler on source when handler on dest is invoked | ||
var count = 0; | ||
propagate(source, dest); | ||
source.on('event', function () { | ||
count++; | ||
}); | ||
dest.on('event', function () { | ||
t.equal(count, 1, 'emit on source first'); | ||
}); | ||
let count = 0 | ||
propagate(source, dest) | ||
source.on('event', () => { | ||
count++ | ||
}) | ||
dest.on('event', () => { | ||
t.equal(count, 1, 'emit on source first') | ||
}) | ||
// Emit the events for assertion | ||
source.emit('event'); | ||
}); | ||
t.true(source.emit('event')) | ||
}) | ||
test('is able to propagate only certain events', function(t) { | ||
t.plan(2); | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
test('is able to propagate only certain events', t => { | ||
t.plan(6) | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
// propagate only event-1 and event-2, leaving out | ||
var p = propagate(['event-1', 'event-2'], ee1, ee2); | ||
const p = propagate(['event-1', 'event-2'], ee1, ee2) | ||
ee2.on('event-1', function() { | ||
t.ok(true, 'event 1 received'); | ||
}); | ||
ee2.on('event-1', () => { | ||
t.ok(true, 'event 1 received') | ||
}) | ||
ee2.on('event-2', function(a, b, c) { | ||
t.ok(true, 'event 2 received'); | ||
}); | ||
ee2.on('event-2', (a, b, c) => { | ||
t.ok(true, 'event 2 received') | ||
}) | ||
ee2.on('event-3', function(a, b, c) { | ||
t.ok(false, 'event 3 should not have been received'); | ||
}); | ||
ee2.on('event-3', (a, b, c) => { | ||
t.fail('event 3 should not have been received') | ||
}) | ||
ee1.emit('event-1'); | ||
ee1.emit('event-2'); | ||
ee1.emit('event-3'); | ||
t.true(ee1.emit('event-1')) | ||
t.true(ee1.emit('event-2')) | ||
t.false(ee1.emit('event-3')) | ||
p.end(); | ||
p.end() | ||
ee1.emit('event-1'); | ||
}); | ||
t.false(ee1.emit('event-1')) | ||
}) | ||
test('is able to propagate and map certain events', function(t) { | ||
t.plan(2); | ||
var ee1 = new EventEmitter(); | ||
var ee2 = new EventEmitter(); | ||
test('is able to propagate and map certain events', t => { | ||
t.plan(6) | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
// propagate only event-1 and event-2, leaving out | ||
var p = propagate({ | ||
'event-1': 'other-event-1', | ||
'event-2': 'other-event-2' | ||
}, ee1, ee2); | ||
const p = propagate( | ||
{ | ||
'event-1': 'other-event-1', | ||
'event-2': 'other-event-2', | ||
}, | ||
ee1, | ||
ee2 | ||
) | ||
ee2.on('other-event-1', function() { | ||
t.ok(true, 'event 1 received'); | ||
}); | ||
ee2.on('other-event-1', () => { | ||
t.ok(true, 'event 1 received') | ||
}) | ||
ee2.on('other-event-2', function(a, b, c) { | ||
t.ok(true, 'event 2 received'); | ||
}); | ||
ee2.on('other-event-2', (a, b, c) => { | ||
t.ok(true, 'event 2 received') | ||
}) | ||
ee2.on('event-3', function(a, b, c) { | ||
t.ok(false, 'event 3 should not have been received'); | ||
}); | ||
ee2.on('event-3', (a, b, c) => { | ||
t.fail('event 3 should not have been received') | ||
}) | ||
ee1.emit('event-1'); | ||
ee1.emit('event-2'); | ||
ee1.emit('event-3'); | ||
t.true(ee1.emit('event-1')) | ||
t.true(ee1.emit('event-2')) | ||
t.false(ee1.emit('event-3')) | ||
p.end(); | ||
p.end() | ||
ee1.emit('event-1'); | ||
}); | ||
t.false(ee1.emit('event-1')) | ||
}) | ||
test('is able to propagate a single event', t => { | ||
t.plan(5) | ||
const ee1 = new EventEmitter() | ||
const ee2 = new EventEmitter() | ||
const p = propagate('event-1', ee1, ee2) | ||
ee2.on('event-1', () => { | ||
t.ok(true, 'event 1 received') | ||
}) | ||
ee2.on('event-2', (a, b, c) => { | ||
t.fail('event 3 should not have been received') | ||
}) | ||
t.true(ee1.emit('event-1')) | ||
t.false(ee1.emit('event-2')) | ||
p.end() | ||
t.false(ee1.emit('event-1')) | ||
t.false(ee1.emit('event-2')) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
10954
10
198
0
73
10
2
1