15 lines
405 B
TypeScript
15 lines
405 B
TypeScript
import { AsyncLocalStorage } from "async_hooks"
|
|
|
|
export type State = {
|
|
id?: string
|
|
session?: string
|
|
project?: string
|
|
}
|
|
|
|
// Ensure "ALS" lives between bun's hot reloads
|
|
const g = globalThis as typeof globalThis & { __thread?: AsyncLocalStorage<State> }
|
|
export const ALS = g.__thread ??= new AsyncLocalStorage<State>()
|
|
|
|
export function getState(): State | undefined {
|
|
return ALS.getStore()
|
|
} |