-
Notifications
You must be signed in to change notification settings - Fork 132
/
Func.jsx
80 lines (70 loc) · 1.8 KB
/
Func.jsx
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
import $ from 'jquery';
import _ from 'underscore';
/**
* Invokes the given callback with the given arguments
*
* @param {Function} callback
* @param {Array} [args]
* @param {Object} [scope]
*
* @returns {Mixed}
*/
function invoke(callback, args, scope) {
if (!_(callback).isFunction()) {
return null;
}
return callback.apply(scope, args || []);
}
/**
* This is nearly the same as invoke, except that it assumes that the callback returns a promise. The callback doesn't
* HAVE to return a promise, but it CAN, and if it doesn't, then we provide a promise
*
* @param {Function} callback
* @param {Array} [args]
* @param {Object} [scope]
*
* @returns {$.Deferred}
*/
function invokeAsync(callback, args, scope) {
if (!_(callback).isFunction()) {
return new $.Deferred().resolve();
}
let promiseFromCallback = callback.apply(scope, args || []);
// If there was not a promise returned from the prefetch callback, then create a dummy promise and resolve it
if (!promiseFromCallback) {
promiseFromCallback = new $.Deferred().resolve();
}
return promiseFromCallback;
}
/**
* Invokes all the given callbacks with the given arguments
*
* @param {Function[]} callbacks
* @param {Array} [args]
*/
function bulkInvoke(callbacks, args) {
callbacks.forEach(callback => invoke(callback, args));
}
/**
* Throws an uncaught error in an attempt to stop JS execution
*/
function die() {
throw new Error('Aborting JavaScript execution');
}
/**
* Call the method on a list of objects
*
* @param {object|array} list
* @param {string} methodName
* @returns {Array}
*/
function mapByName(list, methodName) {
return _.map(list, item => item[methodName].call(item));
}
export {
invoke,
invokeAsync,
bulkInvoke,
die,
mapByName,
};