@credo/cls
Version:
Continuation Local Storage for Credo application
43 lines • 1.33 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
// IMPORTS
// ================================================================================================
const async_hooks = require("async_hooks");
// MODULE VARIABLES
// ================================================================================================
let ctxMap = new Map();
let hooks;
// PUBLIC FUNCTIONS
// ================================================================================================
function setContext(ctx) {
const eid = async_hooks.executionAsyncId();
if (ctxMap.has(eid)) {
// TODO: throw error
}
ctxMap.set(eid, ctx);
}
exports.setContext = setContext;
function getContext() {
const eid = async_hooks.executionAsyncId();
return ctxMap.get(eid);
}
exports.getContext = getContext;
function enable() {
hooks = async_hooks.createHook({
init(id, type, triggerAsyncId) {
const ctx = ctxMap.get(triggerAsyncId);
ctxMap.set(id, ctx);
},
destroy(id) {
ctxMap.delete(id);
}
}).enable();
}
exports.enable = enable;
function disable() {
hooks.disable();
hooks = undefined;
ctxMap = new Map();
}
exports.disable = disable;
//# sourceMappingURL=index.js.map
;