Skip to content

Commit 657a33d

Browse files
committed
async_hooks: add copyHooks function
This commit introduces a copyHooks function that can be used by storeActiveHooks and restoreActiveHooks to remove some code duplication.
1 parent 1329844 commit 657a33d

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/internal/async_hooks.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,23 @@ function storeActiveHooks() {
212212
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
213213
// 4. So do this the long way.
214214
active_hooks.tmp_fields = [];
215-
active_hooks.tmp_fields[kInit] = async_hook_fields[kInit];
216-
active_hooks.tmp_fields[kBefore] = async_hook_fields[kBefore];
217-
active_hooks.tmp_fields[kAfter] = async_hook_fields[kAfter];
218-
active_hooks.tmp_fields[kDestroy] = async_hook_fields[kDestroy];
219-
active_hooks.tmp_fields[kPromiseResolve] = async_hook_fields[kPromiseResolve];
215+
copyHooks(active_hooks.tmp_fields, async_hook_fields);
216+
}
217+
218+
function copyHooks(destination, source) {
219+
destination[kInit] = source[kInit];
220+
destination[kBefore] = source[kBefore];
221+
destination[kAfter] = source[kAfter];
222+
destination[kDestroy] = source[kDestroy];
223+
destination[kPromiseResolve] = source[kPromiseResolve];
220224
}
221225

222226

223227
// Then restore the correct hooks array in case any hooks were added/removed
224228
// during hook callback execution.
225229
function restoreActiveHooks() {
226230
active_hooks.array = active_hooks.tmp_array;
227-
async_hook_fields[kInit] = active_hooks.tmp_fields[kInit];
228-
async_hook_fields[kBefore] = active_hooks.tmp_fields[kBefore];
229-
async_hook_fields[kAfter] = active_hooks.tmp_fields[kAfter];
230-
async_hook_fields[kDestroy] = active_hooks.tmp_fields[kDestroy];
231-
async_hook_fields[kPromiseResolve] = active_hooks.tmp_fields[kPromiseResolve];
231+
copyHooks(async_hook_fields, active_hooks.tmp_fields);
232232

233233
active_hooks.tmp_array = null;
234234
active_hooks.tmp_fields = null;

0 commit comments

Comments
 (0)