dce-reactkit 3.1.5 → 3.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Run tasks in parallel with a limit on how many tasks can execute at once.
3
+ * No guarantees are made about the order of task execution
4
+ * @author Gabe Abrams
5
+ * @param taskFunctions functions that start asynchronous tasks and optionally
6
+ * resolve with values
7
+ * @param [limit=no limit] maximum number of asynchronous tasks to permit to run at
8
+ * once
9
+ * @returns array of resolved values in the same order as the task functions
10
+ */
11
+ const parallelLimit = async (
12
+ taskFunctions: (() => Promise<any>)[],
13
+ limit?: number,
14
+ ): Promise<any[]> => {
15
+ const results: any[] = [];
16
+
17
+ // Wait until finished with all tasks
18
+ await new Promise<void>((resolve) => {
19
+ /* ------------- Helpers ------------ */
20
+
21
+ let nextTaskIndex = 0;
22
+ let numFinishedTasks = 0;
23
+ /**
24
+ * Start the next task
25
+ * @author Gabe Abrams
26
+ */
27
+ const startTask = async () => {
28
+ const taskIndex = nextTaskIndex++;
29
+
30
+ // Get the task
31
+ const taskFunction = taskFunctions[taskIndex];
32
+ if (!taskFunction) {
33
+ return;
34
+ }
35
+
36
+ // Execute task
37
+ const result = await taskFunction();
38
+
39
+ // Add results
40
+ results[taskIndex] = result;
41
+
42
+ // Tally and finish
43
+ if (++numFinishedTasks === taskFunctions.length) {
44
+ return resolve();
45
+ }
46
+
47
+ // Not finished! Start another task
48
+ startTask();
49
+ };
50
+
51
+ /* ----------- Start Tasks ---------- */
52
+
53
+ // If no limit, start all tasks. At least start 1 task
54
+ const numTasks = Math.max((limit || taskFunctions.length), 1);
55
+ for (let i = 0; i < numTasks; i++) {
56
+ startTask();
57
+ }
58
+ });
59
+
60
+ return results;
61
+ };
62
+
63
+ export default parallelLimit;
package/src/index.ts CHANGED
@@ -47,6 +47,7 @@ import getHumanReadableDate from './helpers/getHumanReadableDate';
47
47
  import getPartOfDay from './helpers/getPartOfDay';
48
48
  import stringsToHumanReadableList from './helpers/stringsToHumanReadableList';
49
49
  import onlyKeepLetters from './helpers/onlyKeepLetters';
50
+ import parallelLimit from './helpers/parallelLimit';
50
51
 
51
52
  // Import types
52
53
  import ModalButtonType from './types/ModalButtonType';
@@ -104,6 +105,7 @@ export {
104
105
  getPartOfDay,
105
106
  stringsToHumanReadableList,
106
107
  onlyKeepLetters,
108
+ parallelLimit,
107
109
  // Client helpers
108
110
  visitServerEndpoint,
109
111
  // Server helpers