-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (46 loc) · 1.25 KB
/
index.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Requires
var Q = require('q');
var _ = require('underscore');
var inherits = require('util').inherits;
function qClass(cls, methodExceptions) {
// Methods not to patch
methodExceptions = methodExceptions || [];
var newCls = function newCls() {
newCls.super_.apply(this, arguments);
};
inherits(newCls, cls);
// Set name
newCls.name = cls.name;
_.methods(cls.prototype).forEach(function(method) {
// Don't change internal functions
var f;
if(method[0] === '_' || _.contains(methodExceptions, method)) {
f = cls.prototype[method];
} else {
f = qMethod(cls.prototype[method]);
}
// Set function
newCls.prototype[method] =f;
});
return newCls;
}
function qMethod(method) {
var f = function() {
var d = Q.defer();
var args = _.toArray(arguments);
// Add callback
args.push(function(err, value) {
if(err) return d.reject(err);
return d.resolve(value);
});
// Call function
method.apply(this, args);
// Return promise
return d.promise;
};
f.name = method.name;
return f;
}
// Exports
exports.qClass = qClass;
exports.qMethod = qMethod;