Skip to content

Commit 9fe3f85

Browse files
trevnorrisaddaleax
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: nodejs/node#14208 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 5e1d151 commit 9fe3f85

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
@@ -455,7 +455,8 @@ void AsyncWrap::ClearAsyncIdStack(const FunctionCallbackInfo<Value>& args) {
455455
void AsyncWrap::AsyncReset(const FunctionCallbackInfo<Value>& args) {
456456
AsyncWrap* wrap;
457457
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
458-
wrap->AsyncReset();
458+
double execution_async_id = args[0]->IsNumber() ? args[0]->NumberValue() : -1;
459+
wrap->AsyncReset(execution_async_id);
459460
}
460461

461462

@@ -577,7 +578,8 @@ void LoadAsyncWrapperInfo(Environment* env) {
577578

578579
AsyncWrap::AsyncWrap(Environment* env,
579580
Local<Object> object,
580-
ProviderType provider)
581+
ProviderType provider,
582+
double execution_async_id)
581583
: BaseObject(env, object),
582584
provider_type_(provider) {
583585
CHECK_NE(provider, PROVIDER_NONE);
@@ -587,7 +589,7 @@ AsyncWrap::AsyncWrap(Environment* env,
587589
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
588590

589591
// Use AsyncReset() call to execute the init() callbacks.
590-
AsyncReset();
592+
AsyncReset(execution_async_id);
591593
}
592594

593595

@@ -603,7 +605,7 @@ AsyncWrap::AsyncWrap(Environment* env,
603605
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_);
604606

605607
// Use AsyncReset() call to execute the init() callbacks.
606-
AsyncReset(silent);
608+
AsyncReset(-1, silent);
607609
}
608610

609611

@@ -615,8 +617,9 @@ AsyncWrap::~AsyncWrap() {
615617
// Generalized call for both the constructor and for handles that are pooled
616618
// and reused over their lifetime. This way a new uid can be assigned when
617619
// the resource is pulled out of the pool and put back into use.
618-
void AsyncWrap::AsyncReset(bool silent) {
619-
async_id_ = env()->new_async_id();
620+
void AsyncWrap::AsyncReset(double execution_async_id, bool silent) {
621+
async_id_ =
622+
execution_async_id == -1 ? env()->new_async_id() : execution_async_id;
620623
trigger_async_id_ = env()->get_init_trigger_async_id();
621624

622625
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)