Skip to content

Commit 0642a14

Browse files
trevnorrisMyles Borins
authored and
Myles Borins
committed
async_wrap: setupHooks now accepts object
The number of callbacks accepted to setupHooks was getting unwieldy. Instead change the implementation to accept an object with all callbacks Ref: #7048 PR-URL: #5756 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Andreas Madsen <[email protected]>
1 parent c3d87ee commit 0642a14

6 files changed

+32
-15
lines changed

src/async-wrap.cc

+26-9
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,35 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
121121

122122
if (env->async_hooks()->callbacks_enabled())
123123
return env->ThrowError("hooks should not be set while also enabled");
124-
125-
if (!args[0]->IsFunction())
124+
if (!args[0]->IsObject())
125+
return env->ThrowTypeError("first argument must be an object");
126+
127+
Local<Object> fn_obj = args[0].As<Object>();
128+
129+
Local<Value> init_v = fn_obj->Get(
130+
env->context(),
131+
FIXED_ONE_BYTE_STRING(env->isolate(), "init")).ToLocalChecked();
132+
Local<Value> pre_v = fn_obj->Get(
133+
env->context(),
134+
FIXED_ONE_BYTE_STRING(env->isolate(), "pre")).ToLocalChecked();
135+
Local<Value> post_v = fn_obj->Get(
136+
env->context(),
137+
FIXED_ONE_BYTE_STRING(env->isolate(), "post")).ToLocalChecked();
138+
Local<Value> destroy_v = fn_obj->Get(
139+
env->context(),
140+
FIXED_ONE_BYTE_STRING(env->isolate(), "destroy")).ToLocalChecked();
141+
142+
if (!init_v->IsFunction())
126143
return env->ThrowTypeError("init callback must be a function");
127144

128-
env->set_async_hooks_init_function(args[0].As<Function>());
145+
env->set_async_hooks_init_function(init_v.As<Function>());
129146

130-
if (args[1]->IsFunction())
131-
env->set_async_hooks_pre_function(args[1].As<Function>());
132-
if (args[2]->IsFunction())
133-
env->set_async_hooks_post_function(args[2].As<Function>());
134-
if (args[3]->IsFunction())
135-
env->set_async_hooks_destroy_function(args[3].As<Function>());
147+
if (pre_v->IsFunction())
148+
env->set_async_hooks_pre_function(pre_v.As<Function>());
149+
if (post_v->IsFunction())
150+
env->set_async_hooks_post_function(post_v.As<Function>());
151+
if (destroy_v->IsFunction())
152+
env->set_async_hooks_destroy_function(destroy_v.As<Function>());
136153
}
137154

138155

test/parallel/test-async-wrap-check-providers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function init(id, provider) {
3636

3737
function noop() { }
3838

39-
async_wrap.setupHooks(init, noop, noop);
39+
async_wrap.setupHooks({ init });
4040

4141
async_wrap.enable();
4242

test/parallel/test-async-wrap-disabled-propagate-parent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function init(uid, type, parentUid, parentHandle) {
3030

3131
function noop() { }
3232

33-
async_wrap.setupHooks(init, noop, noop);
33+
async_wrap.setupHooks({ init });
3434
async_wrap.enable();
3535

3636
const server = net.createServer(function(c) {

test/parallel/test-async-wrap-propagate-parent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function init(uid, type, parentUid, parentHandle) {
3030

3131
function noop() { }
3232

33-
async_wrap.setupHooks(init, noop, noop);
33+
async_wrap.setupHooks({ init });
3434
async_wrap.enable();
3535

3636
const server = net.createServer(function(c) {

test/parallel/test-async-wrap-throw-no-init.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const async_wrap = process.binding('async_wrap');
77

88
assert.throws(function() {
99
async_wrap.setupHooks(null);
10-
}, /init callback must be a function/);
10+
}, /first argument must be an object/);
1111

1212
assert.throws(function() {
1313
async_wrap.enable();
1414
}, /init callback is not assigned to a function/);
1515

1616
// Should not throw
17-
async_wrap.setupHooks(() => {});
17+
async_wrap.setupHooks({ init: () => {} });
1818
async_wrap.enable();
1919

2020
assert.throws(function() {

test/parallel/test-async-wrap-uid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const assert = require('assert');
66
const async_wrap = process.binding('async_wrap');
77

88
const storage = new Map();
9-
async_wrap.setupHooks(init, pre, post, destroy);
9+
async_wrap.setupHooks({ init, pre, post, destroy });
1010
async_wrap.enable();
1111

1212
function init(uid) {

0 commit comments

Comments
 (0)