Comparing version 0.0.2 to 0.0.3
@@ -14,3 +14,2 @@ /** | ||
private hooks; | ||
private process; | ||
/** | ||
@@ -22,17 +21,28 @@ * Creates an instance of ContinuationLocalStorage. | ||
/** | ||
* Get the data stored on current execution context | ||
* Get the current execution context data | ||
* | ||
* @param {number} [uid=this.currUid] | ||
* @returns {(T|undefined)} | ||
*/ | ||
getData(uid?: number): T | undefined; | ||
getContext(): T | undefined; | ||
/** | ||
* Set the data to store on current execution context | ||
* Set the current execution context data | ||
* | ||
* @param {T} value | ||
* @param {number} [uid=this.currUid] | ||
* @returns {(T)} | ||
*/ | ||
setContext(value: T): T; | ||
/** | ||
* Get the root execution context data | ||
* | ||
* @returns {(T|undefined)} | ||
*/ | ||
setData(value: T, uid?: number): T | undefined; | ||
getRootContext(): T | undefined; | ||
/** | ||
* Set the current execution context data | ||
* | ||
* @param {T} value | ||
* @returns {(T)} | ||
*/ | ||
setRootContext(value: T): T; | ||
/** | ||
* Get the Uid of the caller (for debugging purpose) | ||
@@ -39,0 +49,0 @@ * |
@@ -6,3 +6,4 @@ "use strict"; | ||
; | ||
const MAIN_UID = 0; | ||
let nodeproc = process; | ||
const ROOT_UID = 0; | ||
/** | ||
@@ -21,6 +22,5 @@ * | ||
constructor() { | ||
this.process = process; | ||
this._currUid = MAIN_UID; | ||
this._currUid = ROOT_UID; | ||
this.uidHookMap = new Map(); | ||
this.uidHookMap.set(MAIN_UID, { uid: MAIN_UID, handle: undefined, provider: 0, previousUid: undefined, previousHook: undefined }); | ||
this.uidHookMap.set(ROOT_UID, { uid: ROOT_UID, handle: undefined, provider: 0, previousUid: undefined, previousHook: undefined }); | ||
this.deleteOnPostHack = false; | ||
@@ -34,6 +34,6 @@ this.hooks = { | ||
this.uidHookMap.set(uid, { uid, handle, provider, previousUid, previousHook }); | ||
// this.debugUid('init', uid); | ||
if (previousUid && !previousHook) { | ||
this.process._rawDebug(`init: WARNING: uid: ${previousUid} is not registered (3)`); | ||
nodeproc._rawDebug(`init: WARNING: uid: ${previousUid} is not registered (3)`); | ||
} | ||
// this.debugUid('init', uid); | ||
}, | ||
@@ -47,5 +47,6 @@ pre: (uid) => { | ||
if (!hi.previousHook) { | ||
this.process._rawDebug(`pre : WARNING: uid: ${hi.previousUid} is not registered (2)`); | ||
nodeproc._rawDebug(`pre : WARNING: uid: ${hi.previousUid} is not registered (2)`); | ||
} | ||
} | ||
// this.debugUid('pre', uid); | ||
}, | ||
@@ -55,3 +56,3 @@ post: (uid, didThrow) => { | ||
if (uid === this._currUid) { | ||
this._currUid = MAIN_UID; | ||
this._currUid = ROOT_UID; | ||
if (this.deleteOnPostHack) { | ||
@@ -62,2 +63,3 @@ this.uidHookMap.delete(uid); | ||
} | ||
// this.debugUid('post', uid); | ||
}, | ||
@@ -83,26 +85,50 @@ destroy: (uid) => { | ||
/** | ||
* Get the data stored on current execution context | ||
* Get the current execution context data | ||
* | ||
* @param {number} [uid=this.currUid] | ||
* @returns {(T|undefined)} | ||
*/ | ||
getData(uid = this.currUid) { | ||
let hi = this.uidHookMap.get(uid); | ||
getContext() { | ||
let hi = this.uidHookMap.get(this.currUid); | ||
return hi ? hi.data : undefined; | ||
} | ||
/** | ||
* Set the data to store on current execution context | ||
* Set the current execution context data | ||
* | ||
* @param {T} value | ||
* @param {number} [uid=this.currUid] | ||
* @returns {(T)} | ||
*/ | ||
setContext(value) { | ||
if (!this.currUid || this.currUid === ROOT_UID) { | ||
throw new Error(`setContext must be called in an async context (${this.currUid})!`); | ||
} | ||
let hi = this.uidHookMap.get(this.currUid); | ||
if (!hi) { | ||
throw new Error('setContext must be called in an async context (2)!'); | ||
} | ||
hi.data = value; | ||
return value; | ||
} | ||
/** | ||
* Get the root execution context data | ||
* | ||
* @returns {(T|undefined)} | ||
*/ | ||
setData(value, uid = this.currUid) { | ||
let hi = this.uidHookMap.get(uid); | ||
getRootContext() { | ||
let hi = this.uidHookMap.get(ROOT_UID); | ||
if (!hi) { | ||
if (!uid) { | ||
throw new Error('setData must be called in an async context!'); | ||
} | ||
return undefined; | ||
throw new Error('internal error: root node not found (1)!'); | ||
} | ||
return hi ? hi.data : undefined; | ||
} | ||
/** | ||
* Set the current execution context data | ||
* | ||
* @param {T} value | ||
* @returns {(T)} | ||
*/ | ||
setRootContext(value) { | ||
let hi = this.uidHookMap.get(ROOT_UID); | ||
if (!hi) { | ||
throw new Error('internal error: root node not found (2)!'); | ||
} | ||
hi.data = value; | ||
@@ -142,7 +168,15 @@ return value; | ||
} | ||
let data = hi.data ? hi.data.toString() : 'undefined'; | ||
this.process._rawDebug(`${prefix}: uid: ${uid} previousUid: ${hi.previousUid} ${funcName} (${data})`); | ||
let data = 'undefined'; | ||
if (hi.data) { | ||
try { | ||
data = JSON.stringify(hi.data); | ||
} | ||
catch (_ignore) { | ||
data = hi.data.toString(); | ||
} | ||
} | ||
nodeproc._rawDebug(`${prefix}: uid: ${uid} previousUid: ${hi.previousUid} ${funcName} (${data})`); | ||
} | ||
else { | ||
this.process._rawDebug(`${prefix}: uid: ${uid}`); | ||
nodeproc._rawDebug(`${prefix}: uid: ${uid}`); | ||
} | ||
@@ -149,0 +183,0 @@ } |
38
index.js
"use strict"; | ||
var ContinuationLocalStorage_1 = require("./ContinuationLocalStorage"); | ||
exports.ContinuationLocalStorage = ContinuationLocalStorage_1.ContinuationLocalStorage; | ||
/* | ||
// tslint:disable | ||
const asyncHook = require('async-hook'); | ||
const node_process: any = process; | ||
asyncHook.addHooks({ | ||
init: (uid:number, handle: any, provider:number, parentUid: number|null, parentHandle:any) => { | ||
let fn = handle ? handle.constructor ? handle.constructor.name : handle.toString() : 'null'; | ||
node_process._rawDebug(`init : uid: ${uid} handle: ${fn}`); | ||
}, | ||
pre: (uid: number) => { | ||
node_process._rawDebug(`pre : uid: ${uid}`); | ||
}, | ||
post: (uid: number, didThrow: boolean) => { | ||
node_process._rawDebug(`post : uid: ${uid}`); | ||
}, | ||
destroy: (uid: number) => { | ||
node_process._rawDebug(`destroy: uid: ${uid}`); | ||
} | ||
}); | ||
asyncHook.enable(); | ||
// see | ||
// https://github.com/nodejs/node-eps/pull/18 | ||
// quote: Promise executors run synchronously, so only the 'then' will trigger events | ||
new Promise((resolve, reject)=>{ | ||
node_process._rawDebug('resolving promise'); | ||
resolve(42); | ||
}).then((val) => { | ||
node_process._rawDebug(`promise resolved ${val}`); | ||
asyncHook.disable(); | ||
return val; | ||
}); | ||
*/ | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "asyncctx", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "an asynchronous execution context for TypeScript/JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
281
0
8935