@shutterstock/p-map-iterable 1.0.11 → 1.1.1
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.
- package/README.md +110 -16
- package/dist/src/blocking-queue.js +61 -70
- package/dist/src/blocking-queue.js.map +1 -1
- package/dist/src/blocking-queue.test.js +70 -71
- package/dist/src/blocking-queue.test.js.map +1 -1
- package/dist/src/iterable-mapper.d.ts +173 -30
- package/dist/src/iterable-mapper.d.ts.map +1 -1
- package/dist/src/iterable-mapper.js +255 -141
- package/dist/src/iterable-mapper.js.map +1 -1
- package/dist/src/iterable-mapper.test.js +347 -720
- package/dist/src/iterable-mapper.test.js.map +1 -1
- package/dist/src/iterable-queue-mapper-simple.d.ts +44 -29
- package/dist/src/iterable-queue-mapper-simple.d.ts.map +1 -1
- package/dist/src/iterable-queue-mapper-simple.js +65 -57
- package/dist/src/iterable-queue-mapper-simple.js.map +1 -1
- package/dist/src/iterable-queue-mapper-simple.test.js +34 -35
- package/dist/src/iterable-queue-mapper-simple.test.js.map +1 -1
- package/dist/src/iterable-queue-mapper.d.ts +38 -58
- package/dist/src/iterable-queue-mapper.d.ts.map +1 -1
- package/dist/src/iterable-queue-mapper.js +37 -42
- package/dist/src/iterable-queue-mapper.js.map +1 -1
- package/dist/src/iterable-queue-mapper.test.js +41 -81
- package/dist/src/iterable-queue-mapper.test.js.map +1 -1
- package/dist/src/iterable-queue.js +8 -11
- package/dist/src/iterable-queue.js.map +1 -1
- package/dist/src/iterable-queue.test.js +43 -122
- package/dist/src/iterable-queue.test.js.map +1 -1
- package/package.json +8 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"blocking-queue.test.js","sourceRoot":"","sources":["../../src/blocking-queue.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"blocking-queue.test.js","sourceRoot":"","sources":["../../src/blocking-queue.test.ts"],"names":[],"mappings":";;AAAA,8BAA8B;AAC9B,qDAAiD;AAEjD,Q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AO,GAAkC,EAAE,CAAC;YAClD,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAC9B,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;YAE9B,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;YAE3B,MAAM,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACjC,MAAM,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACjC,MAAM,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;YACzC,MAAM,CAAC,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC;QAC3C,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -3,23 +3,48 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export interface IterableMapperOptions {
|
|
5
5
|
/**
|
|
6
|
-
*
|
|
6
|
+
* Maximum number of concurrent invocations of `mapper` to run at once.
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* The number of concurrent invocations is dynamically adjusted based on the `maxUnread` limit:
|
|
9
|
+
* - If there are no unread items and `maxUnread` is 10 with `concurrency` of 4, all 4 mappers can run.
|
|
10
|
+
* - If there are already 8 unread items in the queue, only 2 mappers will run to avoid exceeding
|
|
11
|
+
* the `maxUnread` limit of 10.
|
|
12
|
+
* - If there are 10 unread items, no mappers will run until an item is consumed from the queue.
|
|
13
|
+
*
|
|
14
|
+
* This ensures efficient processing while maintaining backpressure through the `maxUnread` limit.
|
|
15
|
+
*
|
|
16
|
+
* Setting `concurrency` to 1 enables serial processing, preserving the order of items
|
|
17
|
+
* while still benefiting from the backpressure mechanism.
|
|
18
|
+
*
|
|
19
|
+
* Must be an integer from 1 and up or `Infinity`, and must be <= `maxUnread`.
|
|
9
20
|
*
|
|
10
21
|
* @default 4
|
|
11
22
|
*/
|
|
12
23
|
readonly concurrency?: number;
|
|
13
24
|
/**
|
|
14
|
-
*
|
|
25
|
+
* Maximum number of unread items allowed to accumulate before applying backpressure.
|
|
26
|
+
*
|
|
27
|
+
* This parameter is crucial for controlling memory usage and system load by:
|
|
28
|
+
* 1. Limiting the number of processed but unread items in the queue
|
|
29
|
+
* 2. Automatically pausing mapper execution when the limit is reached
|
|
30
|
+
* 3. Resuming processing when items are consumed, maintaining optimal throughput
|
|
15
31
|
*
|
|
16
|
-
*
|
|
32
|
+
* For example, when reading from a slow database:
|
|
33
|
+
* - With maxUnread=10, only 10 items will be fetched before the consumer reads them
|
|
34
|
+
* - Additional items won't be fetched until the consumer reads existing items
|
|
35
|
+
* - This prevents runaway memory usage for items that cannot be processed quickly enough
|
|
36
|
+
*
|
|
37
|
+
* Must be an integer from 1 and up or `Infinity`, and must be >= `concurrency`.
|
|
38
|
+
* It is not typical to set this value to `Infinity`, but rather to a value such as 1 to 10.
|
|
17
39
|
*
|
|
18
40
|
* @default 8
|
|
19
41
|
*/
|
|
20
42
|
readonly maxUnread?: number;
|
|
21
43
|
/**
|
|
22
|
-
* When set to `false`, instead of stopping when a promise rejects, it will wait for all
|
|
44
|
+
* When set to `false`, instead of stopping when a promise rejects, it will wait for all
|
|
45
|
+
* the promises to settle and then reject with an
|
|
46
|
+
* [aggregated error](https://github.com/sindresorhus/aggregate-error) containing all the
|
|
47
|
+
* errors from the rejected promises.
|
|
23
48
|
*
|
|
24
49
|
* @default true
|
|
25
50
|
*/
|
|
@@ -35,20 +60,150 @@ export interface IterableMapperOptions {
|
|
|
35
60
|
*/
|
|
36
61
|
export type Mapper<Element = unknown, NewElement = unknown> = (element: Element, index: number) => NewElement | Promise<NewElement>;
|
|
37
62
|
/**
|
|
38
|
-
* Iterates over a source iterable with specified concurrency
|
|
63
|
+
* Iterates over a source iterable / generator with specified `concurrency`,
|
|
39
64
|
* calling the `mapper` on each iterated item, and storing the
|
|
40
|
-
* `mapper` result in a queue of
|
|
65
|
+
* `mapper` result in a queue of `maxUnread` size, before
|
|
41
66
|
* being iterated / read by the caller.
|
|
42
67
|
*
|
|
43
68
|
* @remarks
|
|
44
69
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
70
|
+
* ### Typical Use Case
|
|
71
|
+
* - Prefetching items from an async I/O source
|
|
72
|
+
* - In the simple sequential (`concurrency: 1`) case, allows items to be prefetched async, preserving order, while caller processes an item
|
|
73
|
+
* - Can allow parallel prefetches for sources that allow for out of order reads (`concurrency: 2+`)
|
|
74
|
+
* - Prevents the producer from racing ahead of the consumer if `maxUnread` is reached
|
|
75
|
+
*
|
|
76
|
+
* ### Error Handling
|
|
77
|
+
* The mapper should ideally handle all errors internally to enable error handling
|
|
78
|
+
* closest to where they occur. However, if errors do escape the mapper:
|
|
79
|
+
*
|
|
80
|
+
* When `stopOnMapperError` is true (default):
|
|
81
|
+
* - First error immediately stops processing
|
|
82
|
+
* - Error is thrown from the `AsyncIterator`'s next() call
|
|
83
|
+
*
|
|
84
|
+
* When `stopOnMapperError` is false:
|
|
85
|
+
* - Processing continues despite errors
|
|
86
|
+
* - All errors are collected and thrown together
|
|
87
|
+
* - Errors are thrown as `AggregateError` after all items complete
|
|
88
|
+
*
|
|
89
|
+
* ### Usage
|
|
90
|
+
* - Items are exposed to the `mapper` via an iterator or async iterator (this includes generator and async generator functions)
|
|
91
|
+
* - IMPORTANT: `mapper` method not be invoked when `maxUnread` is reached, until items are consumed
|
|
92
|
+
* - The iterable will set `done` when the `input` has indicated `done` and all `mapper` promises have resolved
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
*
|
|
96
|
+
* ### Typical Processing Loop without `IterableMapper`
|
|
97
|
+
*
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const source = new SomeSource();
|
|
100
|
+
* const sourceIds = [1, 2,... 1000];
|
|
101
|
+
* const sink = new SomeSink();
|
|
102
|
+
* for (const sourceId of sourceIds) {
|
|
103
|
+
* const item = await source.read(sourceId); // takes 300 ms of I/O wait, no CPU
|
|
104
|
+
* const outputItem = doSomeOperation(item); // takes 20 ms of CPU
|
|
105
|
+
* await sink.write(outputItem); // takes 500 ms of I/O wait, no CPU
|
|
106
|
+
* }
|
|
107
|
+
* ```
|
|
108
|
+
*
|
|
109
|
+
* Each iteration takes 820ms total, but we waste time waiting for I/O.
|
|
110
|
+
* We could prefetch the next read (300ms) while processing (20ms) and writing (500ms),
|
|
111
|
+
* without changing the order of reads or writes.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
48
114
|
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
115
|
+
* ### Using `IterableMapper` as Prefetcher with Blocking Sequential Writes
|
|
116
|
+
*
|
|
117
|
+
* `concurrency: 1` on the prefetcher preserves the order of the reads and and writes are sequential and blocking (unchanged).
|
|
118
|
+
*
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const source = new SomeSource();
|
|
121
|
+
* const sourceIds = [1, 2,... 1000];
|
|
122
|
+
* // Pre-reads up to 8 items serially and releases in sequential order
|
|
123
|
+
* const sourcePrefetcher = new IterableMapper(sourceIds,
|
|
124
|
+
* async (sourceId) => source.read(sourceId),
|
|
125
|
+
* { concurrency: 1, maxUnread: 10 }
|
|
126
|
+
* );
|
|
127
|
+
* const sink = new SomeSink();
|
|
128
|
+
* for await (const item of sourcePrefetcher) { // may not block for fast sources
|
|
129
|
+
* const outputItem = doSomeOperation(item); // takes 20 ms of CPU
|
|
130
|
+
* await sink.write(outputItem); // takes 500 ms of I/O wait, no CPU
|
|
131
|
+
* }
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* This reduces iteration time to 520ms by overlapping reads with processing/writing.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
*
|
|
138
|
+
* ### Using `IterableMapper` as Prefetcher with Background Sequential Writes with `IterableQueueMapperSimple`
|
|
139
|
+
*
|
|
140
|
+
* `concurrency: 1` on the prefetcher preserves the order of the reads.
|
|
141
|
+
* `concurrency: 1` on the flusher preserves the order of the writes, but allows the loop to iterate while last write is completing.
|
|
142
|
+
*
|
|
143
|
+
* ```typescript
|
|
144
|
+
* const source = new SomeSource();
|
|
145
|
+
* const sourceIds = [1, 2,... 1000];
|
|
146
|
+
* const sourcePrefetcher = new IterableMapper(sourceIds,
|
|
147
|
+
* async (sourceId) => source.read(sourceId),
|
|
148
|
+
* { concurrency: 1, maxUnread: 10 }
|
|
149
|
+
* );
|
|
150
|
+
* const sink = new SomeSink();
|
|
151
|
+
* const flusher = new IterableQueueMapperSimple(
|
|
152
|
+
* async (outputItem) => sink.write(outputItem),
|
|
153
|
+
* { concurrency: 1 }
|
|
154
|
+
* );
|
|
155
|
+
* for await (const item of sourcePrefetcher) { // may not block for fast sources
|
|
156
|
+
* const outputItem = doSomeOperation(item); // takes 20 ms of CPU
|
|
157
|
+
* await flusher.enqueue(outputItem); // will periodically block for portion of write time
|
|
158
|
+
* }
|
|
159
|
+
* // Wait for all writes to complete
|
|
160
|
+
* await flusher.onIdle();
|
|
161
|
+
* // Check for errors
|
|
162
|
+
* if (flusher.errors.length > 0) {
|
|
163
|
+
* // ...
|
|
164
|
+
* }
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* This reduces iteration time to about `max((max(readTime, writeTime) - cpuOpTime, cpuOpTime))`
|
|
168
|
+
* by overlapping reads and writes with the CPU processing step.
|
|
169
|
+
* In this contrived example, the loop time is reduced to 500ms - 20ms = 480ms.
|
|
170
|
+
* In cases where the CPU usage time is higher, the impact can be greater.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
*
|
|
174
|
+
* ### Using `IterableMapper` as Prefetcher with Out of Order Reads and Background Out of Order Writes with `IterableQueueMapperSimple`
|
|
175
|
+
*
|
|
176
|
+
* For maximum throughput, allow out of order reads and writes with
|
|
177
|
+
* `IterableQueueMapper` (to iterate results with backpressure when too many unread items) or
|
|
178
|
+
* `IterableQueueMapperSimple` (to handle errors at end without custom iteration and applying backpressure to block further enqueues when `concurrency` items are in process):
|
|
179
|
+
*
|
|
180
|
+
* ```typescript
|
|
181
|
+
* const source = new SomeSource();
|
|
182
|
+
* const sourceIds = [1, 2,... 1000];
|
|
183
|
+
* const sourcePrefetcher = new IterableMapper(sourceIds,
|
|
184
|
+
* async (sourceId) => source.read(sourceId),
|
|
185
|
+
* { concurrency: 10, maxUnread: 20 }
|
|
186
|
+
* );
|
|
187
|
+
* const sink = new SomeSink();
|
|
188
|
+
* const flusher = new IterableQueueMapperSimple(
|
|
189
|
+
* async (outputItem) => sink.write(outputItem),
|
|
190
|
+
* { concurrency: 10 }
|
|
191
|
+
* );
|
|
192
|
+
* for await (const item of sourcePrefetcher) { // typically will not block
|
|
193
|
+
* const outputItem = doSomeOperation(item); // takes 20 ms of CPU
|
|
194
|
+
* await flusher.enqueue(outputItem); // typically will not block
|
|
195
|
+
* }
|
|
196
|
+
* // Wait for all writes to complete
|
|
197
|
+
* await flusher.onIdle();
|
|
198
|
+
* // Check for errors
|
|
199
|
+
* if (flusher.errors.length > 0) {
|
|
200
|
+
* // ...
|
|
201
|
+
* }
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* This reduces iteration time to about 20ms by overlapping reads and writes with the CPU processing step.
|
|
205
|
+
* In this contrived (but common) example we would get a 41x improvement in throughput, removing 97.5% of
|
|
206
|
+
* the time to process each item and fully utilizing the CPU time available in the JS event loop.
|
|
52
207
|
*
|
|
53
208
|
* @category Iterable Input
|
|
54
209
|
*/
|
|
@@ -68,23 +223,11 @@ export declare class IterableMapper<Element, NewElement> implements AsyncIterabl
|
|
|
68
223
|
/**
|
|
69
224
|
* Create a new `IterableMapper`
|
|
70
225
|
*
|
|
71
|
-
* @param input Iterated over concurrently in the `mapper` function.
|
|
72
|
-
* @param mapper Function
|
|
73
|
-
* Expected to return a `Promise` or value.
|
|
74
|
-
*
|
|
75
|
-
* The `mapper` *should* handle all errors and not allow an error to be thrown
|
|
76
|
-
* out of the `mapper` function as this enables the best handling of errors
|
|
77
|
-
* closest to the time that they occur.
|
|
78
|
-
*
|
|
79
|
-
* If the `mapper` function does allow an error to be thrown then the
|
|
80
|
-
* `stopOnMapperError` option controls the behavior:
|
|
81
|
-
* - `stopOnMapperError`: `true` - will throw the error
|
|
82
|
-
* out of `next` or the `AsyncIterator` returned from `[Symbol.asyncIterator]`
|
|
83
|
-
* and stop processing.
|
|
84
|
-
* - `stopOnMapperError`: `false` - will continue processing
|
|
85
|
-
* and accumulate the errors to be thrown from `next` or the `AsyncIterator`
|
|
86
|
-
* returned from `[Symbol.asyncIterator]` when all items have been processed.
|
|
226
|
+
* @param input Iterated over concurrently, or serially, in the `mapper` function.
|
|
227
|
+
* @param mapper Function called for every item in `input`. Returns a `Promise` or value.
|
|
87
228
|
* @param options IterableMapper options
|
|
229
|
+
*
|
|
230
|
+
* @see {@link IterableQueueMapper} for full class documentation
|
|
88
231
|
*/
|
|
89
232
|
constructor(input: AsyncIterable<Element> | Iterable<Element>, mapper: Mapper<Element, NewElement>, options?: IterableMapperOptions);
|
|
90
233
|
[Symbol.asyncIterator](): AsyncIterator<NewElement>;
|
|
@@ -106,7 +249,7 @@ export declare class IterableMapper<Element, NewElement> implements AsyncIterabl
|
|
|
106
249
|
*/
|
|
107
250
|
private throwIfError;
|
|
108
251
|
/**
|
|
109
|
-
* Get the next item from the
|
|
252
|
+
* Get the next item from the `input` iterable.
|
|
110
253
|
*
|
|
111
254
|
* @remarks
|
|
112
255
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"iterable-mapper.d.ts","sourceRoot":"","sources":["../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