Skip to content

Commit 054dd28

Browse files
danbevMylesBorins
authored andcommitted
src: make AsyncWrap constructors delegate
Currently, there is an AsyncWrap constructor that is only used by PromiseWrap. This constructor has a body which is very similar to the other AsyncWrap constructor. This commit suggests updating the private constructor that is used by PromiseWrap and also have the second constructor delegate to this one to avoid the code duplication. PR-URL: #19366 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent f490421 commit 054dd28

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/async_wrap.cc

+7-16
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void AsyncWrap::EmitAfter(Environment* env, double async_id) {
230230
class PromiseWrap : public AsyncWrap {
231231
public:
232232
PromiseWrap(Environment* env, Local<Object> object, bool silent)
233-
: AsyncWrap(env, object, silent) {
233+
: AsyncWrap(env, object, PROVIDER_PROMISE, -1, silent) {
234234
MakeWeak(this);
235235
}
236236
size_t self_size() const override { return sizeof(*this); }
@@ -602,32 +602,23 @@ AsyncWrap::AsyncWrap(Environment* env,
602602
Local<Object> object,
603603
ProviderType provider,
604604
double execution_async_id)
605-
: BaseObject(env, object),
606-
provider_type_(provider) {
607-
CHECK_NE(provider, PROVIDER_NONE);
608-
CHECK_GE(object->InternalFieldCount(), 1);
605+
: AsyncWrap(env, object, provider, execution_async_id, false) {}
609606

610-
// Shift provider value over to prevent id collision.
611-
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider);
612-
613-
// Use AsyncReset() call to execute the init() callbacks.
614-
AsyncReset(execution_async_id);
615-
}
616-
617-
618-
// This is specifically used by the PromiseWrap constructor.
619607
AsyncWrap::AsyncWrap(Environment* env,
620608
Local<Object> object,
609+
ProviderType provider,
610+
double execution_async_id,
621611
bool silent)
622612
: BaseObject(env, object),
623-
provider_type_(PROVIDER_PROMISE) {
613+
provider_type_(provider) {
614+
CHECK_NE(provider, PROVIDER_NONE);
624615
CHECK_GE(object->InternalFieldCount(), 1);
625616

626617
// Shift provider value over to prevent id collision.
627618
persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider_type_);
628619

629620
// Use AsyncReset() call to execute the init() callbacks.
630-
AsyncReset(-1, silent);
621+
AsyncReset(execution_async_id, silent);
631622
}
632623

633624

src/async_wrap.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,11 @@ class AsyncWrap : public BaseObject {
172172
private:
173173
friend class PromiseWrap;
174174

175-
// This is specifically used by the PromiseWrap constructor.
176-
AsyncWrap(Environment* env, v8::Local<v8::Object> promise, bool silent);
175+
AsyncWrap(Environment* env,
176+
v8::Local<v8::Object> promise,
177+
ProviderType provider,
178+
double execution_async_id,
179+
bool silent);
177180
inline AsyncWrap();
178181
const ProviderType provider_type_;
179182
// Because the values may be Reset(), cannot be made const.

0 commit comments

Comments
 (0)