Skip to content

Commit 7b51c63

Browse files
danbevMayaLekova
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: nodejs#19295 Reviewed-By: Anna Henningsen <[email protected]>
1 parent eb94cdd commit 7b51c63

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
@@ -162,19 +162,25 @@ static void DestroyAsyncIdsCallback(void* arg) {
162162
}
163163

164164

165-
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
165+
void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
166+
Local<Function> fn) {
166167
AsyncHooks* async_hooks = env->async_hooks();
167168

168-
if (async_hooks->fields()[AsyncHooks::kPromiseResolve] == 0)
169+
if (async_hooks->fields()[type] == 0)
169170
return;
170171

171172
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
172-
Local<Function> fn = env->async_hooks_promise_resolve_function();
173173
FatalTryCatch try_catch(env);
174174
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
175175
}
176176

177177

178+
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
179+
Emit(env, async_id, AsyncHooks::kPromiseResolve,
180+
env->async_hooks_promise_resolve_function());
181+
}
182+
183+
178184
void AsyncWrap::EmitTraceEventBefore() {
179185
switch (provider_type()) {
180186
#define V(PROVIDER) \
@@ -192,15 +198,8 @@ void AsyncWrap::EmitTraceEventBefore() {
192198

193199

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

206205

@@ -221,17 +220,10 @@ void AsyncWrap::EmitTraceEventAfter() {
221220

222221

223222
void AsyncWrap::EmitAfter(Environment* env, double async_id) {
224-
AsyncHooks* async_hooks = env->async_hooks();
225-
226-
if (async_hooks->fields()[AsyncHooks::kAfter] == 0)
227-
return;
228-
229223
// If the user's callback failed then the after() hooks will be called at the
230224
// end of _fatalException().
231-
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
232-
Local<Function> fn = env->async_hooks_after_function();
233-
FatalTryCatch try_catch(env);
234-
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
225+
Emit(env, async_id, AsyncHooks::kAfter,
226+
env->async_hooks_after_function());
235227
}
236228

237229
class PromiseWrap : public AsyncWrap {

0 commit comments

Comments
 (0)