@@ -283,8 +283,8 @@ bool AsyncWrap::EmitAfter(Environment* env, double async_id) {
283
283
284
284
class PromiseWrap : public AsyncWrap {
285
285
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 ) {}
288
288
size_t self_size () const override { return sizeof (*this ); }
289
289
};
290
290
@@ -293,13 +293,14 @@ static void PromiseHook(PromiseHookType type, Local<Promise> promise,
293
293
Local<Value> parent, void * arg) {
294
294
Local<Context> context = promise->CreationContext ();
295
295
Environment* env = Environment::GetCurrent (context);
296
- if (type == PromiseHookType::kInit ) {
297
- PromiseWrap* wrap = new PromiseWrap (env, promise);
296
+ PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
297
+ if (type == PromiseHookType::kInit || wrap == nullptr ) {
298
+ bool silent = type != PromiseHookType::kInit ;
299
+ wrap = new PromiseWrap (env, promise, silent);
298
300
wrap->MakeWeak (wrap);
299
301
} else if (type == PromiseHookType::kResolve ) {
300
302
// TODO(matthewloring): need to expose this through the async hooks api.
301
303
}
302
- PromiseWrap* wrap = Unwrap<PromiseWrap>(promise);
303
304
CHECK_NE (wrap, nullptr );
304
305
if (type == PromiseHookType::kBefore ) {
305
306
PreCallbackExecution (wrap, false );
@@ -491,7 +492,8 @@ void LoadAsyncWrapperInfo(Environment* env) {
491
492
492
493
AsyncWrap::AsyncWrap (Environment* env,
493
494
Local<Object> object,
494
- ProviderType provider)
495
+ ProviderType provider,
496
+ bool silent)
495
497
: BaseObject(env, object),
496
498
provider_type_(provider) {
497
499
CHECK_NE (provider, PROVIDER_NONE);
@@ -501,7 +503,7 @@ AsyncWrap::AsyncWrap(Environment* env,
501
503
persistent ().SetWrapperClassId (NODE_ASYNC_ID_OFFSET + provider);
502
504
503
505
// Use AsyncReset() call to execute the init() callbacks.
504
- AsyncReset ();
506
+ AsyncReset (silent );
505
507
}
506
508
507
509
@@ -513,10 +515,12 @@ AsyncWrap::~AsyncWrap() {
513
515
// Generalized call for both the constructor and for handles that are pooled
514
516
// and reused over their lifetime. This way a new uid can be assigned when
515
517
// the resource is pulled out of the pool and put back into use.
516
- void AsyncWrap::AsyncReset () {
518
+ void AsyncWrap::AsyncReset (bool silent ) {
517
519
async_id_ = env ()->new_async_id ();
518
520
trigger_id_ = env ()->get_init_trigger_id ();
519
521
522
+ if (silent) return ;
523
+
520
524
EmitAsyncInit (env (), object (),
521
525
env ()->async_hooks ()->provider_string (provider_type ()),
522
526
async_id_, trigger_id_);
0 commit comments