@@ -283,8 +283,8 @@ bool AsyncWrap::EmitAfter(Environment* env, double async_id) {
283283
284284class 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
502493AsyncWrap::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_);
0 commit comments