-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
85 lines (73 loc) · 2.2 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var Fiber = require('fibers');
var Future = require('fibers/future');
var _ = require('underscore');
var assert = require('assert');
// Makes a function that returns a promise or takes a callback synchronous
exports.wrapAsync = function (fn, context, options) {
return function (/* arguments */) {
var self = context || this;
options = _.defaults({}, options, {
supportCallback: true
})
var newArgs = _.toArray(arguments);
var future = new Future();
var callback = _.once(function resolver(error, result) {
if (error) {
future.throw(error);
} else {
if (_.isUndefined(result)) {
result = self;
}
future.return(result);
}
});
var hasReturnedPromise = false;
if (options.supportCallback) {
newArgs.push(function () {
if (!hasReturnedPromise) {
callback.apply(null, arguments);
}
});
}
var result = fn.apply(self, newArgs);
if (result && _.isFunction(result.then)) {
hasReturnedPromise = true;
result.then(
function (result) {
callback(null, result);
},
function (error) {
callback(error);
}
);
}
return future.wait();
};
};
exports.wrapAsyncObject = function (object, properties, options) {
options = options || {};
var syncByDefault = options.syncByDefault !== false;
var wrapAsync = options.wrapAsync || exports.wrapAsync;
var wrapper = {
_original: object
};
_.forEach(properties, function (propertyName) {
var asyncMethod = object[propertyName];
var boundAsyncMethod = asyncMethod.bind(object);
if (_.isFunction(asyncMethod)) {
var syncMethod = wrapAsync(asyncMethod, object);
var asyncMethodName = propertyName + 'Async';
if (properties.indexOf(asyncMethodName) !== -1) {
asyncMethodName += '2';
}
wrapper[asyncMethodName] = boundAsyncMethod;
var syncMethodName = propertyName + 'Sync';
if (properties.indexOf(syncMethodName) !== -1) {
syncMethodName += '2';
}
wrapper[syncMethodName] = syncMethod;
wrapper[propertyName] = syncByDefault ? syncMethod : boundAsyncMethod;
}
});
return wrapper;
};