Skip to content
This repository was archived by the owner on Jul 31, 2018. It is now read-only.

Commit 5e8bb63

Browse files
committed
Rename parentId to triggerId
Intuitively the "parentId" of a given execution scope would be the "currentId" of the execution scope that created the handle that's currently executing. Using the formal definition for trigger "cause (an event or situation) to happen or exist", the "triggerId" is the id of the handle responsible for init() being called. For example: const server = net.createServer(conn => { // currentId() returns the id of the handle that's executing // the callback. In this case it's the id of "server". async_hooks.currentId(); // Though the handle that caused (or triggered) this callback to // be called was that of the new connection. Thus the return value // of triggerId() is the id of "conn". async_hooks.triggerId(); }).listen(); Add async_hooks.triggerId() API to return the trigger id responsible for the current execution scope.
1 parent 48d9c0b commit 5e8bb63

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

XXX-asyncwrap-api.md

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ more detail further down.
8383
const async_hooks = require('async_hooks');
8484

8585
// 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
8787
// 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();
8993

9094
// Create a new instance of AsyncHook. All of these callbacks are optional.
9195
const asyncHook = async_hooks.createHook({ init, before, after, destroy });
@@ -105,7 +109,7 @@ asyncHook.disable();
105109
// init() is called during object construction. The handle may not have
106110
// completed construction when this callback runs. So all fields of the
107111
// handle referenced by "id" may not have been populated.
108-
function init(id, type, parentId, handle) { }
112+
function init(id, type, triggerId, handle) { }
109113

110114
// before() is called just before the handle's callback is called. It can be
111115
// called 0-N times for handles (e.g. TCPWrap), and should be called exactly 1
@@ -133,7 +137,7 @@ async_hooks.setCurrentId(id);
133137

134138
// Call the init() callbacks. Returns an instance of AsyncEvent that will be
135139
// used in other emit calls.
136-
async_hooks.emitInit(id, handle, type[, parentId]);
140+
async_hooks.emitInit(id, handle, type[, triggerId]);
137141

138142
// Call the before() callbacks. The reason for requiring both arguments is
139143
// explained in further detail below.
@@ -155,8 +159,26 @@ The object returned from `require('async_hooks')`.
155159
#### `async_hooks.currentId()`
156160

157161
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+
```
160182

161183

162184
#### `async_hooks.createHook(callbacks)`
@@ -241,11 +263,11 @@ instance is destructed. For cases where resources are reused, instantiation and
241263
destructor calls are emulated.
242264

243265

244-
#### `init(id, type, parentId, handle)`
266+
#### `init(id, type, triggerId, handle)`
245267

246268
* `id` {Number}
247269
* `type` {String}
248-
* `parentId` {Number}
270+
* `triggerId` {Number}
249271
* `handle` {Object}
250272

251273
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
276298
define their own `type` when using the public embedder API.
277299

278300
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.
281303

282304
The following is a simple demonstration of this:
283305

@@ -286,9 +308,9 @@ const async_hooks = require('async_hooks');
286308
const net = require('net');
287309

288310
asyn_hooks.createHook({
289-
init: (id, type, parentId) => {
311+
init: (id, type, triggerId) => {
290312
const cId = async_hooks.currentId();
291-
process._rawDebug(`${type}(${id}): parent: ${parentId} scope: ${cId}`);
313+
process._rawDebug(`${type}(${id}): trigger: ${triggerId} scope: ${cId}`);
292314
}
293315
}).enable();
294316

@@ -298,16 +320,16 @@ net.createServer(c => {}).listen(8080);
298320
Output hitting the server with `nc localhost 8080`:
299321

300322
```
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
303325
```
304326

305327
The second `TCPWRAP` is the new connection from the client. When a new
306328
connection is made the `TCPWrap` instance is immediately constructed. This
307329
happens outside of any JavaScript stack (side note: a `currentId()` of `0`
308330
means it's being executed in the void, with no JavaScript stack above it). With
309331
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
311333
propagating what other handle is responsible for the new resource's existence.
312334

313335
Below is another example with additional information about the calls to
@@ -318,10 +340,10 @@ elaborate to make calling context easier to see.
318340
```js
319341
let ws = 0;
320342
async_hooks.createHook({
321-
init: (id, type, parentId) => {
343+
init: (id, type, triggerId) => {
322344
const cId = async_hooks.currentId();
323345
process._rawDebug(' '.repeat(ws) +
324-
`${type}(${id}): parent: ${parentId} scope: ${cId}`);
346+
`${type}(${id}): trigger: ${triggerId} scope: ${cId}`);
325347
},
326348
before: (id) => {
327349
print(' '.repeat(ws) + 'before: ', id);
@@ -344,17 +366,17 @@ net.createServer(() => {}).listen(8080, () => {
344366
Output from only starting the server:
345367

346368
```
347-
TCPWRAP(2): parent: 1 scope: 1
369+
TCPWRAP(2): trigger: 1 scope: 1
348370
# .listen()
349-
TickObject(3): parent: 1 scope: 1
371+
TickObject(3): trigger: 1 scope: 1
350372
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
353375
after: 3
354376
before: 5
355377
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
358380
>>> 4
359381
after: 4
360382
after: 5
@@ -376,7 +398,7 @@ completely asynchronous API the user's callback is placed in a
376398
`process.nextTick()`.
377399

378400
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.
380402

381403

382404
#### `before(id)`
@@ -477,7 +499,7 @@ MaybeLocal<Value> node::MakeCallback(Isolate* isolate,
477499
There is also a JS API to allow for conceptual correctness. For example, if a
478500
request batches calls under the hood this would still allow for each request
479501
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.
481503
482504
This cannot be enforced in any way. It is up to the implementer to make sure
483505
all of the callbacks are placed and called at the correct time.
@@ -501,12 +523,12 @@ class MyClass {
501523
```
502524

503525

504-
#### `async_hooks.emitInit(id, handle, type[, parentId])`
526+
#### `async_hooks.emitInit(id, handle, type[, triggerId])`
505527

506528
* `id` {Number} Generated by calling `newUid()`
507529
* `handle` {Object}
508530
* `type` {String}
509-
* `parentId` {Number} **Default:** `currentId()`
531+
* `triggerId` {Number} **Default:** `currentId()`
510532
* Return: {Undefined}
511533

512534
Emit that a handle is being initialized. `id` should be a value returned by
@@ -521,8 +543,8 @@ class Foo {
521543
}
522544
```
523545

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.
526548

527549
It is suggested to have `emitInit()` be the last call in the object's
528550
constructor.
@@ -546,7 +568,7 @@ MyThing.prototype.done = function done() {
546568
// handle wrapping the id that's been passed.
547569
async_hooks.emitBefore(this.asyncId);
548570

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
550572
// that was assigned when this object was instantiated.
551573
const previous_id = async_hooks.currentId();
552574
async_hooks.setCurrentId(this.asyncId);
@@ -622,7 +644,7 @@ these JS handles are linked to the `TimerWrap` instance.
622644
### Native Modules
623645

624646
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
626648
that users can use. One will be a static API and another will be a class that
627649
users can inherit from.
628650

0 commit comments

Comments
 (0)