Skip to content

Commit eb507e1

Browse files
author
Stephen Belanger
committed
async_hooks: refactor diagnostics_channel integration
1 parent 1341442 commit eb507e1

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

lib/async_hooks.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const {
2828
} = require('internal/validators');
2929
const internal_async_hooks = require('internal/async_hooks');
3030
const { tracingChannel } = require('diagnostics_channel');
31+
const console = require('console');
3132

3233
// Get functions
3334
// For userland AsyncResources, make sure to emit a destroy event when the
@@ -361,31 +362,13 @@ class AsyncLocalStorage {
361362

362363
const store = this;
363364

364-
function start(ctx) {
365-
if (store.enabled) {
366-
ctx[store.kResourceStore] = store.getStore();
367-
}
368-
store.enterWith(transform(ctx.data));
369-
}
370-
371-
function end(ctx) {
372-
if (ObjectPrototypeHasOwnProperty(ctx, store.kResourceStore)) {
373-
store.enterWith(ctx[store.kResourceStore]);
374-
} else {
375-
store.disable();
376-
}
365+
function start(register) {
366+
register(store, transform);
377367
}
378368

379-
channel.subscribe({
380-
start,
381-
end
382-
});
383-
369+
channel.subscribe({ start });
384370
return () => {
385-
channel.unsubscribe({
386-
start,
387-
end
388-
});
371+
channel.unsubscribe({ start });
389372
};
390373
}
391374

@@ -394,10 +377,26 @@ class AsyncLocalStorage {
394377
channel = tracingChannel(channel);
395378
}
396379

397-
channel.traceSync(runner, { data });
380+
const bindings = [];
381+
382+
function register(store, transform) {
383+
bindings.push({ store, data: transform(data) });
384+
}
385+
386+
return channel.traceSync(() => {
387+
for (const { store, data } of bindings) {
388+
runner = wrapRunInStoreBinding(store, data, runner);
389+
}
390+
391+
return runner();
392+
}, register);
398393
}
399394
}
400395

396+
function wrapRunInStoreBinding(store, data, next) {
397+
return () => store.run(data, next);
398+
}
399+
401400
// Placing all exports down here because the exported classes won't export
402401
// otherwise.
403402
module.exports = {

0 commit comments

Comments
 (0)