| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3497370 commit f71fc90
14 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -106,7 +106,7 @@ function buildDestroy(getServe) { | |||
| 106 | 106 | function buildAsyncLocalStorage(getServe) { | |
| 107 | 107 | const asyncLocalStorage = new AsyncLocalStorage(); | |
| 108 | 108 | const server = createServer((req, res) => { | |
| 109 | - asyncLocalStorage.runSyncAndReturn(() => { | ||
| 109 | + asyncLocalStorage.runSyncAndReturn({}, () => { | ||
| 110 | 110 | getServe(getCLS, setCLS)(req, res); | |
| 111 | 111 | }); | |
| 112 | 112 | }); | |
@@ -118,12 +118,12 @@ function buildAsyncLocalStorage(getServe) { | |||
| 118 | 118 | ||
| 119 | 119 | function getCLS() { | |
| 120 | 120 | const store = asyncLocalStorage.getStore(); | |
| 121 | - return store.get('store'); | ||
| 121 | + return store.state; | ||
| 122 | 122 | } | |
| 123 | 123 | ||
| 124 | 124 | function setCLS(state) { | |
| 125 | 125 | const store = asyncLocalStorage.getStore(); | |
| 126 | - store.set('store', state); | ||
| 126 | + store.state = state; | ||
| 127 | 127 | } | |
| 128 | 128 | ||
| 129 | 129 | function close() { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -893,7 +893,7 @@ function log(...args) { | |||
| 893 | 893 | } | |
| 894 | 894 | ||
| 895 | 895 | http.createServer((request, response) => { | |
| 896 | - asyncLocalStorage.run(() => { | ||
| 896 | + asyncLocalStorage.run(new Map(), () => { | ||
| 897 | 897 | const store = asyncLocalStorage.getStore(); | |
| 898 | 898 | store.set(kReq, request); | |
| 899 | 899 | someAsyncOperation((err, result) => { | |
@@ -943,27 +943,27 @@ in the current process. | |||
| 943 | 943 | added: REPLACEME | |
| 944 | 944 | --> | |
| 945 | 945 | ||
| 946 | - * Returns: {Map} | ||
| 946 | + * Returns: {any} | ||
| 947 | 947 | ||
| 948 | 948 | This method returns the current store. | |
| 949 | 949 | 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.run(callback[, ...args])` | ||
| 953 | + ### `asyncLocalStorage.run(store, callback[, ...args])` | ||
| 954 | 954 | <!-- YAML | |
| 955 | 955 | added: REPLACEME | |
| 956 | 956 | --> | |
| 957 | 957 | ||
| 958 | + * `store` {any} | ||
| 958 | 959 | * `callback` {Function} | |
| 959 | 960 | * `...args` {any} | |
| 960 | 961 | ||
| 961 | 962 | Calling `asyncLocalStorage.run(callback)` will create a new asynchronous | |
| 962 | - context. | ||
| 963 | - Within the callback function and the asynchronous operations from the callback, | ||
| 964 | - `asyncLocalStorage.getStore()` will return an instance of `Map` known as | ||
| 965 | - "the store". This store will be persistent through the following | ||
| 966 | - asynchronous calls. | ||
| 963 | + context. Within the callback function and the asynchronous operations from | ||
| 964 | + the callback, `asyncLocalStorage.getStore()` will return the object or | ||
| 965 | + the primitive value passed into the `store` argument (known as "the store"). | ||
| 966 | + This store will be persistent through the following asynchronous calls. | ||
| 967 | 967 | ||
| 968 | 968 | The callback will be ran asynchronously. Optionally, arguments can be passed | |
| 969 | 969 | to the function. They will be passed to the callback function. | |
@@ -975,10 +975,11 @@ Also, the stacktrace will be impacted by the asynchronous call. | |||
| 975 | 975 | Example: | |
| 976 | 976 | ||
| 977 | 977 | ```js | |
| 978 | - asyncLocalStorage.run(() => { | ||
| 979 | - asyncLocalStorage.getStore(); // Returns a Map | ||
| 978 | + const store = { id: 1 }; | ||
| 979 | + asyncLocalStorage.run(store, () => { | ||
| 980 | + asyncLocalStorage.getStore(); // Returns the store object | ||
| 980 | 981 | someAsyncOperation(() => { | |
| 981 | - asyncLocalStorage.getStore(); // Returns the same Map | ||
| 982 | + asyncLocalStorage.getStore(); // Returns the same object | ||
| 982 | 983 | }); | |
| 983 | 984 | }); | |
| 984 | 985 | asyncLocalStorage.getStore(); // Returns undefined | |
@@ -1007,20 +1008,21 @@ Also, the stacktrace will be impacted by the asynchronous call. | |||
| 1007 | 1008 | Example: | |
| 1008 | 1009 | ||
| 1009 | 1010 | ```js | |
| 1010 | - asyncLocalStorage.run(() => { | ||
| 1011 | - asyncLocalStorage.getStore(); // Returns a Map | ||
| 1011 | + asyncLocalStorage.run('store value', () => { | ||
| 1012 | + asyncLocalStorage.getStore(); // Returns 'store value' | ||
| 1012 | 1013 | asyncLocalStorage.exit(() => { | |
| 1013 | 1014 | asyncLocalStorage.getStore(); // Returns undefined | |
| 1014 | 1015 | }); | |
| 1015 | - asyncLocalStorage.getStore(); // Returns the same Map | ||
| 1016 | + asyncLocalStorage.getStore(); // Returns 'store value' | ||
| 1016 | 1017 | }); | |
| 1017 | 1018 | ``` | |
| 1018 | 1019 | ||
| 1019 | - ### `asyncLocalStorage.runSyncAndReturn(callback[, ...args])` | ||
| 1020 | + ### `asyncLocalStorage.runSyncAndReturn(store, callback[, ...args])` | ||
| 1020 | 1021 | <!-- YAML | |
| 1021 | 1022 | added: REPLACEME | |
| 1022 | 1023 | --> | |
| 1023 | 1024 | ||
| 1025 | + * `store` {any} | ||
| 1024 | 1026 | * `callback` {Function} | |
| 1025 | 1027 | * `...args` {any} | |
| 1026 | 1028 | ||
@@ -1038,9 +1040,10 @@ the context will be exited. | |||
| 1038 | 1040 | Example: | |
| 1039 | 1041 | ||
| 1040 | 1042 | ```js | |
| 1043 | + const store = { id: 2 }; | ||
| 1041 | 1044 | try { | |
| 1042 | - asyncLocalStorage.runSyncAndReturn(() => { | ||
| 1043 | - asyncLocalStorage.getStore(); // Returns a Map | ||
| 1045 | + asyncLocalStorage.runSyncAndReturn(store, () => { | ||
| 1046 | + asyncLocalStorage.getStore(); // Returns the store object | ||
| 1044 | 1047 | throw new Error(); | |
| 1045 | 1048 | }); | |
| 1046 | 1049 | } catch (e) { | |
@@ -1073,13 +1076,13 @@ Example: | |||
| 1073 | 1076 | ```js | |
| 1074 | 1077 | // Within a call to run or runSyncAndReturn | |
| 1075 | 1078 | try { | |
| 1076 | - asyncLocalStorage.getStore(); // Returns a Map | ||
| 1079 | + asyncLocalStorage.getStore(); // Returns the store object or value | ||
| 1077 | 1080 | asyncLocalStorage.exitSyncAndReturn(() => { | |
| 1078 | 1081 | asyncLocalStorage.getStore(); // Returns undefined | |
| 1079 | 1082 | throw new Error(); | |
| 1080 | 1083 | }); | |
| 1081 | 1084 | } catch (e) { | |
| 1082 | - asyncLocalStorage.getStore(); // Returns the same Map | ||
| 1085 | + asyncLocalStorage.getStore(); // Returns the same object or value | ||
| 1083 | 1086 | // The error will be caught here | |
| 1084 | 1087 | } | |
| 1085 | 1088 | ``` | |
@@ -1105,8 +1108,9 @@ It cannot be promisified using `util.promisify`. If needed, the `Promise` | |||
| 1105 | 1108 | constructor can be used: | |
| 1106 | 1109 | ||
| 1107 | 1110 | ```js | |
| 1111 | + const store = new Map(); // initialize the store | ||
| 1108 | 1112 | new Promise((resolve, reject) => { | |
| 1109 | - asyncLocalStorage.run(() => { | ||
| 1113 | + asyncLocalStorage.run(store, () => { | ||
| 1110 | 1114 | someFunction((err, result) => { | |
| 1111 | 1115 | if (err) { | |
| 1112 | 1116 | return reject(err); | |
@@ -1135,7 +1139,7 @@ the following pattern should be used: | |||
| 1135 | 1139 | ||
| 1136 | 1140 | ```js | |
| 1137 | 1141 | async function fn() { | |
| 1138 | - await asyncLocalStorage.runSyncAndReturn(() => { | ||
| 1142 | + await asyncLocalStorage.runSyncAndReturn(new Map(), () => { | ||
| 1139 | 1143 | asyncLocalStorage.getStore().set('key', value); | |
| 1140 | 1144 | return foo(); // The return value of foo will be awaited | |
| 1141 | 1145 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,11 +1,9 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - Map, | ||
| 5 | 4 | NumberIsSafeInteger, | |
| 6 | 5 | ReflectApply, | |
| 7 | 6 | Symbol, | |
| 8 | - | ||
| 9 | 7 | } = primordials; | |
| 10 | 8 | ||
| 11 | 9 | const { | |
@@ -247,14 +245,14 @@ class AsyncLocalStorage { | |||
| 247 | 245 | } | |
| 248 | 246 | } | |
| 249 | 247 | ||
| 250 | - _enter() { | ||
| 248 | + _enter(store) { | ||
| 251 | 249 | if (!this.enabled) { | |
| 252 | 250 | this.enabled = true; | |
| 253 | 251 | storageList.push(this); | |
| 254 | 252 | storageHook.enable(); | |
| 255 | 253 | } | |
| 256 | 254 | const resource = executionAsyncResource(); | |
| 257 | - resource[this.kResourceStore] = new Map(); | ||
| 255 | + resource[this.kResourceStore] = store; | ||
| 258 | 256 | } | |
| 259 | 257 | ||
| 260 | 258 | _exit() { | |
@@ -264,8 +262,8 @@ class AsyncLocalStorage { | |||
| 264 | 262 | } | |
| 265 | 263 | } | |
| 266 | 264 | ||
| 267 | - runSyncAndReturn(callback, ...args) { | ||
| 268 | - this._enter(); | ||
| 265 | + runSyncAndReturn(store, callback, ...args) { | ||
| 266 | + this._enter(store); | ||
| 269 | 267 | try { | |
| 270 | 268 | return callback(...args); | |
| 271 | 269 | } finally { | |
@@ -289,8 +287,8 @@ class AsyncLocalStorage { | |||
| 289 | 287 | } | |
| 290 | 288 | } | |
| 291 | 289 | ||
| 292 | - run(callback, ...args) { | ||
| 293 | - this._enter(); | ||
| 290 | + run(store, callback, ...args) { | ||
| 291 | + this._enter(store); | ||
| 294 | 292 | process.nextTick(callback, ...args); | |
| 295 | 293 | this._exit(); | |
| 296 | 294 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,14 +5,14 @@ const { AsyncLocalStorage } = require('async_hooks'); | |||
| 5 | 5 | ||
| 6 | 6 | const asyncLocalStorage = new AsyncLocalStorage(); | |
| 7 | 7 | ||
| 8 | - asyncLocalStorage.run((runArg) => { | ||
| 8 | + asyncLocalStorage.run({}, (runArg) => { | ||
| 9 | 9 | assert.strictEqual(runArg, 1); | |
| 10 | 10 | asyncLocalStorage.exit((exitArg) => { | |
| 11 | 11 | assert.strictEqual(exitArg, 2); | |
| 12 | 12 | }, 2); | |
| 13 | 13 | }, 1); | |
| 14 | 14 | ||
| 15 | - asyncLocalStorage.runSyncAndReturn((runArg) => { | ||
| 15 | + asyncLocalStorage.runSyncAndReturn({}, (runArg) => { | ||
| 16 | 16 | assert.strictEqual(runArg, 'foo'); | |
| 17 | 17 | asyncLocalStorage.exitSyncAndReturn((exitArg) => { | |
| 18 | 18 | assert.strictEqual(exitArg, 'bar'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ async function test() { | |||
| 12 | 12 | } | |
| 13 | 13 | ||
| 14 | 14 | async function main() { | |
| 15 | - await asyncLocalStorage.runSyncAndReturn(test); | ||
| 15 | + await asyncLocalStorage.runSyncAndReturn(new Map(), test); | ||
| 16 | 16 | assert.strictEqual(asyncLocalStorage.getStore(), undefined); | |
| 17 | 17 | } | |
| 18 | 18 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ async function testAwait() { | |||
| 19 | 19 | await asyncLocalStorage.exitSyncAndReturn(testOut); | |
| 20 | 20 | } | |
| 21 | 21 | ||
| 22 | - asyncLocalStorage.run(() => { | ||
| 22 | + asyncLocalStorage.run(new Map(), () => { | ||
| 23 | 23 | const store = asyncLocalStorage.getStore(); | |
| 24 | 24 | store.set('key', 'value'); | |
| 25 | 25 | testAwait(); // should not reject | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,15 +5,15 @@ const { AsyncLocalStorage } = require('async_hooks'); | |||
| 5 | 5 | ||
| 6 | 6 | const asyncLocalStorage = new AsyncLocalStorage(); | |
| 7 | 7 | ||
| 8 | - asyncLocalStorage.runSyncAndReturn(() => { | ||
| 8 | + asyncLocalStorage.runSyncAndReturn(new Map(), () => { | ||
| 9 | 9 | asyncLocalStorage.getStore().set('foo', 'bar'); | |
| 10 | 10 | process.nextTick(() => { | |
| 11 | 11 | assert.strictEqual(asyncLocalStorage.getStore().get('foo'), 'bar'); | |
| 12 | 12 | asyncLocalStorage.disable(); | |
| 13 | 13 | assert.strictEqual(asyncLocalStorage.getStore(), undefined); | |
| 14 | 14 | process.nextTick(() => { | |
| 15 | 15 | assert.strictEqual(asyncLocalStorage.getStore(), undefined); | |
| 16 | - asyncLocalStorage.runSyncAndReturn(() => { | ||
| 16 | + asyncLocalStorage.runSyncAndReturn(new Map(), () => { | ||
| 17 | 17 | assert.notStrictEqual(asyncLocalStorage.getStore(), undefined); | |
| 18 | 18 | }); | |
| 19 | 19 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,7 +13,7 @@ process.setUncaughtExceptionCaptureCallback((err) => { | |||
| 13 | 13 | assert.strictEqual(asyncLocalStorage.getStore().get('hello'), 'node'); | |
| 14 | 14 | }); | |
| 15 | 15 | ||
| 16 | - asyncLocalStorage.run(() => { | ||
| 16 | + asyncLocalStorage.run(new Map(), () => { | ||
| 17 | 17 | const store = asyncLocalStorage.getStore(); | |
| 18 | 18 | store.set('hello', 'node'); | |
| 19 | 19 | setTimeout(() => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,7 +14,7 @@ process.setUncaughtExceptionCaptureCallback((err) => { | |||
| 14 | 14 | }); | |
| 15 | 15 | ||
| 16 | 16 | try { | |
| 17 | - asyncLocalStorage.runSyncAndReturn(() => { | ||
| 17 | + asyncLocalStorage.runSyncAndReturn(new Map(), () => { | ||
| 18 | 18 | const store = asyncLocalStorage.getStore(); | |
| 19 | 19 | store.set('hello', 'node'); | |
| 20 | 20 | setTimeout(() => { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ const server = http.createServer((req, res) => { | |||
| 10 | 10 | }); | |
| 11 | 11 | ||
| 12 | 12 | server.listen(0, () => { | |
| 13 | - asyncLocalStorage.run(() => { | ||
| 13 | + asyncLocalStorage.run(new Map(), () => { | ||
| 14 | 14 | const store = asyncLocalStorage.getStore(); | |
| 15 | 15 | store.set('hello', 'world'); | |
| 16 | 16 | http.get({ host: 'localhost', port: server.address().port }, () => { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments