cloudaxe-opencode/packages/console/core/src/context.ts

22 lines
467 B
TypeScript
Raw Permalink Normal View History

2025-08-08 17:22:54 +00:00
import { AsyncLocalStorage } from "node:async_hooks"
export namespace Context {
export class NotFound extends Error {}
export function create<T>() {
const storage = new AsyncLocalStorage<T>()
return {
use() {
const result = storage.getStore()
if (!result) {
throw new NotFound()
}
return result
},
provide<R>(value: T, fn: () => R) {
2025-10-10 22:17:10 +00:00
return storage.run(value, fn)
2025-08-08 17:22:54 +00:00
},
}
}
}