Skip to content

Commit 2b9b46c

Browse files
committed
async_wrap: allow user to pass execution_async_id
Allow the user to pass in an execution_async_id instead of always generating one. This way the JS API can be used to pre-allocate the execution_async_id when the JS object is instantiated, before the native resource is created. Also allow the new execution_async_id to be passed via asyncReset(). PR-URL: #14208 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent e1eae3c commit 2b9b46c

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/async-wrap.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ void AsyncWrap::ClearAsyncIdStack(const FunctionCallbackInfo<Value>& args) {
451451
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
452452
AsyncWrap* wrap;
453453
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
454-
wrap->AsyncReset();
454+
double execution_async_id = args[0]->IsNumber() ? args[0]->NumberValue() : -1;
455+
wrap->AsyncReset(execution_async_id);
455456
}
456457

457458

@@ -573,7 +574,8 @@ void LoadAsyncWrapperInfo(Environment* env) {
573574

574575
AsyncWrap::AsyncWrap(Environment* env,
575576
Local<Object> object,
576-
ProviderType provider)
577+
ProviderType provider,
578+
double execution_async_id)
577579
: BaseObject(env, object),
578580
provider_type_(provider) {
579581
CHECK_NE(provider, PROVIDER_NONE);
@@ -583,7 +585,7 @@ AsyncWrap::AsyncWrap(Environment* env,
583585
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
584586

585587
// Use AsyncReset() call to execute the init() callbacks.
586-
AsyncReset();
588+
AsyncReset(execution_async_id);
587589
}
588590

589591

@@ -599,7 +601,7 @@ AsyncWrap::AsyncWrap(Environment* env,
599601
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_);
600602

601603
// Use AsyncReset() call to execute the init() callbacks.
602-
AsyncReset(silent);
604+
AsyncReset(-1, silent);
603605
}
604606

605607

@@ -611,8 +613,9 @@ AsyncWrap::~AsyncWrap() {
611613
// Generalized call for both the constructor and for handles that are pooled
612614
// and reused over their lifetime. This way a new uid can be assigned when
613615
// the resource is pulled out of the pool and put back into use.
614-
void AsyncWrap::AsyncReset(bool silent) {
615-
async_id_ = env()->new_async_id();
616+
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
617+
async_id_ =
618+
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
616619
trigger_async_id_ = env()->get_init_trigger_async_id();
617620

618621
if (silent) return;

src/async-wrap.h

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

9595
AsyncWrap(Environment* env,
9696
v8::Local<v8::Object> object,
97-
ProviderType provider);
97+
ProviderType provider,
98+
double execution_async_id = -1);
9899

99100
virtual ~AsyncWrap();
100101

@@ -132,7 +133,7 @@ class AsyncWrap : public BaseObject {
132133

133134
inline double get_trigger_async_id() const;
134135

135-
void AsyncReset(bool silent = false);
136+
void AsyncReset(double execution_async_id = -1, bool silent = false);
136137

137138
// Only call these within a valid HandleScope.
138139
v8::MaybeLocal<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,

0 commit comments

Comments
 (0)