Skip to content

Commit e208282

Browse files
danbevMylesBorins
authored andcommitted
src: refactor emit before/after/promiseResolve
Currently EmitBefore, EmitAfter, EmitPromiseResolve are very similar. This commit suggests extracting the code they have in common to a new function to reduce code duplication. PR-URL: #19295 Reviewed-By: Anna Henningsen <[email protected]>
1 parent 11a0ef5 commit e208282

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/async_wrap.cc

+13-21
Original file line numberDiff line numberDiff line change
@@ -166,19 +166,25 @@ static void DestroyAsyncIdsCallback(void* arg) {
166166
}
167167

168168

169-
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
169+
void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
170+
Local<Function> fn) {
170171
AsyncHooks* async_hooks = env->async_hooks();
171172

172-
if (async_hooks->fields()[AsyncHooks::kPromiseResolve] == 0)
173+
if (async_hooks->fields()[type] == 0)
173174
return;
174175

175176
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
176-
Local<Function> fn = env->async_hooks_promise_resolve_function();
177177
FatalTryCatch try_catch(env);
178178
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
179179
}
180180

181181

182+
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
183+
Emit(env, async_id, AsyncHooks::kPromiseResolve,
184+
env->async_hooks_promise_resolve_function());
185+
}
186+
187+
182188
void AsyncWrap::EmitTraceEventBefore() {
183189
switch (provider_type()) {
184190
#define V(PROVIDER) \
@@ -195,15 +201,8 @@ void AsyncWrap::EmitTraceEventBefore() {
195201

196202

197203
void AsyncWrap::EmitBefore(Environment* env, double async_id) {
198-
AsyncHooks* async_hooks = env->async_hooks();
199-
200-
if (async_hooks->fields()[AsyncHooks::kBefore] == 0)
201-
return;
202-
203-
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
204-
Local<Function> fn = env->async_hooks_before_function();
205-
FatalTryCatch try_catch(env);
206-
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
204+
Emit(env, async_id, AsyncHooks::kBefore,
205+
env->async_hooks_before_function());
207206
}
208207

209208

@@ -223,17 +222,10 @@ void AsyncWrap::EmitTraceEventAfter() {
223222

224223

225224
void AsyncWrap::EmitAfter(Environment* env, double async_id) {
226-
AsyncHooks* async_hooks = env->async_hooks();
227-
228-
if (async_hooks->fields()[AsyncHooks::kAfter] == 0)
229-
return;
230-
231225
// If the user's callback failed then the after() hooks will be called at the
232226
// end of _fatalException().
233-
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
234-
Local<Function> fn = env->async_hooks_after_function();
235-
FatalTryCatch try_catch(env);
236-
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
227+
Emit(env, async_id, AsyncHooks::kAfter,
228+
env->async_hooks_after_function());
237229
}
238230

239231
class PromiseWrap : public AsyncWrap {

0 commit comments

Comments
 (0)