Skip to content

Commit f7f1437

Browse files
committed
src: add helper for before/after scope without JS calls
Add `AsyncScope` for cases where the async_hooks `before` and `after` callbacks should be called, to track async context, but no actual JS is called in between and we can therefore skip things like draining the microtask or `nextTick` queues. PR-URL: #18936 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent f734b3e commit f7f1437

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/async_wrap-inl.h

+16
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ inline double AsyncWrap::get_trigger_async_id() const {
4545
}
4646

4747

48+
inline AsyncWrap::AsyncScope::AsyncScope(AsyncWrap* wrap)
49+
: wrap_(wrap) {
50+
Environment* env = wrap->env();
51+
if (env->async_hooks()->fields()[Environment::AsyncHooks::kBefore] == 0)
52+
return;
53+
EmitBefore(env, wrap->get_async_id());
54+
}
55+
56+
inline AsyncWrap::AsyncScope::~AsyncScope() {
57+
Environment* env = wrap_->env();
58+
if (env->async_hooks()->fields()[Environment::AsyncHooks::kAfter] == 0)
59+
return;
60+
EmitAfter(env, wrap_->get_async_id());
61+
}
62+
63+
4864
inline v8::MaybeLocal<v8::Value> AsyncWrap::MakeCallback(
4965
const v8::Local<v8::String> symbol,
5066
int argc,

src/async_wrap.h

+12
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ class AsyncWrap : public BaseObject {
169169

170170
static void WeakCallback(const v8::WeakCallbackInfo<DestroyParam> &info);
171171

172+
// This is a simplified version of InternalCallbackScope that only runs
173+
// the `before` and `after` hooks. Only use it when not actually calling
174+
// back into JS; otherwise, use InternalCallbackScope.
175+
class AsyncScope {
176+
public:
177+
explicit inline AsyncScope(AsyncWrap* wrap);
178+
~AsyncScope();
179+
180+
private:
181+
AsyncWrap* wrap_ = nullptr;
182+
};
183+
172184
private:
173185
friend class PromiseWrap;
174186

0 commit comments

Comments
 (0)