| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 02ebc81 commit 4d5981b
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -950,6 +950,48 @@ If this method is called outside of an asynchronous context initialized by | |||
| 950 | 950 | calling `asyncLocalStorage.run` or `asyncLocalStorage.runAndReturn`, it will | |
| 951 | 951 | return `undefined`. | |
| 952 | 952 | ||
| 953 | + ### `asyncLocalStorage.enterWith(store)` | ||
| 954 | + <!-- YAML | ||
| 955 | + added: REPLACEME | ||
| 956 | + --> | ||
| 957 | + | ||
| 958 | + * `store` {any} | ||
| 959 | + | ||
| 960 | + Calling `asyncLocalStorage.enterWith(store)` will transition into the context | ||
| 961 | + for the remainder of the current synchronous execution and will persist | ||
| 962 | + through any following asynchronous calls. | ||
| 963 | + | ||
| 964 | + Example: | ||
| 965 | + | ||
| 966 | + ```js | ||
| 967 | + const store = { id: 1 }; | ||
| 968 | + asyncLocalStorage.enterWith(store); | ||
| 969 | + asyncLocalStorage.getStore(); // Returns the store object | ||
| 970 | + someAsyncOperation(() => { | ||
| 971 | + asyncLocalStorage.getStore(); // Returns the same object | ||
| 972 | + }); | ||
| 973 | + ``` | ||
| 974 | + | ||
| 975 | + This transition will continue for the _entire_ synchronous execution. | ||
| 976 | + This means that if, for example, the context is entered within an event | ||
| 977 | + handler subsequent event handlers will also run within that context unless | ||
| 978 | + specifically bound to another context with an `AsyncResource`. | ||
| 979 | + | ||
| 980 | + ```js | ||
| 981 | + const store = { id: 1 }; | ||
| 982 | + | ||
| 983 | + emitter.on('my-event', () => { | ||
| 984 | + asyncLocalStorage.enterWith(store); | ||
| 985 | + }); | ||
| 986 | + emitter.on('my-event', () => { | ||
| 987 | + asyncLocalStorage.getStore(); // Returns the same object | ||
| 988 | + }); | ||
| 989 | + | ||
| 990 | + asyncLocalStorage.getStore(); // Returns undefined | ||
| 991 | + emitter.emit('my-event'); | ||
| 992 | + asyncLocalStorage.getStore(); // Returns the same object | ||
| 993 | + ``` | ||
| 994 | + | ||
| 953 | 995 | ### `asyncLocalStorage.run(store, callback[, ...args])` | |
| 954 | 996 | <!-- YAML | |
| 955 | 997 | added: v13.10.0 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -245,7 +245,7 @@ class AsyncLocalStorage { | |||
| 245 | 245 | } | |
| 246 | 246 | } | |
| 247 | 247 | ||
| 248 | - _enter(store) { | ||
| 248 | + enterWith(store) { | ||
| 249 | 249 | if (!this.enabled) { | |
| 250 | 250 | this.enabled = true; | |
| 251 | 251 | storageList.push(this); | |
@@ -258,7 +258,7 @@ class AsyncLocalStorage { | |||
| 258 | 258 | runSyncAndReturn(store, callback, ...args) { | |
| 259 | 259 | const resource = executionAsyncResource(); | |
| 260 | 260 | const outerStore = resource[this.kResourceStore]; | |
| 261 | - this._enter(store); | ||
| 261 | + this.enterWith(store); | ||
| 262 | 262 | try { | |
| 263 | 263 | return callback(...args); | |
| 264 | 264 | } finally { | |
@@ -288,7 +288,7 @@ class AsyncLocalStorage { | |||
| 288 | 288 | run(store, callback, ...args) { | |
| 289 | 289 | const resource = executionAsyncResource(); | |
| 290 | 290 | const outerStore = resource[this.kResourceStore]; | |
| 291 | - this._enter(store); | ||
| 291 | + this.enterWith(store); | ||
| 292 | 292 | process.nextTick(callback, ...args); | |
| 293 | 293 | resource[this.kResourceStore] = outerStore; | |
| 294 | 294 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,20 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { AsyncLocalStorage } = require('async_hooks'); | ||
| 5 | + | ||
| 6 | + const asyncLocalStorage = new AsyncLocalStorage(); | ||
| 7 | + | ||
| 8 | + setImmediate(() => { | ||
| 9 | + const store = { foo: 'bar' }; | ||
| 10 | + asyncLocalStorage.enterWith(store); | ||
| 11 | + | ||
| 12 | + assert.strictEqual(asyncLocalStorage.getStore(), store); | ||
| 13 | + setTimeout(() => { | ||
| 14 | + assert.strictEqual(asyncLocalStorage.getStore(), store); | ||
| 15 | + }, 10); | ||
| 16 | + }); | ||
| 17 | + | ||
| 18 | + setTimeout(() => { | ||
| 19 | + assert.strictEqual(asyncLocalStorage.getStore(), undefined); | ||
| 20 | + }, 10); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments