hook-emitter
Advanced tools
Comparing version 0.1.8 to 0.2.0
@@ -23,2 +23,3 @@ 'use strict'; | ||
this._events = new Map(); | ||
this._links = []; | ||
} | ||
@@ -161,2 +162,3 @@ | ||
* @param {Object} type - The event type. | ||
* @param {Function} getListeners - A function that returns an array of listeners to compose. | ||
* @param {Function} [callback] - An optional function to call after all listeners have been fired. | ||
@@ -178,3 +180,9 @@ * @param {Function} [transform] - An function that transforms a result with the original payload. | ||
// listeners may change before the hook is called | ||
const getListeners = () => this._events.get(type); | ||
const getListeners = () => { | ||
const listeners = this._events.get(type) || []; | ||
const linkedListeners = this._links.map(link => { | ||
return link.emitter.events.get((link.prefix || '') + type) || []; | ||
}); | ||
return listeners.concat.apply(listeners, linkedListeners); | ||
}; | ||
@@ -187,3 +195,3 @@ // return the wrapped function | ||
const listeners = getListeners() || []; | ||
const listeners = getListeners(); | ||
@@ -200,4 +208,6 @@ if (!Array.isArray(listeners)) { | ||
// clone the listeners | ||
const _listeners = callback ? listeners.concat(callback) : listeners.slice(); | ||
if (callback) { | ||
listeners.push(callback); | ||
} | ||
let index = -1; | ||
@@ -219,3 +229,3 @@ | ||
let listener = _listeners[i]; | ||
let listener = listeners[i]; | ||
if (!listener) { | ||
@@ -376,4 +386,46 @@ return Promise.resolve(payload); | ||
} | ||
/** | ||
* Links all listeners from another hook emitter into this instance. When an | ||
* event is emitted, it will notify all of this instance's listeners, then | ||
* notify all linked hook emitter's listeners. Same applies to hooked | ||
* functions. | ||
* | ||
* @param {HookEmitter} emitter - A hook emitter to link to. | ||
* @param {String} [prefix] - A string to prefix to all emitted event names. | ||
* @returns {HookEmitter} | ||
* @access public | ||
*/ | ||
link(emitter, prefix) { | ||
if (!(emitter instanceof HookEmitter)) { | ||
throw new TypeError('Expected argument to be a HookEmitter.'); | ||
} | ||
this._links.push({ emitter, prefix }); | ||
return this; | ||
} | ||
/** | ||
* Unlinks all listeners from another hook emitter from this instance. | ||
* | ||
* @param {HookEmitter} emitter - A hook emitter to unlink. | ||
* @returns {HookEmitter} | ||
* @access public | ||
*/ | ||
unlink(emitter) { | ||
if (!(emitter instanceof HookEmitter)) { | ||
throw new TypeError('Expected argument to be a HookEmitter.'); | ||
} | ||
for (let i = 0; i < this._links.length; i++) { | ||
if (this._links[i].emitter === emitter) { | ||
this._links.splice(i--, 1); | ||
} | ||
} | ||
return this; | ||
} | ||
} | ||
exports.HookEmitter = HookEmitter; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "hook-emitter", | ||
"version": "0.1.8", | ||
"version": "0.2.0", | ||
"description": "Event emitter with support for asynchronous handlers and a sweet function hook mechanism.", | ||
@@ -16,3 +16,3 @@ "main": "./dist/index.js", | ||
"docs": "gulp docs", | ||
"prepublish": "gulp build", | ||
"prepublish": "gulp build && npm shrinkwrap", | ||
"test": "gulp test" | ||
@@ -25,3 +25,3 @@ }, | ||
"babel-eslint": "^5.0.0", | ||
"babel-polyfill": "^6.5.0", | ||
"babel-polyfill": "^6.7.2", | ||
"babel-preset-nodejs-lts": "^1.2.1", | ||
@@ -28,0 +28,0 @@ "chai": "^3.5.0", |
@@ -141,2 +141,19 @@ # hook-emitter | ||
#### `link(emitter, prefix)` | ||
Links another `HookEmitter` to this instance. Useful if you have a class that | ||
extends a `HookEmitter`, then another `HookEmitter` that you want to receive | ||
the exact same events. | ||
* `emitter` HookEmitter - The hook emitter to link to this instance. | ||
* `prefix` String (optional) - An optional prefix to prepend to the event name | ||
being emitted from all linked emitters. | ||
#### `unlink(emitter)` | ||
Unlinks another `HookEmitter` from this instance. It does the opposite of | ||
`link()`. | ||
* `emitter` HookEmitter - The hook emitter to unlink. | ||
## License | ||
@@ -143,0 +160,0 @@ |
Sorry, the diff of this file is not supported yet
NPM Shrinkwrap
Supply chain riskPackage contains a shrinkwrap file. This may allow the package to bypass normal install procedures.
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
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
138522
8
3053
196
1