Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[squash] do not emit init event
  • Loading branch information
addaleax committed May 27, 2017
commit af444fcf040f763a831ae6c966aa2a1e9f7c6cf2
9 changes: 9 additions & 0 deletions src/async-wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,17 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
if (type == PromiseHookType::kInit ||
wrap == nullptr) {
uint32_t stored_init_field;
if (type != PromiseHookType::kInit) {
// Do not emit an init event.
stored_init_field = env->async_hooks()->fields()[AsyncHooks::kInit];
env->async_hooks()->fields()[AsyncHooks::kInit] = 0;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, this is a fun hack :) – I'm not sure I approve, would a default parameter in AsyncWrap::AsyncWrap and AsyncWrap::AsyncReset be that difficult?

@Fishrock123 thoughts?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would a default parameter in AsyncWrap::AsyncWrap and AsyncWrap::AsyncReset be that difficult?

@AndreasMadsen yeah no you’re right. done. 😄

}
wrap = new PromiseWrap(env, promise);
wrap->MakeWeak(wrap);
if (type != PromiseHookType::kInit) {
env->async_hooks()->fields()[AsyncHooks::kInit] = stored_init_field;
}
} else if (type == PromiseHookType::kResolve) {
// TODO(matthewloring): need to expose this through the async hooks api.
}
Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-async-wrap-promise-after-enabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const p = new Promise((resolve) => resolve(1));
p.then(() => seenEvents.push('then'));

const hooks = async_hooks.createHook({
init: common.mustNotCall(),

before: common.mustCall((id) => {
assert.ok(id > 1);
seenEvents.push('before');
Expand All @@ -23,8 +25,10 @@ const hooks = async_hooks.createHook({
seenEvents.push('after');
hooks.disable();
})
}).enable();
});

setImmediate(() => {
assert.deepStrictEqual(seenEvents, ['before', 'then', 'after']);
});

hooks.enable(); // After `setImmediate` in order to not catch its init event.