Skip to content

Commit

Permalink
async_hooks: add copyHooks function
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
danbev authored and MylesBorins committed Mar 20, 2018
1 parent 8757799 commit f0f31d0
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,23 @@ function storeActiveHooks() {
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
// 4. So do this the long way.
active_hooks.tmp_fields = [];
active_hooks.tmp_fields[kInit] = async_hook_fields[kInit];
active_hooks.tmp_fields[kBefore] = async_hook_fields[kBefore];
active_hooks.tmp_fields[kAfter] = async_hook_fields[kAfter];
active_hooks.tmp_fields[kDestroy] = async_hook_fields[kDestroy];
active_hooks.tmp_fields[kPromiseResolve] = async_hook_fields[kPromiseResolve];
copyHooks(active_hooks.tmp_fields, async_hook_fields);
}

function copyHooks(destination, source) {
destination[kInit] = source[kInit];
destination[kBefore] = source[kBefore];
destination[kAfter] = source[kAfter];
destination[kDestroy] = source[kDestroy];
destination[kPromiseResolve] = source[kPromiseResolve];
}


// Then restore the correct hooks array in case any hooks were added/removed
// during hook callback execution.
function restoreActiveHooks() {
active_hooks.array = active_hooks.tmp_array;
async_hook_fields[kInit] = active_hooks.tmp_fields[kInit];
async_hook_fields[kBefore] = active_hooks.tmp_fields[kBefore];
async_hook_fields[kAfter] = active_hooks.tmp_fields[kAfter];
async_hook_fields[kDestroy] = active_hooks.tmp_fields[kDestroy];
async_hook_fields[kPromiseResolve] = active_hooks.tmp_fields[kPromiseResolve];
copyHooks(async_hook_fields, active_hooks.tmp_fields);

active_hooks.tmp_array = null;
active_hooks.tmp_fields = null;
Expand Down

0 comments on commit f0f31d0

Please sign in to comment.