@arcgis/toolkit 5.1.0-next.77 → 5.1.0-next.79

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.
@@ -11,6 +11,20 @@ class Deferred {
11
11
  });
12
12
  }
13
13
  }
14
+ async function forEachWithConcurrency(items, concurrency, callback) {
15
+ if (items.length === 0) {
16
+ return;
17
+ }
18
+ let index = 0;
19
+ async function consumeItems() {
20
+ while (index < items.length) {
21
+ const item = items[index];
22
+ index += 1;
23
+ await callback(item);
24
+ }
25
+ }
26
+ await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, async () => await consumeItems()));
27
+ }
14
28
  const devToolsAwareTimeout = (callback, timeout) => {
15
29
  const interval = timeout > longTimeoutThreshold ? longTimeoutInterval : timeout / shortTimeoutIntervals;
16
30
  let elapsed = 0;
@@ -28,3 +42,4 @@ const longTimeoutInterval = 2e3;
28
42
  const shortTimeoutIntervals = 4;
29
43
  exports.Deferred = Deferred;
30
44
  exports.devToolsAwareTimeout = devToolsAwareTimeout;
45
+ exports.forEachWithConcurrency = forEachWithConcurrency;
@@ -37,6 +37,20 @@ export interface Deferred<T> {
37
37
  */
38
38
  reject(_error: unknown): void;
39
39
  }
40
+ /**
41
+ * Execute async callbacks against a list with a fixed number of in-flight
42
+ * operations.
43
+ * Useful for rate-limiting or to avoid overwhelming resources with too many concurrent operations.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const items = [...lotsOfItems];
48
+ * await forEachWithConcurrency(items, 5, async (item) => {
49
+ * await doSomethingAsync(item);
50
+ * });
51
+ * ```
52
+ */
53
+ export declare function forEachWithConcurrency<T>(items: readonly T[], concurrency: number, callback: (item: T) => Promise<void>): Promise<void>;
40
54
  /**
41
55
  * Like setTimeout(), but does not advance the clock if the program is
42
56
  * stopped on a debugger breakpoint.
@@ -37,6 +37,20 @@ export interface Deferred<T> {
37
37
  */
38
38
  reject(_error: unknown): void;
39
39
  }
40
+ /**
41
+ * Execute async callbacks against a list with a fixed number of in-flight
42
+ * operations.
43
+ * Useful for rate-limiting or to avoid overwhelming resources with too many concurrent operations.
44
+ *
45
+ * @example
46
+ * ```ts
47
+ * const items = [...lotsOfItems];
48
+ * await forEachWithConcurrency(items, 5, async (item) => {
49
+ * await doSomethingAsync(item);
50
+ * });
51
+ * ```
52
+ */
53
+ export declare function forEachWithConcurrency<T>(items: readonly T[], concurrency: number, callback: (item: T) => Promise<void>): Promise<void>;
40
54
  /**
41
55
  * Like setTimeout(), but does not advance the clock if the program is
42
56
  * stopped on a debugger breakpoint.
@@ -9,6 +9,20 @@ class Deferred {
9
9
  });
10
10
  }
11
11
  }
12
+ async function forEachWithConcurrency(items, concurrency, callback) {
13
+ if (items.length === 0) {
14
+ return;
15
+ }
16
+ let index = 0;
17
+ async function consumeItems() {
18
+ while (index < items.length) {
19
+ const item = items[index];
20
+ index += 1;
21
+ await callback(item);
22
+ }
23
+ }
24
+ await Promise.all(Array.from({ length: Math.min(concurrency, items.length) }, async () => await consumeItems()));
25
+ }
12
26
  const devToolsAwareTimeout = (callback, timeout) => {
13
27
  const interval = timeout > longTimeoutThreshold ? longTimeoutInterval : timeout / shortTimeoutIntervals;
14
28
  let elapsed = 0;
@@ -26,5 +40,6 @@ const longTimeoutInterval = 2e3;
26
40
  const shortTimeoutIntervals = 4;
27
41
  export {
28
42
  Deferred,
29
- devToolsAwareTimeout
43
+ devToolsAwareTimeout,
44
+ forEachWithConcurrency
30
45
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcgis/toolkit",
3
- "version": "5.1.0-next.77",
3
+ "version": "5.1.0-next.79",
4
4
  "description": "Collection of common internal patterns and utilities for ArcGIS Maps SDK for JavaScript components.",
5
5
  "homepage": "https://developers.arcgis.com/javascript/latest/",
6
6
  "sideEffects": false,