diff --git a/src/async_wrap-inl.h b/src/async_wrap-inl.h index 21b1f9cee9f0e8..c9f12333243092 100644 --- a/src/async_wrap-inl.h +++ b/src/async_wrap-inl.h @@ -45,6 +45,22 @@ inline double AsyncWrap::get_trigger_async_id() const { } +inline AsyncWrap::AsyncScope::AsyncScope(AsyncWrap* wrap) + : wrap_(wrap) { + Environment* env = wrap->env(); + if (env->async_hooks()->fields()[Environment::AsyncHooks::kBefore] == 0) + return; + EmitBefore(env, wrap->get_async_id()); +} + +inline AsyncWrap::AsyncScope::~AsyncScope() { + Environment* env = wrap_->env(); + if (env->async_hooks()->fields()[Environment::AsyncHooks::kAfter] == 0) + return; + EmitAfter(env, wrap_->get_async_id()); +} + + inline v8::MaybeLocal AsyncWrap::MakeCallback( const v8::Local symbol, int argc, diff --git a/src/async_wrap.h b/src/async_wrap.h index b7aed5d789754b..608764bab5361c 100644 --- a/src/async_wrap.h +++ b/src/async_wrap.h @@ -169,6 +169,18 @@ class AsyncWrap : public BaseObject { static void WeakCallback(const v8::WeakCallbackInfo &info); + // This is a simplified version of InternalCallbackScope that only runs + // the `before` and `after` hooks. Only use it when not actually calling + // back into JS; otherwise, use InternalCallbackScope. + class AsyncScope { + public: + explicit inline AsyncScope(AsyncWrap* wrap); + ~AsyncScope(); + + private: + AsyncWrap* wrap_ = nullptr; + }; + private: friend class PromiseWrap;