-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
101 lines (94 loc) · 3.46 KB
/
utils.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import config from './config';
/**
* Converts given argument to a Promise resolved with AsyncResult instance.
* In case argument is an Error AsyncResult instance will be created with error.
* In case argument is a promise which wiil be rejected will create AsyncResult with error
*
* @param {*} promise - any argument
* @param {*} [OwnAsyncResult] - own AsyncResult class, if omitted will be used config's version
* @returns {Promise} - promise which will be resolved with AsyncResult
*/
function toAsyncResult(promise, OwnAsyncResult) {
let AsyncResult = OwnAsyncResult || config.AsyncResult;
const newPromise = new Promise(resolve => {
// if given argument is AsyncResult then just resolve with it;
if (promise instanceof AsyncResult) {
resolve(promise);
} else if (promise instanceof Error) {
// if given argument is Error then resolving with error;
resolve(new AsyncResult(promise));
} else if (!promise || !promise.then) {
// if given argument is not promise then successfully resolving with this argument;
resolve(new AsyncResult(null, promise));
}
// if given argument is a promise, then trying to resolve or catch it
promise
.then(data => {
let resolvingWith =
data instanceof AsyncResult ? data : new AsyncResult(null, data);
resolve(resolvingWith);
})
.catch(error => {
let resolvingWith =
error instanceof AsyncResult ? error : new AsyncResult(error);
resolve(resolvingWith);
});
});
return newPromise;
}
/**
* Wraps given method, returned result of a method will be instance of AsyncResult
* @param {function} method - any method you wants to wrap
* @param {object} [options] - You may specify context and AsyncResult for new method through options
* @return {function} - wrapped method
*/
function wrapMethod(method, { context, AsyncResult, prototypeMethod, methodName } = {}) {
if (typeof method !== 'function') {
throw new Error('first argument should be a function or context should be provided');
}
!AsyncResult && (AsyncResult = config.AsyncResult);
let asyncMethod = function() {
try {
let result;
if (methodName) {
result = this[methodName].apply(this, arguments);
} else {
result = method.apply(this, arguments);
}
return toAsyncResult(result, AsyncResult);
} catch (error) {
return toAsyncResult(error, AsyncResult);
}
};
if (context) {
asyncMethod = asyncMethod.bind(context);
}
return asyncMethod;
}
/**
* Adds async versions of provided methods
* @param {(Object|Class)} context - object literal or class
* @param {(string|string[])} methodName - method name(s) of provided object.
* In case context is a class and there is no option `static:true` will use prototype
* @param {object} options - options. context, static
* @return {void}
*/
function addAsync(context, methodName, options = {}) {
if (Array.isArray(methodName)) {
for (let x = 0; x < methodName.length; x++) {
addAsync(context, methodName[x], options);
}
} else {
let isCtor = typeof context === 'function';
options.methodName = methodName;
if (isCtor && !options.static) {
context = context.prototype
options.prototypeMethod = true;
}
if (!isCtor && options.context === void 0) {
options.context = context;
}
context[methodName + 'Async'] = wrapMethod(context[methodName], options);
}
}
export { toAsyncResult, wrapMethod, addAsync };