Skip to content

Commit 4d1a938

Browse files
committed
async_hooks: rename AsyncLocalStore#enterWith to set
The name enterWith indicates that there should be a matching exit but this is not the case. AsyncLocalStore#exit exists but it is not the opponent of enterWith. enterWith() is kept as alias and marked as doc only deprecated.
1 parent e080331 commit 4d1a938

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

benchmark/diagnostics_channel/http.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function patch() {
4242
function wrappedEmit(...args) {
4343
const [name, req, res] = args;
4444
if (name === 'request') {
45-
als.enterWith({
45+
als.set({
4646
url: req.url,
4747
start: process.hrtime.bigint()
4848
});
@@ -72,7 +72,7 @@ function diagnostics_channel() {
7272
const finish = dc.channel('http.server.response.finish');
7373

7474
function onStart(req) {
75-
als.enterWith({
75+
als.set({
7676
url: req.url,
7777
start: process.hrtime.bigint()
7878
});

doc/api/async_context.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ added:
125125
-->
126126

127127
Creates a new instance of `AsyncLocalStorage`. Store is only provided within a
128-
`run()` call or after an `enterWith()` call.
128+
`run()` call or after an `set()` call.
129129

130130
### `asyncLocalStorage.disable()`
131131

@@ -139,7 +139,7 @@ added:
139139
140140
Disables the instance of `AsyncLocalStorage`. All subsequent calls
141141
to `asyncLocalStorage.getStore()` will return `undefined` until
142-
`asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()` is called again.
142+
`asyncLocalStorage.run()` or `asyncLocalStorage.set()` is called again.
143143

144144
When calling `asyncLocalStorage.disable()`, all current contexts linked to the
145145
instance will be exited.
@@ -164,7 +164,7 @@ added:
164164

165165
Returns the current store.
166166
If called outside of an asynchronous context initialized by
167-
calling `asyncLocalStorage.run()` or `asyncLocalStorage.enterWith()`, it
167+
calling `asyncLocalStorage.run()` or `asyncLocalStorage.set()`, it
168168
returns `undefined`.
169169

170170
### `asyncLocalStorage.enterWith(store)`
@@ -173,13 +173,26 @@ returns `undefined`.
173173
added:
174174
- v13.11.0
175175
- v12.17.0
176+
deprecated:
177+
- REPLACEME
178+
-->
179+
180+
> Stability: 0 - Deprecated: Use [`asyncLocalStorage.set(store)`][]
181+
182+
This is a deprecated alias for [`asyncLocalStorage.set(store)`][].
183+
184+
### `asyncLocalStorage.set(store)`
185+
186+
<!-- YAML
187+
added:
188+
- REPLACEME
176189
-->
177190

178191
> Stability: 1 - Experimental
179192
180193
* `store` {any}
181194

182-
Transitions into the context for the remainder of the current
195+
Sets `store` on current execution context for the remainder of the current
183196
synchronous execution and then persists the store through any following
184197
asynchronous calls.
185198

@@ -188,7 +201,7 @@ Example:
188201
```js
189202
const store = { id: 1 };
190203
// Replaces previous store with the given store object
191-
asyncLocalStorage.enterWith(store);
204+
asyncLocalStorage.set(store);
192205
asyncLocalStorage.getStore(); // Returns the store object
193206
someAsyncOperation(() => {
194207
asyncLocalStorage.getStore(); // Returns the same object
@@ -199,14 +212,14 @@ This transition will continue for the _entire_ synchronous execution.
199212
This means that if, for example, the context is entered within an event
200213
handler subsequent event handlers will also run within that context unless
201214
specifically bound to another context with an `AsyncResource`. That is why
202-
`run()` should be preferred over `enterWith()` unless there are strong reasons
215+
`run()` should be preferred over `set()` unless there are strong reasons
203216
to use the latter method.
204217

205218
```js
206219
const store = { id: 1 };
207220

208221
emitter.on('my-event', () => {
209-
asyncLocalStorage.enterWith(store);
222+
asyncLocalStorage.set(store);
210223
});
211224
emitter.on('my-event', () => {
212225
asyncLocalStorage.getStore(); // Returns the same object
@@ -816,4 +829,5 @@ const server = createServer((req, res) => {
816829
[`EventEmitter`]: events.md#class-eventemitter
817830
[`Stream`]: stream.md#stream
818831
[`Worker`]: worker_threads.md#class-worker
832+
[`asyncLocalStorage.set(store)`]: #asynclocalstoragesetstore`
819833
[`util.promisify()`]: util.md#utilpromisifyoriginal

lib/async_hooks.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ class AsyncLocalStorage {
307307
}
308308
}
309309

310-
enterWith(store) {
310+
set(store) {
311311
this._enable();
312312
const resource = executionAsyncResource();
313313
resource[this.kResourceStore] = store;
@@ -353,6 +353,9 @@ class AsyncLocalStorage {
353353
}
354354
}
355355

356+
// Create deprecated enterWith alias
357+
AsyncLocalStorage.prototype.enterWith = AsyncLocalStorage.prototype.set;
358+
356359
// Placing all exports down here because the exported classes won't export
357360
// otherwise.
358361
module.exports = {

test/async-hooks/test-async-local-storage-enter-with.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ const { AsyncLocalStorage } = require('async_hooks');
55

66
const asyncLocalStorage = new AsyncLocalStorage();
77

8+
assert.strictEqual(AsyncLocalStorage.prototype.enterWith, AsyncLocalStorage.prototype.set);
9+
810
setImmediate(() => {
911
const store = { foo: 'bar' };
10-
asyncLocalStorage.enterWith(store);
12+
asyncLocalStorage.set(store);
1113

1214
assert.strictEqual(asyncLocalStorage.getStore(), store);
1315
setTimeout(() => {

test/parallel/test-diagnostics-channel-http-server-start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let context;
1111

1212
// Bind requests to an AsyncLocalStorage context
1313
dc.subscribe('http.server.request.start', common.mustCall((message) => {
14-
als.enterWith(message);
14+
als.set(message);
1515
context = message;
1616
}));
1717

0 commit comments

Comments
 (0)