Created
September 24, 2020 02:39
-
-
Save assertnotnull/140b30f32f3f7931154f0147d97704ee to your computer and use it in GitHub Desktop.
reusable asyncstore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AsyncLocalStorage } from 'async_hooks'; | |
const context = new AsyncLocalStorage; | |
export function getStore() { | |
return context; | |
} | |
export function run(cb) { | |
context.run(new Map(), cb) | |
} | |
export function set(k, v): void { | |
context.getStore().set(k, v) | |
} | |
export function get(k): object { | |
return context.getStore().get(k) | |
} | |
export function runWithRequest(req, res, cb) { | |
run(() => { | |
set('req', req) | |
set('res', res) | |
cb() | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment