@@ -83,9 +83,13 @@ more detail further down.
83
83
const async_hooks = require (' async_hooks' );
84
84
85
85
// Return the id of the current execution context. Useful for tracking state
86
- // and retrieving the handle of the current parent without needing to use an
86
+ // and retrieving the handle of the current trigger without needing to use an
87
87
// AsyncHook.
88
- const id = async_hooks .currentId ();
88
+ const cid = async_hooks .currentId ();
89
+
90
+ // Return the id of the handle responsible for triggering the callback of the
91
+ // current execution scope to fire.
92
+ const tid = aysnc_hooks .triggerId ();
89
93
90
94
// Create a new instance of AsyncHook. All of these callbacks are optional.
91
95
const asyncHook = async_hooks .createHook ({ init, before, after, destroy });
@@ -105,7 +109,7 @@ asyncHook.disable();
105
109
// init() is called during object construction. The handle may not have
106
110
// completed construction when this callback runs. So all fields of the
107
111
// handle referenced by "id" may not have been populated.
108
- function init (id , type , parentId , handle ) { }
112
+ function init (id , type , triggerId , handle ) { }
109
113
110
114
// before() is called just before the handle's callback is called. It can be
111
115
// called 0-N times for handles (e.g. TCPWrap), and should be called exactly 1
@@ -133,7 +137,7 @@ async_hooks.setCurrentId(id);
133
137
134
138
// Call the init() callbacks. Returns an instance of AsyncEvent that will be
135
139
// used in other emit calls.
136
- async_hooks .emitInit (id, handle, type[, parentId ]);
140
+ async_hooks .emitInit (id, handle, type[, triggerId ]);
137
141
138
142
// Call the before() callbacks. The reason for requiring both arguments is
139
143
// explained in further detail below.
@@ -155,8 +159,26 @@ The object returned from `require('async_hooks')`.
155
159
#### ` async_hooks.currentId() `
156
160
157
161
Return the id of the current execution context. Useful to track the
158
- asynchronous execution chain. It can also be used to propagate state without
159
- needing to use the ` AsyncHook ` constructor.
162
+ asynchronous execution chain.
163
+
164
+
165
+ #### ` async_hooks.triggerId() `
166
+
167
+ Return the id of the handle responsible for calling the callback of the current
168
+ execution scope. For example:
169
+
170
+ ``` js
171
+ const server = net .createServer (conn => {
172
+ // currentId() returns the id of the handle that's executing
173
+ // the callback. In this case it's the id of "server".
174
+ async_hooks .currentId ();
175
+
176
+ // Though the handle that caused (or triggered) this callback to
177
+ // be called was that of the new connection. Thus the return value
178
+ // of triggerId() is the id of "conn".
179
+ async_hooks .triggerId ();
180
+ }).listen ();
181
+ ```
160
182
161
183
162
184
#### ` async_hooks.createHook(callbacks) `
@@ -241,11 +263,11 @@ instance is destructed. For cases where resources are reused, instantiation and
241
263
destructor calls are emulated.
242
264
243
265
244
- #### ` init(id, type, parentId , handle) `
266
+ #### ` init(id, type, triggerId , handle) `
245
267
246
268
* ` id ` {Number}
247
269
* ` type ` {String}
248
- * ` parentId ` {Number}
270
+ * ` triggerId ` {Number}
249
271
* ` handle ` {Object}
250
272
251
273
Called when a class is constructed that has the possibility to trigger an
@@ -276,8 +298,8 @@ examples include `TCP`, `GetAddrInfo` and `HTTPParser`. Users will be able to
276
298
define their own ` type ` when using the public embedder API.
277
299
278
300
When ` init() ` is called the id that the resource was initialized in can be
279
- found with ` currentId() ` . ` parentId ` is meant to communicate the "conceptual"
280
- parent of the new handle. In other words the reason for the handle's existence .
301
+ found with ` currentId() ` . ` triggerId ` is the id of the resource responsible for
302
+ ` init() ` being called .
281
303
282
304
The following is a simple demonstration of this:
283
305
@@ -286,9 +308,9 @@ const async_hooks = require('async_hooks');
286
308
const net = require(' net' );
287
309
288
310
asyn_hooks.createHook({
289
- init: (id, type, parentId ) => {
311
+ init: (id, type, triggerId ) => {
290
312
const cId = async_hooks.currentId();
291
- process._ rawDebug(` ${type}(${id}): parent : ${parentId } scope: ${cId} ` );
313
+ process._ rawDebug(` ${type}(${id}): trigger : ${triggerId } scope: ${cId} ` );
292
314
}
293
315
}).enable();
294
316
@@ -298,16 +320,16 @@ net.createServer(c => {}).listen(8080);
298
320
Output hitting the server with ` nc localhost 8080 ` :
299
321
300
322
```
301
- TCPWRAP(2): parent : 1 scope: 1
302
- TCPWRAP(4): parent : 2 scope: 0
323
+ TCPWRAP(2): trigger : 1 scope: 1
324
+ TCPWRAP(4): trigger : 2 scope: 0
303
325
```
304
326
305
327
The second ` TCPWRAP ` is the new connection from the client. When a new
306
328
connection is made the ` TCPWrap ` instance is immediately constructed. This
307
329
happens outside of any JavaScript stack (side note: a ` currentId() ` of ` 0 `
308
330
means it's being executed in the void, with no JavaScript stack above it). With
309
331
only that information it would be impossible to link resources together in
310
- terms of what caused them to be created. So ` parentId ` is given the task of
332
+ terms of what caused them to be created. So ` triggerId ` is given the task of
311
333
propagating what other handle is responsible for the new resource's existence.
312
334
313
335
Below is another example with additional information about the calls to
@@ -318,10 +340,10 @@ elaborate to make calling context easier to see.
318
340
``` js
319
341
let ws = 0 ;
320
342
async_hooks .createHook ({
321
- init : (id , type , parentId ) => {
343
+ init : (id , type , triggerId ) => {
322
344
const cId = async_hooks .currentId ();
323
345
process ._rawDebug (' ' .repeat (ws) +
324
- ` ${ type} (${ id} ): parent : ${ parentId } scope: ${ cId} ` );
346
+ ` ${ type} (${ id} ): trigger : ${ triggerId } scope: ${ cId} ` );
325
347
},
326
348
before : (id ) => {
327
349
print (' ' .repeat (ws) + ' before: ' , id);
@@ -344,17 +366,17 @@ net.createServer(() => {}).listen(8080, () => {
344
366
Output from only starting the server:
345
367
346
368
```
347
- TCPWRAP(2): parent : 1 scope: 1
369
+ TCPWRAP(2): trigger : 1 scope: 1
348
370
# .listen()
349
- TickObject(3): parent : 1 scope: 1
371
+ TickObject(3): trigger : 1 scope: 1
350
372
before: 3
351
- Timeout(4): parent : 3 scope: 3
352
- TIMERWRAP(5): parent : 3 scope: 3
373
+ Timeout(4): trigger : 3 scope: 3
374
+ TIMERWRAP(5): trigger : 3 scope: 3
353
375
after: 3
354
376
before: 5
355
377
before: 4
356
- TTYWRAP(6): parent : 4 scope: 4
357
- SIGNALWRAP(7): parent : 4 scope: 4
378
+ TTYWRAP(6): trigger : 4 scope: 4
379
+ SIGNALWRAP(7): trigger : 4 scope: 4
358
380
>>> 4
359
381
after: 4
360
382
after: 5
@@ -376,7 +398,7 @@ completely asynchronous API the user's callback is placed in a
376
398
` process.nextTick() ` .
377
399
378
400
The graph only shows ** when** a resource was created. Not ** why** . The later is
379
- what ` parentId ` is meant to communicate.
401
+ what ` triggerId ` is meant to communicate.
380
402
381
403
382
404
#### ` before(id) `
@@ -477,7 +499,7 @@ MaybeLocal<Value> node::MakeCallback(Isolate* isolate,
477
499
There is also a JS API to allow for conceptual correctness. For example, if a
478
500
request batches calls under the hood this would still allow for each request
479
501
to trigger the callbacks individually so that the callback associated with
480
- each bach request execute inside the correct parent .
502
+ each bach request execute inside the correct trigger .
481
503
482
504
This cannot be enforced in any way. It is up to the implementer to make sure
483
505
all of the callbacks are placed and called at the correct time.
@@ -501,12 +523,12 @@ class MyClass {
501
523
```
502
524
503
525
504
- #### ` async_hooks.emitInit(id, handle, type[, parentId ]) `
526
+ #### ` async_hooks.emitInit(id, handle, type[, triggerId ]) `
505
527
506
528
* ` id ` {Number} Generated by calling ` newUid() `
507
529
* ` handle ` {Object}
508
530
* ` type ` {String}
509
- * ` parentId ` {Number} ** Default:** ` currentId() `
531
+ * ` triggerId ` {Number} ** Default:** ` currentId() `
510
532
* Return: {Undefined}
511
533
512
534
Emit that a handle is being initialized. ` id ` should be a value returned by
@@ -521,8 +543,8 @@ class Foo {
521
543
}
522
544
```
523
545
524
- In the unusual circumstance that the embedder needs to define a different
525
- parent id than ` currentId() ` , they can pass in that id manually.
546
+ In the circumstance that the embedder needs to define a different trigger id
547
+ than ` currentId() ` , they can pass in that id manually.
526
548
527
549
It is suggested to have ` emitInit() ` be the last call in the object's
528
550
constructor.
@@ -546,7 +568,7 @@ MyThing.prototype.done = function done() {
546
568
// handle wrapping the id that's been passed.
547
569
async_hooks .emitBefore (this .asyncId );
548
570
549
- // Store the current parent id then set the global current id to the id
571
+ // Store the current trigger id then set the global current id to the id
550
572
// that was assigned when this object was instantiated.
551
573
const previous_id = async_hooks .currentId ();
552
574
async_hooks .setCurrentId (this .asyncId );
@@ -622,7 +644,7 @@ these JS handles are linked to the `TimerWrap` instance.
622
644
### Native Modules
623
645
624
646
Existing native modules that call ` node::MakeCallback ` today will always have
625
- their parent id as ` 1 ` . Which is the root id. There will be two native APIs
647
+ their trigger id as ` 1 ` . Which is the root id. There will be two native APIs
626
648
that users can use. One will be a static API and another will be a class that
627
649
users can inherit from.
628
650
0 commit comments