File tree 2 files changed +20
-2
lines changed
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 3
3
const {
4
4
NumberIsSafeInteger,
5
5
ObjectDefineProperties,
6
+ ObjectIs,
6
7
ReflectApply,
7
8
Symbol,
8
9
} = primordials ;
@@ -285,6 +286,10 @@ class AsyncLocalStorage {
285
286
}
286
287
287
288
run ( store , callback , ...args ) {
289
+ // Avoid creation of an AsyncResource if store is already active
290
+ if ( ObjectIs ( store , this . getStore ( ) ) ) {
291
+ return callback ( ...args ) ;
292
+ }
288
293
const resource = new AsyncResource ( 'AsyncLocalStorage' ) ;
289
294
return resource . runInAsyncScope ( ( ) => {
290
295
this . enterWith ( store ) ;
Original file line number Diff line number Diff line change @@ -10,8 +10,21 @@ const asyncLocalStorage = new AsyncLocalStorage();
10
10
11
11
const outerResource = executionAsyncResource ( ) ;
12
12
13
- asyncLocalStorage . run ( new Map ( ) , ( ) => {
14
- assert . notStrictEqual ( executionAsyncResource ( ) , outerResource ) ;
13
+ const store = new Map ( ) ;
14
+ asyncLocalStorage . run ( store , ( ) => {
15
+ assert . strictEqual ( asyncLocalStorage . getStore ( ) , store ) ;
16
+ const innerResource = executionAsyncResource ( ) ;
17
+ assert . notStrictEqual ( innerResource , outerResource ) ;
18
+ asyncLocalStorage . run ( store , ( ) => {
19
+ assert . strictEqual ( asyncLocalStorage . getStore ( ) , store ) ;
20
+ assert . strictEqual ( executionAsyncResource ( ) , innerResource ) ;
21
+ const otherStore = new Map ( ) ;
22
+ asyncLocalStorage . run ( otherStore , ( ) => {
23
+ assert . strictEqual ( asyncLocalStorage . getStore ( ) , otherStore ) ;
24
+ assert . notStrictEqual ( executionAsyncResource ( ) , innerResource ) ;
25
+ assert . notStrictEqual ( executionAsyncResource ( ) , outerResource ) ;
26
+ } ) ;
27
+ } ) ;
15
28
} ) ;
16
29
17
30
assert . strictEqual ( executionAsyncResource ( ) , outerResource ) ;
You can’t perform that action at this time.
0 commit comments