File tree Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Expand file tree Collapse file tree 2 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ interface AsyncLocalStorage<T> {
11
11
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
12
12
const MaybeGlobalAsyncLocalStorage = ( GLOBAL_OBJ as any ) . AsyncLocalStorage ;
13
13
14
+ let asyncStorage : AsyncLocalStorage < Hub > ;
15
+
14
16
/**
15
17
* Sets the async context strategy to use AsyncLocalStorage which should be available in the edge runtime.
16
18
*/
@@ -23,7 +25,9 @@ export function setAsyncLocalStorageAsyncContextStrategy(): void {
23
25
return ;
24
26
}
25
27
26
- const asyncStorage : AsyncLocalStorage < Hub > = new MaybeGlobalAsyncLocalStorage ( ) ;
28
+ if ( ! asyncStorage ) {
29
+ asyncStorage = new MaybeGlobalAsyncLocalStorage ( ) ;
30
+ }
27
31
28
32
function getCurrentHub ( ) : Hub | undefined {
29
33
return asyncStorage . getStore ( ) ;
Original file line number Diff line number Diff line change @@ -12,11 +12,15 @@ type AsyncLocalStorageConstructor = { new <T>(): AsyncLocalStorage<T> };
12
12
// AsyncLocalStorage only exists in async_hook after Node v12.17.0 or v13.10.0
13
13
type NewerAsyncHooks = typeof async_hooks & { AsyncLocalStorage : AsyncLocalStorageConstructor } ;
14
14
15
+ let asyncStorage : AsyncLocalStorage < Hub > ;
16
+
15
17
/**
16
18
* Sets the async context strategy to use AsyncLocalStorage which requires Node v12.17.0 or v13.10.0.
17
19
*/
18
20
export function setHooksAsyncContextStrategy ( ) : void {
19
- const asyncStorage = new ( async_hooks as NewerAsyncHooks ) . AsyncLocalStorage < Hub > ( ) ;
21
+ if ( ! asyncStorage ) {
22
+ asyncStorage = new ( async_hooks as NewerAsyncHooks ) . AsyncLocalStorage < Hub > ( ) ;
23
+ }
20
24
21
25
function getCurrentHub ( ) : Hub | undefined {
22
26
return asyncStorage . getStore ( ) ;
You can’t perform that action at this time.
0 commit comments