Conduitry
/
contexty
Archived
1
Fork 0

initial version of code

This commit is contained in:
Conduitry 2017-06-24 05:03:13 -04:00
parent f711c2e661
commit 5ddd43471b
1 changed files with 25 additions and 0 deletions

25
src/AsyncContexter.js Normal file
View File

@ -0,0 +1,25 @@
import { createHook, executionAsyncId } from 'async_hooks'
let asyncContexters = new Set()
createHook({
init(asyncId, type, triggerAsyncId) {
asyncContexters.forEach(asyncContexter => asyncContexter._contexts.set(asyncId, asyncContexter._contexts.get(triggerAsyncId)))
},
destroy(asyncId) {
asyncContexters.forEach(asyncContexter => asyncContexter._contexts.delete(asyncId))
},
}).enable()
export default class AsyncContexter {
constructor() {
this._contexts = new Map()
asyncContexters.add(this)
}
new() {
this._contexts.set(executionAsyncId(), Object.create(this._contexts.get(executionAsyncId()) || null))
}
get context() {
return this._contexts.get(executionAsyncId())
}
}