Skip to content

Commit f0f31d0

Browse files
danbevMylesBorins
authored andcommitted
async_hooks: add copyHooks function
This commit introduces a copyHooks function that can be used by storeActiveHooks and restoreActiveHooks to remove some code duplication. PR-URL: #19391 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent 8757799 commit f0f31d0

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/internal/async_hooks.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,23 @@ function storeActiveHooks() {
207207
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
208208
// 4. So do this the long way.
209209
active_hooks.tmp_fields = [];
210-
active_hooks.tmp_fields[kInit] = async_hook_fields[kInit];
211-
active_hooks.tmp_fields[kBefore] = async_hook_fields[kBefore];
212-
active_hooks.tmp_fields[kAfter] = async_hook_fields[kAfter];
213-
active_hooks.tmp_fields[kDestroy] = async_hook_fields[kDestroy];
214-
active_hooks.tmp_fields[kPromiseResolve] = async_hook_fields[kPromiseResolve];
210+
copyHooks(active_hooks.tmp_fields, async_hook_fields);
211+
}
212+
213+
function copyHooks(destination, source) {
214+
destination[kInit] = source[kInit];
215+
destination[kBefore] = source[kBefore];
216+
destination[kAfter] = source[kAfter];
217+
destination[kDestroy] = source[kDestroy];
218+
destination[kPromiseResolve] = source[kPromiseResolve];
215219
}
216220

217221

218222
// Then restore the correct hooks array in case any hooks were added/removed
219223
// during hook callback execution.
220224
function restoreActiveHooks() {
221225
active_hooks.array = active_hooks.tmp_array;
222-
async_hook_fields[kInit] = active_hooks.tmp_fields[kInit];
223-
async_hook_fields[kBefore] = active_hooks.tmp_fields[kBefore];
224-
async_hook_fields[kAfter] = active_hooks.tmp_fields[kAfter];
225-
async_hook_fields[kDestroy] = active_hooks.tmp_fields[kDestroy];
226-
async_hook_fields[kPromiseResolve] = active_hooks.tmp_fields[kPromiseResolve];
226+
copyHooks(async_hook_fields, active_hooks.tmp_fields);
227227

228228
active_hooks.tmp_array = null;
229229
active_hooks.tmp_fields = null;

0 commit comments

Comments
 (0)