@aofl/middleware
Advanced tools
Comparing version 2.0.0 to 2.1.0-alpha.4
@@ -25,4 +25,9 @@ /** | ||
use(callback, hook) { | ||
if (typeof callback !== 'function') { throw new Error('callback must be a function'); } | ||
if (typeof this.middleware[hook] === 'undefined') { throw new Error(`Only ${Object.keys(this.middleware)} hooks are supported.`); } | ||
if (typeof callback !== 'function') { | ||
throw new Error('callback must be a function'); | ||
} | ||
if (typeof this.middleware[hook] === 'undefined') { | ||
throw new Error(`Only ${Object.keys(this.middleware)} hooks are supported.`); | ||
} | ||
this.middleware[hook].push({ | ||
@@ -32,2 +37,4 @@ callback, | ||
}); | ||
return this.createUnsubscribeFn(); | ||
} | ||
@@ -80,4 +87,29 @@ | ||
} | ||
/** | ||
* Creates an unsubscribe function | ||
* | ||
* @private | ||
* @param {String} hook | ||
* @param {function} callback | ||
*/ | ||
createUnsubscribeFn(hook, callback) { | ||
const unsubscribe = () => { | ||
if (unsubscribe.executed) { return; } | ||
Object.defineProperty(unsubscribe, 'executed', { | ||
value: true | ||
}); | ||
const index = this.middleware[hook].indexOf(callback); | ||
/* istanbul ignore else */ | ||
if (index > -1) { | ||
this.middleware[hook].splice(index, 1); | ||
} | ||
}; | ||
return unsubscribe; | ||
} | ||
} | ||
export default Middleware; |
{ | ||
"name": "@aofl/middleware", | ||
"version": "2.0.0", | ||
"version": "2.1.0-alpha.4", | ||
"description": "Simple base middleware class", | ||
@@ -24,3 +24,3 @@ "main": "index.js", | ||
}, | ||
"gitHead": "91b9b3074f7031a51ce7afa9b542ff5dcfd64997" | ||
"gitHead": "7c4ae57824ce8cec4984c32b93011f7fef10e1b8" | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8201
174
3