Comparing version 1.0.5 to 1.0.6
@@ -12,3 +12,3 @@ module.exports = { | ||
"sourceType": "module", | ||
"ecmaVersion": 2017 | ||
"ecmaVersion": 2018 | ||
}, | ||
@@ -15,0 +15,0 @@ "rules": { |
## Release notes | ||
__1.0.6__ | ||
Refactored and less memory trace and better performance. | ||
__1.0.5__ | ||
@@ -3,0 +5,0 @@ Bugfix for start(). |
let fs = require('fs'); | ||
let ah = require('async_hooks'); | ||
let {AsyncResource} = ah; | ||
let major = require('./major'); | ||
@@ -46,8 +47,12 @@ | ||
function run(cb) { | ||
return Promise.resolve(1) | ||
.then(() => { | ||
log(`start context ${ ah.executionAsyncId()}`); | ||
stack[ah.executionAsyncId()].contexts[ns] = c; | ||
if (cls.debug) | ||
log('run'); | ||
const resource = new AsyncResource('node-cls'); | ||
return resource.runInAsyncScope(() => { | ||
let asyncId = ah.executionAsyncId(); | ||
let current = stack[asyncId]; | ||
current.contexts[ns] = c; | ||
if (cb) | ||
return cb(); | ||
}); | ||
}); | ||
} | ||
@@ -58,3 +63,3 @@ | ||
throw new Error('start() is not supported in nodejs < v12.0.0'); | ||
return Promise.resolve(1).then(() => { | ||
return Promise.resolve().then(() => { | ||
stack[ah.executionAsyncId()].contexts[ns] = c; | ||
@@ -65,15 +70,2 @@ }); | ||
function getAllContexts(asyncId) { | ||
let obj = {}; | ||
while(stack[asyncId]) { | ||
for (let ns in stack[asyncId].contexts) { | ||
if (!obj[ns]) { | ||
obj[ns] = stack[asyncId].contexts[ns]; | ||
} | ||
} | ||
asyncId = stack[asyncId].parent; | ||
} | ||
return obj; | ||
} | ||
function getContext(ns) { | ||
@@ -92,3 +84,4 @@ let asyncId = ah.executionAsyncId(); | ||
async function exitContext(ns) { | ||
log('exit Context'); | ||
if (cls.debug) | ||
log('exit Context'); | ||
let asyncId = ah.executionAsyncId(); | ||
@@ -107,3 +100,4 @@ let context = getContext(ns); | ||
while(stack[asyncId]) { | ||
log(`exit ${ asyncId}`); | ||
if (cls.debug) | ||
log(`exit ${ asyncId}`); | ||
asyncId = stack[asyncId].parent; | ||
@@ -126,20 +120,21 @@ let parent = stack[asyncId]; | ||
function init(asyncId, type, triggerId) { | ||
let node = { contexts: {} }; | ||
Object.defineProperty(node, 'id', { | ||
enumerable: false, | ||
value: asyncId | ||
}); | ||
Object.defineProperty(node, 'parent', { | ||
enumerable: false, | ||
value: triggerId | ||
}); | ||
Object.defineProperty(node, 'children', { | ||
enumerable: false, | ||
value: {} | ||
}); | ||
stack[asyncId] = node; | ||
if (stack[triggerId]) { | ||
stack[triggerId].children[asyncId] = node; | ||
function newNode(asyncId, triggerId, contexts) { | ||
return { contexts, id: asyncId, parent: triggerId}; | ||
} | ||
function init(asyncId, _type, triggerId) { | ||
let parent = stack[triggerId]; | ||
let contexts = {}; | ||
if (!parent) { | ||
if (cls.debug) | ||
log('no parent'); | ||
stack[asyncId] = newNode(asyncId, triggerId, contexts); | ||
} | ||
else { | ||
Object.assign(contexts, parent.contexts); | ||
stack[asyncId] = newNode(asyncId, triggerId, contexts); | ||
} | ||
if (cls.debug) | ||
log('init ' + asyncId); | ||
} | ||
@@ -153,12 +148,9 @@ | ||
function destroy(asyncId) { | ||
log(`destroy ${ asyncId}`); | ||
if (stack[asyncId]) { | ||
let contexts = getAllContexts(asyncId); | ||
for (let childId in stack[asyncId].children) { | ||
let child = stack[asyncId].children[childId]; | ||
for (let ns in contexts) { | ||
child.contexts[ns] = child.contexts[ns] || contexts[ns]; | ||
} | ||
} | ||
if (stack[asyncId]) | ||
delete stack[asyncId]; | ||
if (cls.debug) { | ||
log(`destroy ${ asyncId} : ${ Object.keys(stack).length}`); | ||
log(`keys ${ Object.keys(stack)}`); | ||
} | ||
delete stack[asyncId]; | ||
@@ -165,0 +157,0 @@ } |
{ | ||
"name": "node-cls", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Continuation Local Storage based on async_hooks", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
15469
311