callback-registry
Advanced tools
Comparing version 2.3.3 to 2.4.0
/** | ||
* Factory method that creates a new callback registry instance | ||
*/ | ||
declare function Factory(): CallbackRegistry; | ||
declare function Factory(options?: InitOptions): CallbackRegistry; | ||
export default Factory; | ||
@@ -35,1 +35,7 @@ | ||
} | ||
export type ErrorHandler = (err: Error) => void | ||
export interface InitOptions { | ||
errorHandling: "log" | "silent" | "throw" | ErrorHandler; | ||
} |
"use strict"; | ||
function createRegistry() { | ||
function createRegistry(options) { | ||
if (options && options.errorHandling | ||
&& typeof options.errorHandling !== "function" | ||
&& options.errorHandling !== "log" | ||
&& options.errorHandling !== "silent" | ||
&& options.errorHandling !== "throw") { | ||
throw new Error("Invalid options passed to createRegistry. Prop errorHandling should be [\"log\" | \"silent\" | \"throw\" | (err) => void], but " + typeof options.errorHandling + " was passed"); | ||
} | ||
var _userErrorHandler = options && typeof options.errorHandling === "function" && options.errorHandling; | ||
var callbacks = {}; | ||
@@ -42,2 +50,3 @@ function add(key, callback) { | ||
results.push(undefined); | ||
_handleError(err, key); | ||
} | ||
@@ -47,2 +56,21 @@ }); | ||
} | ||
function _handleError(exceptionArtifact, key) { | ||
var errParam = exceptionArtifact instanceof Error ? exceptionArtifact : new Error(exceptionArtifact); | ||
if (_userErrorHandler) { | ||
_userErrorHandler(errParam); | ||
return; | ||
} | ||
var msg = "[ERROR] callback-registry: User callback for key \"" + key + "\" failed: " + errParam.stack; | ||
if (options) { | ||
switch (options.errorHandling) { | ||
case "log": | ||
return console.error(msg); | ||
case "silent": | ||
return; | ||
case "throw": | ||
throw new Error(msg); | ||
} | ||
} | ||
console.error(msg); | ||
} | ||
function clear() { | ||
@@ -49,0 +77,0 @@ callbacks = {}; |
{ | ||
"name": "callback-registry", | ||
"version": "2.3.3", | ||
"version": "2.4.0", | ||
"description": "Registry for callbacks", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
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
7449
118
1