mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
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: https://github.com/nodejs/node/pull/19295 Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
1 parent
11a0ef566a
commit
e208282f68
1 file changed
+13
-21
+13
-21
@@ -166,19 +166,25 @@ static void DestroyAsyncIdsCallback(void* arg) {
|
||||
}
|
||||
|
||||
|
||||
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
|
||||
void Emit(Environment* env, double async_id, AsyncHooks::Fields type,
|
||||
Local<Function> fn) {
|
||||
AsyncHooks* async_hooks = env->async_hooks();
|
||||
|
||||
if (async_hooks->fields()[AsyncHooks::kPromiseResolve] == 0)
|
||||
if (async_hooks->fields()[type] == 0)
|
||||
return;
|
||||
|
||||
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
|
||||
Local<Function> fn = env->async_hooks_promise_resolve_function();
|
||||
FatalTryCatch try_catch(env);
|
||||
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
|
||||
}
|
||||
|
||||
|
||||
void AsyncWrap::EmitPromiseResolve(Environment* env, double async_id) {
|
||||
Emit(env, async_id, AsyncHooks::kPromiseResolve,
|
||||
env->async_hooks_promise_resolve_function());
|
||||
}
|
||||
|
||||
|
||||
void AsyncWrap::EmitTraceEventBefore() {
|
||||
switch (provider_type()) {
|
||||
#define V(PROVIDER) \
|
||||
@@ -195,15 +201,8 @@ void AsyncWrap::EmitTraceEventBefore() {
|
||||
|
||||
|
||||
void AsyncWrap::EmitBefore(Environment* env, double async_id) {
|
||||
AsyncHooks* async_hooks = env->async_hooks();
|
||||
|
||||
if (async_hooks->fields()[AsyncHooks::kBefore] == 0)
|
||||
return;
|
||||
|
||||
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
|
||||
Local<Function> fn = env->async_hooks_before_function();
|
||||
FatalTryCatch try_catch(env);
|
||||
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
|
||||
Emit(env, async_id, AsyncHooks::kBefore,
|
||||
env->async_hooks_before_function());
|
||||
}
|
||||
|
||||
|
||||
@@ -223,17 +222,10 @@ void AsyncWrap::EmitTraceEventAfter() {
|
||||
|
||||
|
||||
void AsyncWrap::EmitAfter(Environment* env, double async_id) {
|
||||
AsyncHooks* async_hooks = env->async_hooks();
|
||||
|
||||
if (async_hooks->fields()[AsyncHooks::kAfter] == 0)
|
||||
return;
|
||||
|
||||
// If the user's callback failed then the after() hooks will be called at the
|
||||
// end of _fatalException().
|
||||
Local<Value> async_id_value = Number::New(env->isolate(), async_id);
|
||||
Local<Function> fn = env->async_hooks_after_function();
|
||||
FatalTryCatch try_catch(env);
|
||||
USE(fn->Call(env->context(), Undefined(env->isolate()), 1, &async_id_value));
|
||||
Emit(env, async_id, AsyncHooks::kAfter,
|
||||
env->async_hooks_after_function());
|
||||
}
|
||||
|
||||
class PromiseWrap : public AsyncWrap {
|
||||
|
||||
Reference in new issue
Block a user