Skip to content

Commit 55f1b33

Browse files
committed
[squash] address AndreasMadsen comment
1 parent af444fc commit 55f1b33

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/async-wrap.cc

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ bool AsyncWrap::EmitAfter(Environment* env, double async_id) {
283283

284284
class PromiseWrap : public AsyncWrap {
285285
public:
286-
PromiseWrap(Environment* env, Local<Object> object)
287-
: AsyncWrap(env, object, PROVIDER_PROMISE) {}
286+
PromiseWrap(Environment* env, Local<Object> object, bool silent)
287+
: AsyncWrap(env, object, PROVIDER_PROMISE, silent) {}
288288
size_t self_size() const override { return sizeof(*this); }
289289
};
290290

@@ -294,19 +294,10 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
294294
Local<Context> context = promise->CreationContext();
295295
Environment* env = Environment::GetCurrent(context);
296296
PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
297-
if (type == PromiseHookType::kInit ||
298-
wrap == nullptr) {
299-
uint32_t stored_init_field;
300-
if (type != PromiseHookType::kInit) {
301-
// Do not emit an init event.
302-
stored_init_field = env->async_hooks()->fields()[AsyncHooks::kInit];
303-
env->async_hooks()->fields()[AsyncHooks::kInit] = 0;
304-
}
305-
wrap = new PromiseWrap(env, promise);
297+
if (type == PromiseHookType::kInit || wrap == nullptr) {
298+
bool silent = type != PromiseHookType::kInit;
299+
wrap = new PromiseWrap(env, promise, silent);
306300
wrap->MakeWeak(wrap);
307-
if (type != PromiseHookType::kInit) {
308-
env->async_hooks()->fields()[AsyncHooks::kInit] = stored_init_field;
309-
}
310301
} else if (type == PromiseHookType::kResolve) {
311302
// TODO(matthewloring): need to expose this through the async hooks api.
312303
}
@@ -501,7 +492,8 @@ void LoadAsyncWrapperInfo(Environment* env) {
501492

502493
AsyncWrap::AsyncWrap(Environment* env,
503494
Local<Object> object,
504-
ProviderType provider)
495+
ProviderType provider,
496+
bool silent)
505497
: BaseObject(env, object),
506498
provider_type_(provider) {
507499
CHECK_NE(provider, PROVIDER_NONE);
@@ -511,7 +503,7 @@ AsyncWrap::AsyncWrap(Environment* env,
511503
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
512504

513505
// Use AsyncReset() call to execute the init() callbacks.
514-
AsyncReset();
506+
AsyncReset(silent);
515507
}
516508

517509

@@ -523,10 +515,13 @@ AsyncWrap::~AsyncWrap() {
523515
// Generalized call for both the constructor and for handles that are pooled
524516
// and reused over their lifetime. This way a new uid can be assigned when
525517
// the resource is pulled out of the pool and put back into use.
526-
void AsyncWrap::AsyncReset() {
518+
void AsyncWrap::AsyncReset(bool silent) {
519+
AsyncHooks* async_hooks = env()->async_hooks();
527520
async_id_ = env()->new_async_id();
528521
trigger_id_ = env()->get_init_trigger_id();
529522

523+
if (silent) return;
524+
530525
EmitAsyncInit(env(), object(),
531526
env()->async_hooks()->provider_string(provider_type()),
532527
async_id_, trigger_id_);

src/async-wrap.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class AsyncWrap : public BaseObject {
8686

8787
AsyncWrap(Environment* env,
8888
v8::Local<v8::Object> object,
89-
ProviderType provider);
89+
ProviderType provider,
90+
bool silent = false);
9091

9192
virtual ~AsyncWrap();
9293

@@ -116,7 +117,7 @@ class AsyncWrap : public BaseObject {
116117

117118
inline double get_trigger_id() const;
118119

119-
void AsyncReset();
120+
void AsyncReset(bool silent = false);
120121

121122
// Only call these within a valid HandleScope.
122123
// TODO(trevnorris): These should return a MaybeLocal.

0 commit comments

Comments
 (0)