2022-11-14 19:00:20 +01:00
|
|
|
import { AsyncLocalStorage } from "async_hooks"
|
|
|
|
|
2022-11-17 14:31:54 +01:00
|
|
|
export default class Context {
|
2022-11-28 18:54:04 +01:00
|
|
|
static storage = new AsyncLocalStorage<Record<string, any>>()
|
2022-11-14 19:00:20 +01:00
|
|
|
|
2022-11-28 18:54:04 +01:00
|
|
|
static run(context: Record<string, any>, func: any) {
|
2022-11-14 19:00:20 +01:00
|
|
|
return Context.storage.run(context, () => func())
|
|
|
|
}
|
|
|
|
|
2022-11-28 18:54:04 +01:00
|
|
|
static get(): Record<string, any> {
|
|
|
|
return Context.storage.getStore() as Record<string, any>
|
2022-11-14 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 18:54:04 +01:00
|
|
|
static set(context: Record<string, any>) {
|
2022-11-14 19:00:20 +01:00
|
|
|
Context.storage.enterWith(context)
|
|
|
|
}
|
|
|
|
}
|