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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191 | 1x
1x
1x
1x
71x
71x
1x
19x
19x
19x
19x
1x
18x
18x
18x
81x
81x
78x
56x
56x
56x
56x
22x
22x
22x
22x
7x
7x
15x
3x
3x
18x
75x
8x
67x
67x
5x
67x
11x
56x
26x
18x
86x
85x
81x
4x
4x
4x
18x
60x
1x
8x
8x
4x
4x
2x
2x
4x
4x
1x
4x
4x
1x
| const DEFAULT_MAX = 5;
export interface Result<T> {
amountDone: number;
amountStarted: number;
amountResolved: number;
amountRejected: number;
amountNextCheckFalsey: number;
rejectedIndexes: number[];
resolvedIndexes: number[];
nextCheckFalseyIndexes: number[];
taskResults: T[];
}
export interface Options {
maxInProgress?: number;
failFast?: boolean;
progressCallback?: <T>(result: Result<T>) => void;
nextCheck?: nextTaskCheck;
}
/**
* Default checker which validates if a next task should begin.
* This can be overwritten to write own checks for example checking the amount
* of used ram and waiting till the ram is low enough for a next task.
*
* It should always resolve with a boolean, either `true` to start a next task
* or `false` to stop executing a new task.
*
* If this method rejects, the error will propagate to the caller
* @param status
* @param tasks
* @returns {Promise}
*/
const defaultNextTaskCheck: nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>): Promise<boolean> => {
return new Promise((resolve, reject) => {
resolve(status.amountStarted < tasks.length);
});
};
const DEFAULT_OPTIONS = {
maxInProgress: DEFAULT_MAX,
failFast: false,
nextCheck: defaultNextTaskCheck
};
export type Task<T> = () => Promise<T>;
export type Tasks<T> = Array<Task<T>>;
export type nextTaskCheck = <T>(status: Result<T>, tasks: Tasks<T>) => Promise<boolean>;
/**
* Raw throttle function, which can return extra meta data.
* @param tasks required array of tasks to be executed
* @param options Options object
* @returns {Promise}
*/
export function raw<T>(tasks: Tasks<T>, options?: Options): Promise<Result<T>> {
return new Promise((resolve, reject) => {
const myOptions = Object.assign({}, DEFAULT_OPTIONS, options);
const result: Result<T> = {
amountDone: 0,
amountStarted: 0,
amountResolved: 0,
amountRejected: 0,
amountNextCheckFalsey: 0,
rejectedIndexes: [],
resolvedIndexes: [],
nextCheckFalseyIndexes: [],
taskResults: []
};
if (tasks.length === 0) {
return resolve(result);
}
let failedFast = false;
let currentTaskIndex = 0;
const executeTask = (index: number) => {
result.amountStarted++;
if (typeof tasks[index] === 'function') {
tasks[index]().then(
taskResult => {
result.taskResults[index] = taskResult;
result.resolvedIndexes.push(index);
result.amountResolved++;
taskDone();
},
error => {
result.taskResults[index] = error;
result.rejectedIndexes.push(index);
result.amountRejected++;
if (myOptions.failFast === true) {
failedFast = true;
return reject(result);
}
taskDone();
}
);
} else {
failedFast = true;
return reject(
new Error('tasks[' + index + ']: ' + tasks[index] + ', is supposed to be of type function')
);
}
};
const taskDone = () => {
//make sure no more tasks are spawned when we failedFast
if (failedFast === true) {
return;
}
result.amountDone++;
if (typeof (myOptions as Options).progressCallback === 'function') {
(myOptions as any).progressCallback(result);
}
if (result.amountDone === tasks.length) {
return resolve(result);
}
if (currentTaskIndex < tasks.length) {
nextTask(currentTaskIndex++);
}
};
const nextTask = (index: number) => {
//check if we can execute the next task
myOptions.nextCheck(result, tasks).then(canExecuteNextTask => {
if (canExecuteNextTask === true) {
//execute it
executeTask(index);
} else {
result.amountNextCheckFalsey++;
result.nextCheckFalseyIndexes.push(index);
taskDone();
}
}, reject);
};
//spawn the first X task
for (let i = 0; i < Math.min(myOptions.maxInProgress, tasks.length); i++) {
nextTask(currentTaskIndex++);
}
});
}
/**
* Executes the raw function, but only return the task array
* @param tasks
* @param options
* @returns {Promise}
*/
function executeRaw<T>(tasks: Tasks<T>, options: Options): Promise<T[]> {
return new Promise((resolve, reject) => {
raw(tasks, options).then(
(result: Result<T>) => {
resolve(result.taskResults);
},
(error: Error | Result<T>) => {
if (error instanceof Error) {
reject(error);
} else {
reject(error.taskResults[error.rejectedIndexes[0]]);
}
}
);
});
}
/**
* Simply run all the promises after each other, so in synchronous manner
* @param tasks required array of tasks to be executed
* @param options Options object
* @returns {Promise}
*/
export function sync<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {
const myOptions = Object.assign({}, { maxInProgress: 1, failFast: true }, options);
return executeRaw(tasks, myOptions);
}
/**
* Exposes the same behaviour as Promise.All(), but throttled!
* @param tasks required array of tasks to be executed
* @param options Options object
* @returns {Promise}
*/
export function all<T>(tasks: Tasks<T>, options?: Options): Promise<T[]> {
const myOptions = Object.assign({}, { failFast: true }, options);
return executeRaw(tasks, myOptions);
}
|