Skip to content

Commit 9da8346

Browse files
trevnorrisMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 09b3fae commit 9da8346

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
@@ -95,7 +95,8 @@ class AsyncWrap : public BaseObject {
9595

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

100101
virtual ~AsyncWrap();
101102

@@ -133,7 +134,7 @@ class AsyncWrap : public BaseObject {
133134

134135
inline double get_trigger_async_id() const;
135136

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

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

0 commit comments

Comments
 (0)