Skip to content

Commit 8c69d7b

Browse files
micnicrvagg
authored andcommitted
domain: forward args to .run(fn)
Adds the feature to define arguments for the function called in domain.run(), this is supposed to be useful when a function is called from another context and some values from the current context are needed as arguments, it's similar to the callback from setTimeout or setInterval. PR-URL: #15 Reviewed-By: Chris Dickinson <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 41ec1a7 commit 8c69d7b

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

benchmark/misc/domain-fn-args.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var common = require('../common.js');
2+
var domain = require('domain');
3+
4+
var bench = common.createBenchmark(main, {
5+
arguments: [0, 1, 2, 3],
6+
n: [10]
7+
});
8+
9+
var bdomain = domain.create();
10+
var gargs = [1, 2, 3];
11+
12+
function main(conf) {
13+
14+
var args, ret, n = +conf.n;
15+
var arguments = gargs.slice(0, conf.arguments);
16+
bench.start();
17+
18+
bdomain.enter();
19+
for (var i = 0; i < n; i++) {
20+
if (arguments.length >= 2) {
21+
args = Array.prototype.slice.call(arguments, 1);
22+
ret = fn.apply(this, args);
23+
} else {
24+
ret = fn.call(this);
25+
}
26+
}
27+
bdomain.exit();
28+
29+
bench.end(n);
30+
}
31+
32+
function fn(a, b, c) {
33+
if (!a)
34+
a = 1;
35+
36+
if (!b)
37+
b = 2;
38+
39+
if (!c)
40+
c = 3;
41+
42+
return a + b + c;
43+
}

doc/api/domain.markdown

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,14 @@ uncaught exceptions to the active Domain object.
260260
Domain is a child class of [EventEmitter][]. To handle the errors that it
261261
catches, listen to its `error` event.
262262

263-
### domain.run(fn)
263+
### domain.run(fn[, arg][, ...])
264264

265265
* `fn` {Function}
266266

267267
Run the supplied function in the context of the domain, implicitly
268268
binding all event emitters, timers, and lowlevel requests that are
269-
created in that context.
269+
created in that context. Optionally, arguments can be passed to
270+
the function.
270271

271272
This is the most basic way to use a domain.
272273

lib/domain.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,23 @@ Domain.prototype.remove = function(ee) {
195195
Domain.prototype.run = function(fn) {
196196
if (this._disposed)
197197
return;
198+
199+
var ret;
200+
198201
this.enter();
199-
var ret = fn.call(this);
202+
if (arguments.length >= 2) {
203+
var len = arguments.length;
204+
var args = new Array(len - 1);
205+
206+
for (var i = 1; i < len; i++)
207+
args[i - 1] = arguments[i];
208+
209+
ret = fn.apply(this, args);
210+
} else {
211+
ret = fn.call(this);
212+
}
200213
this.exit();
214+
201215
return ret;
202216
};
203217

test/simple/test-domain.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,13 @@ var result = d.run(function () {
258258
assert.equal(result, 'return value');
259259

260260

261+
// check if the executed function take in count the applied parameters
262+
result = d.run(function (a, b) {
263+
return a + ' ' + b;
264+
}, 'return', 'value');
265+
assert.equal(result, 'return value');
266+
267+
261268
var fst = fs.createReadStream('stream for nonexistent file')
262269
d.add(fst)
263270
expectCaught++;

0 commit comments

Comments
 (0)