diff options
Diffstat (limited to 'packages/opencode/src/util/local-context.ts')
| -rw-r--r-- | packages/opencode/src/util/local-context.ts | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/packages/opencode/src/util/local-context.ts b/packages/opencode/src/util/local-context.ts new file mode 100644 index 000000000..26f88ab09 --- /dev/null +++ b/packages/opencode/src/util/local-context.ts @@ -0,0 +1,25 @@ +import { AsyncLocalStorage } from "async_hooks" + +export namespace LocalContext { + export class NotFound extends Error { + constructor(public override readonly name: string) { + super(`No context found for ${name}`) + } + } + + export function create<T>(name: string) { + const storage = new AsyncLocalStorage<T>() + return { + use() { + const result = storage.getStore() + if (!result) { + throw new NotFound(name) + } + return result + }, + provide<R>(value: T, fn: () => R) { + return storage.run(value, fn) + }, + } + } +} |
