-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
39 lines (35 loc) · 960 Bytes
/
Copy pathindex.js
File metadata and controls
39 lines (35 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
var sliced = require('sliced');
function twoArgHandler(parent, callback) {
return function(err) {
if(err) {
parent(err);
} else {
callback.apply(callback, sliced(arguments, 1));
}
}
}
//bind callbacks with active domains
function withDomains(parent, callback) {
if(callback) {
return process.domain.bind(twoArgHandler(parent, callback));
}
return process.domain.intercept(parent);
}
//bind callbacks without an active domain
function withoutDomains(parent, callback) {
if(callback) {
return twoArgHandler(parent, callback);
}
//single callback + no domain = throw on error
return function(err) {
if(err) throw err;
parent.apply(parent, sliced(arguments, 1));
}
}
function zeroArgHandler(err) {
if(err) throw err;
}
module.exports = function(parent, callback) {
if (!parent) return zeroArgHandler;
return process.domain ? withDomains(parent, callback) : withoutDomains(parent, callback);
};