| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0af9bee commit 0a51aa8
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | const { | |
| 4 | 4 | NumberIsSafeInteger, | |
| 5 | 5 | ObjectDefineProperties, | |
| 6 | + ObjectIs, | ||
| 6 | 7 | ReflectApply, | |
| 7 | 8 | Symbol, | |
| 8 | 9 | } = primordials; | |
@@ -288,6 +289,10 @@ class AsyncLocalStorage { | |||
| 288 | 289 | } | |
| 289 | 290 | ||
| 290 | 291 | run(store, callback, ...args) { | |
| 292 | + // Avoid creation of an AsyncResource if store is already active | ||
| 293 | + if (ObjectIs(store, this.getStore())) { | ||
| 294 | + return callback(...args); | ||
| 295 | + } | ||
| 291 | 296 | const resource = new AsyncResource('AsyncLocalStorage'); | |
| 292 | 297 | return resource.runInAsyncScope(() => { | |
| 293 | 298 | 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); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments