-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
33 lines (30 loc) · 763 Bytes
/
Copy pathcontext.js
File metadata and controls
33 lines (30 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const asyncHooks = require("async_hooks");
const contexts = {};
asyncHooks
.createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (contexts[triggerAsyncId]) {
contexts[asyncId] = contexts[triggerAsyncId];
} else {
contexts[asyncId] = {};
}
},
destroy(asyncId) {
delete contexts[asyncId];
}
})
.enable();
function setContext(value) {
const asyncId = asyncHooks.executionAsyncId();
contexts[asyncId] = value;
}
function getContext() {
const asyncId = asyncHooks.executionAsyncId();
// We try to get the context object linked to our current asyncId
// if there is none, we return an empty object
return contexts[asyncId] || {};
}
module.exports = {
setContext,
getContext
};