File tree Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Expand file tree Collapse file tree 2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change 33const {
44 NumberIsSafeInteger,
55 ObjectDefineProperties,
6+ ObjectIs,
67 ReflectApply,
78 Symbol,
89} = primordials ;
@@ -285,6 +286,10 @@ class AsyncLocalStorage {
285286 }
286287
287288 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+ }
288293 const resource = new AsyncResource ( 'AsyncLocalStorage' ) ;
289294 return resource . runInAsyncScope ( ( ) => {
290295 this . enterWith ( store ) ;
Original file line number Diff line number Diff line change @@ -10,8 +10,21 @@ const asyncLocalStorage = new AsyncLocalStorage();
1010
1111const outerResource = executionAsyncResource ( ) ;
1212
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+ } ) ;
1528} ) ;
1629
1730assert . strictEqual ( executionAsyncResource ( ) , outerResource ) ;
You can’t perform that action at this time.
0 commit comments