2022-11-14 19:00:20 +01:00
|
|
|
import { AsyncLocalStorage } from "async_hooks"
|
2023-02-24 18:31:58 +01:00
|
|
|
import { ContextMap } from "./types"
|
2022-11-14 19:00:20 +01:00
|
|
|
|
2022-11-17 14:31:54 +01:00
|
|
|
export default class Context {
|
2023-01-13 20:53:46 +01:00
|
|
|
static storage = new AsyncLocalStorage<ContextMap>()
|
2022-11-14 19:00:20 +01:00
|
|
|
|
2023-11-08 17:17:24 +01:00
|
|
|
static run<T>(context: ContextMap, func: () => T) {
|
2022-11-14 19:00:20 +01:00
|
|
|
return Context.storage.run(context, () => func())
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:53:46 +01:00
|
|
|
static get(): ContextMap {
|
|
|
|
return Context.storage.getStore() as ContextMap
|
2022-11-14 19:00:20 +01:00
|
|
|
}
|
|
|
|
}
|