disposable-cls
Version:
Provides disposable continuation local storage for Node.js.
40 lines (39 loc) • 1.44 kB
JavaScript
;
var ContextStack_1 = require("./ContextStack");
// Pull in the async-listener pollyfil if required
if (!process.addAsyncListener) {
require("async-listener");
}
var contextStack = new ContextStack_1.ContextStack();
/**
* Creates scope that will be preserved for the lifetime of the supplied asynchronous invocations.
*
* @param contextItems An array of objects that will be scoped.
* @param asyncFunction The root asynchronous function for which scope is to be preserved.
*/
function using(contextItems, asyncFunction) {
contextStack.pushScope(contextItems);
setImmediate(asyncFunction);
}
exports.using = using;
/**
* Retrieves an object from the current scope.
*
* @param objectType A constructor function representing the type of object that should be returned.
* @returns An object from the current scope that is of the supplied type; or 'undefined' if no
* object of that type could be found in the current scope.
*/
function getCurrentObject(objectType) {
return contextStack.findContextObjectFromScope(objectType);
}
exports.getCurrentObject = getCurrentObject;
/**
* Bind an EventEmitter to the currently active context stack.
*
* @param emitter The EventEmitter to bind.
*/
function bindEventEmitter(emitter) {
contextStack.bindEventEmitter(emitter);
}
exports.bindEventEmitter = bindEventEmitter;
process.addAsyncListener(contextStack);