-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfiniteApplication.js
More file actions
48 lines (39 loc) · 1.23 KB
/
Copy pathinfiniteApplication.js
File metadata and controls
48 lines (39 loc) · 1.23 KB
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
function infiniteApplication(fn, useConfigForArgs, ...initialArgs) {
if (typeof fn !== 'function') {
throw new Error('infiniteApplication expects to be called with a function as the first argument.');
}
if (typeof useConfigForArgs === 'undefined') {
useConfigForArgs = false;
}
if (typeof useConfigForArgs !== 'boolean') {
throw new Error('infiniteApplication expects that a second argument, if present, be a boolean.');
}
let cachedArgs;
if (useConfigForArgs) {
cachedArgs = Object.assign({}, ...initialArgs);
} else {
cachedArgs = [...initialArgs];
}
const infiniteApplicationWrappedFunction = function () {
if (arguments.length === 0) {
if (useConfigForArgs) {
return fn.call(null, cachedArgs);
} else {
return fn.apply(null, cachedArgs);
}
}
if (useConfigForArgs) {
for (const arg of arguments) {
if(typeof arg !== 'object' || arg === null) {
throw new Error('infiniteApplication expects objects as subsequent args when using `useConfigForArgs` mode');
}
}
Object.assign(cachedArgs, ...arguments);
} else {
cachedArgs.push(...arguments);
}
return infiniteApplicationWrappedFunction;
};
return infiniteApplicationWrappedFunction;
}
export default infiniteApplication;