callback-registry
Advanced tools
Comparing version 2.5.2 to 2.6.0
@@ -10,5 +10,9 @@ /** | ||
* Adds a new callback to the registry under some key. | ||
* The callback will be notified when someone executes | ||
* The callback will be notified when someone executes. | ||
* The callback will be called with all replayArguments: | ||
* e.g. add('clientAdded', (name, age) => console.log(name, age), [['Alice', 25], ['Bob', 30]]) | ||
* will invoke the callback with 'Alice', 25 and 'Bob', 30 *after* returning the unsubscribe function. | ||
* An example use case are events that need to call the provided callback with previous values (replay) and want to allow the callback to unsubscribe while replaying | ||
*/ | ||
add(key: string, callback: Callback): UnsubscribeFunction; | ||
add(key: string, callback: Callback, replayArgumentsArr?: any[] | Array<any[]>): UnsubscribeFunction; | ||
@@ -15,0 +19,0 @@ /** |
@@ -12,3 +12,3 @@ "use strict"; | ||
var callbacks = {}; | ||
function add(key, callback) { | ||
function add(key, callback, replayArgumentsArr) { | ||
var callbacksForKey = callbacks[key]; | ||
@@ -20,2 +20,22 @@ if (!callbacksForKey) { | ||
callbacksForKey.push(callback); | ||
if (replayArgumentsArr) { | ||
setTimeout(function () { | ||
replayArgumentsArr.forEach(function (replayArgument) { | ||
var _a; | ||
if ((_a = callbacks[key]) === null || _a === void 0 ? void 0 : _a.includes(callback)) { | ||
try { | ||
if (Array.isArray(replayArgument)) { | ||
callback.apply(undefined, replayArgument); | ||
} | ||
else { | ||
callback.apply(undefined, [replayArgument]); | ||
} | ||
} | ||
catch (err) { | ||
_handleError(err, key); | ||
} | ||
} | ||
}); | ||
}, 0); | ||
} | ||
return function () { | ||
@@ -22,0 +42,0 @@ var allForKey = callbacks[key]; |
{ | ||
"name": "callback-registry", | ||
"version": "2.5.2", | ||
"version": "2.6.0", | ||
"description": "Registry for callbacks", | ||
@@ -31,3 +31,3 @@ "main": "./lib/index.js", | ||
"mocha": "6.2.2", | ||
"typescript": "3.6.4" | ||
"typescript": "^3.9.6" | ||
}, | ||
@@ -34,0 +34,0 @@ "publishConfig": { |
@@ -46,3 +46,5 @@ # Intro | ||
# Change log | ||
* 2.5.0 | ||
* 2.6.0 | ||
added replayArgumentsArr that allows you to replay arguments to a new callback | ||
* 2.5.0 | ||
added clearKey method that removes a key from the registry | ||
@@ -49,0 +51,0 @@ * 2.3.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
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
9306
154
55