clusterhub
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -62,3 +62,3 @@ const cluster = require('cluster'); | ||
this.subscribe = this.on; | ||
this.removeLIstener = this.off; | ||
this.removeListener = this.off; | ||
this.unsubscribe = this.off; | ||
@@ -209,2 +209,16 @@ | ||
/** | ||
* @param {string} event | ||
* @param {Function(...args)} listener | ||
* @param {boolean} append | ||
*/ | ||
_on(event, listener, append) { | ||
if (!this._listeners.has(event)) this._listeners.set(event, []); | ||
this._listeners.get(event)[append ? 'push' : 'unshift'](listener); | ||
if (isWorker) { | ||
this._sendMaster({ cmd: commands.ON, event }); | ||
} | ||
} | ||
/** | ||
* Starts listening to an event within the hub. | ||
@@ -217,8 +231,14 @@ * | ||
on(event, listener) { | ||
if (!this._listeners.has(event)) this._listeners.set(event, []); | ||
this._listeners.get(event).push(listener); | ||
this._on(event, listener, true); | ||
} | ||
if (isWorker) { | ||
this._sendMaster({ cmd: commands.ON, event }); | ||
} | ||
/** | ||
* Adds the listener at the beginner of the list of listeners, | ||
* calling it before other listeners. | ||
* | ||
* @param {string} event | ||
* @param {Function(...args)} listener | ||
*/ | ||
prependListener(event, listener) { | ||
this._on(event, listener, false); | ||
} | ||
@@ -255,4 +275,5 @@ | ||
* @param {Function(...args)} listener | ||
* @param {boolean} append | ||
*/ | ||
many(n, event, listener) { | ||
_many(n, event, listener, append) { | ||
const wrapper = (...args) => { | ||
@@ -263,6 +284,26 @@ if (--n === 0) this.off(event, listener); | ||
wrapper.listener = listener; | ||
this.on(event, wrapper); | ||
this[append ? 'on' : 'prependListener'](event, wrapper); | ||
} | ||
/** | ||
* Listens for n number of the event and then stops listening. | ||
* | ||
* @param {number} n | ||
* @param {string} event | ||
* @param {Function(...args)} listener | ||
*/ | ||
many(n, event, listener) { | ||
this._many(n, event, listener, true); | ||
} | ||
/** | ||
* @param {number} n | ||
* @param {string} event | ||
* @param {Function(...args)} listener | ||
*/ | ||
prependManyListener(n, event, listener) { | ||
this._many(n, event, listener, false); | ||
} | ||
/** | ||
* Shortcut for `many(1, event, listener)` | ||
@@ -274,6 +315,14 @@ * | ||
once(event, listener) { | ||
this.many(1, event, listener); | ||
this._many(1, event, listener, true); | ||
} | ||
/** | ||
* @param {string} event | ||
* @param {Function(...args)} listener | ||
*/ | ||
prependOnceListener(event, listener) { | ||
this._many(1, event, listener, false); | ||
} | ||
/** | ||
* Removes all listeners for the event. | ||
@@ -280,0 +329,0 @@ * |
@@ -11,3 +11,3 @@ { | ||
], | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"repository": { | ||
@@ -14,0 +14,0 @@ "type": "git", |
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
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
21144
574