Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a485612

Browse filesBrowse files
trevnorrisevanlucas
authored andcommitted
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 PR-URL: #5756 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
1 parent f9d0166 commit a485612
Copy full SHA for a485612

File tree

Expand file treeCollapse file tree

6 files changed

+32
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

6 files changed

+32
-15
lines changed
Open diff view settings
Collapse file

‎src/async-wrap.cc‎

Copy file name to clipboardExpand all lines: src/async-wrap.cc
+26-9Lines changed: 26 additions & 9 deletions
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-check-providers.js
+1-1Lines changed: 1 addition & 1 deletion
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

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-disabled-propagate-parent.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function init(uid, type, parentUid, parentHandle) {
3131

3232
function noop() { }
3333

34-
async_wrap.setupHooks(init, noop, noop);
34+
async_wrap.setupHooks({ init });
3535
async_wrap.enable();
3636

3737
server = net.createServer(function(c) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-propagate-parent.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function init(uid, type, parentUid, parentHandle) {
3131

3232
function noop() { }
3333

34-
async_wrap.setupHooks(init, noop, noop);
34+
async_wrap.setupHooks({ init });
3535
async_wrap.enable();
3636

3737
server = net.createServer(function(c) {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-throw-no-init.js
+2-2Lines changed: 2 additions & 2 deletions
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() {
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-async-wrap-uid.js
+1-1Lines changed: 1 addition & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.