1+ import { process } from "./util.ts" ;
2+
13export type Defer = ( ) => void | Promise < void > ;
24
35export interface ComptimeContext {
@@ -14,24 +16,17 @@ export interface AsyncContext<T> {
1416 getStore ( ) : T | undefined ;
1517}
1618
17- let AsyncLocalStorage : new ( ) => AsyncContext < { __comptime_context : ComptimeContext } > ;
19+ export let asyncLocalStore : AsyncContext < { __comptime_context : ComptimeContext } > ;
1820
1921try {
20- // avoid an unnecessary import if we're not in a node-like environment
21- if ( typeof process === "undefined" ) throw new Error ( ) ;
22- AsyncLocalStorage = await import ( "node:async_hooks" ) . then ( m => m . AsyncLocalStorage ) ;
22+ // avoid an unnecessary import attempt if we're not in a node-like environment
23+ if ( ! process ) throw new Error ( ) ;
24+ const AsyncLocalStorage = ( await import ( "node:async_hooks" ) ) . AsyncLocalStorage ;
25+ asyncLocalStore = new AsyncLocalStorage ( ) ;
2326} catch {
24- // if we're not in a node-like environment, use a noop implementation, no need to error
25- class NoopAsyncLocalStorage < T > implements AsyncContext < T > {
26- run < R > ( _ : T , callback : ( ) => R ) : R {
27- return callback ( ) ;
28- }
29- getStore ( ) : T | undefined {
30- return undefined ;
31- }
32- }
33-
34- AsyncLocalStorage = NoopAsyncLocalStorage ;
27+ // noop AsyncLocalStorage implementation if we're not in a node-like environment
28+ asyncLocalStore = {
29+ run : ( _ , callback ) => callback ( ) ,
30+ getStore : ( ) => undefined ,
31+ } ;
3532}
36-
37- export const asyncLocalStore = AsyncLocalStorage && new AsyncLocalStorage ( ) ;
0 commit comments