@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.
@@ -1,32 +1,27 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
4
  /// <reference types="jest" />
6
5
  const iterable_mapper_1 = require("./iterable-mapper");
7
6
  const util_1 = require("util");
8
7
  const sleep = (0, util_1.promisify)(setTimeout);
9
- function mapper({ value, ms }) {
10
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
11
- yield sleep(ms);
12
- return value;
13
- });
8
+ async function mapper({ value, ms }) {
9
+ await sleep(ms);
10
+ return value;
14
11
  }
15
12
  class SleepIterator {
16
13
  constructor(max) {
17
14
  this._current = 1;
18
15
  this._max = max;
19
16
  }
20
- [Symbol.asyncIterator]() {
21
- return tslib_1.__asyncGenerator(this, arguments, function* _a() {
22
- for (let i = 0; i < this._max; i++) {
23
- yield tslib_1.__await(sleep(1 * (i % 10)));
24
- if (this._current <= this._max) {
25
- yield yield tslib_1.__await(this._current);
26
- }
27
- this._current++;
17
+ async *[Symbol.asyncIterator]() {
18
+ for (let i = 0; i < this._max; i++) {
19
+ await sleep(1 * (i % 10));
20
+ if (this._current <= this._max) {
21
+ yield this._current;
28
22
  }
29
- });
23
+ this._current++;
24
+ }
30
25
  }
31
26
  }
32
27
  class ThrowingIterator {
@@ -41,16 +36,14 @@ class ThrowingIterator {
41
36
  const delayMS = this._delayMS;
42
37
  const throwOnIndex = this._throwOnIndex;
43
38
  return {
44
- next() {
45
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
46
- yield sleep(delayMS);
47
- if (index === throwOnIndex) {
48
- throw new Error(`throwing on index ${index}`);
49
- }
50
- const item = { value: index, done: index === max };
51
- index++;
52
- return item;
53
- });
39
+ async next() {
40
+ await sleep(delayMS);
41
+ if (index === throwOnIndex) {
42
+ throw new Error(`throwing on index ${index}`);
43
+ }
44
+ const item = { value: index, done: index === max };
45
+ index++;
46
+ return item;
54
47
  },
55
48
  };
56
49
  }
@@ -65,8 +58,7 @@ describe('IterableMapper', () => {
65
58
  jest.clearAllMocks();
66
59
  });
67
60
  describe('simple tests', () => {
68
- it('simple mapper works - sync mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
69
- var _a, e_1, _b, _c;
61
+ it('simple mapper works - sync mapper', async () => {
70
62
  const max = 100;
71
63
  const iterator = new SleepIterator(max);
72
64
  let total = 0;
@@ -78,78 +70,52 @@ describe('IterableMapper', () => {
78
70
  });
79
71
  let lastTotal = 0;
80
72
  let loopCount = 0;
81
- try {
82
- for (var _d = true, prefetcher_1 = tslib_1.__asyncValues(prefetcher), prefetcher_1_1; prefetcher_1_1 = yield prefetcher_1.next(), _a = prefetcher_1_1.done, !_a; _d = true) {
83
- _c = prefetcher_1_1.value;
84
- _d = false;
85
- const item = _c;
86
- loopCount++;
87
- if (item > lastTotal) {
88
- lastTotal = item;
89
- }
90
- }
91
- }
92
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
93
- finally {
94
- try {
95
- if (!_d && !_a && (_b = prefetcher_1.return)) yield _b.call(prefetcher_1);
73
+ for await (const item of prefetcher) {
74
+ loopCount++;
75
+ if (item > lastTotal) {
76
+ lastTotal = item;
96
77
  }
97
- finally { if (e_1) throw e_1.error; }
98
78
  }
99
79
  expect(callCount).toBe(max);
100
80
  expect(loopCount).toBe(max);
101
81
  expect(total).toBe((max * (max + 1)) / 2);
102
82
  expect(lastTotal).toBe((max * (max + 1)) / 2);
103
- }));
104
- it('simple mapper works - async mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
105
- var _e, e_2, _f, _g;
83
+ });
84
+ it('simple mapper works - async mapper', async () => {
106
85
  const max = 100;
107
86
  const iterator = new SleepIterator(max);
108
87
  let total = 0;
109
88
  let callCount = 0;
110
- const prefetcher = new iterable_mapper_1.IterableMapper(iterator, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
89
+ const prefetcher = new iterable_mapper_1.IterableMapper(iterator, async (value) => {
111
90
  callCount++;
112
91
  total += value;
113
- yield sleep(10 * (callCount % 6));
92
+ await sleep(10 * (callCount % 6));
114
93
  return total;
115
- }));
94
+ });
116
95
  let lastTotal = 0;
117
96
  let loopCount = 0;
118
- try {
119
- for (var _h = true, prefetcher_2 = tslib_1.__asyncValues(prefetcher), prefetcher_2_1; prefetcher_2_1 = yield prefetcher_2.next(), _e = prefetcher_2_1.done, !_e; _h = true) {
120
- _g = prefetcher_2_1.value;
121
- _h = false;
122
- const item = _g;
123
- loopCount++;
124
- if (item > lastTotal) {
125
- lastTotal = item;
126
- }
127
- }
128
- }
129
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
130
- finally {
131
- try {
132
- if (!_h && !_e && (_f = prefetcher_2.return)) yield _f.call(prefetcher_2);
97
+ for await (const item of prefetcher) {
98
+ loopCount++;
99
+ if (item > lastTotal) {
100
+ lastTotal = item;
133
101
  }
134
- finally { if (e_2) throw e_2.error; }
135
102
  }
136
103
  expect(callCount).toBe(max);
137
104
  expect(loopCount).toBe(max);
138
105
  expect(total).toBe((max * (max + 1)) / 2);
139
106
  expect(lastTotal).toBe((max * (max + 1)) / 2);
140
- }));
141
- it('async mapper works - parallel next() calls', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
142
- var _j, e_3, _k, _l;
107
+ });
108
+ it('async mapper works - parallel next() calls', async () => {
143
109
  const max = 100;
144
110
  const inputIterator = new SleepIterator(max);
145
111
  let total = 0;
146
112
  let callCount = 0;
147
- const prefetcher = new iterable_mapper_1.IterableMapper(inputIterator, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
113
+ const prefetcher = new iterable_mapper_1.IterableMapper(inputIterator, async (value) => {
148
114
  callCount++;
149
115
  total += value;
150
- yield sleep(10 * (callCount % 6));
116
+ await sleep(10 * (callCount % 6));
151
117
  return total;
152
- }));
118
+ });
153
119
  let lastTotal = 0;
154
120
  let loopCount = 0;
155
121
  const nextPromises = [];
@@ -158,35 +124,22 @@ describe('IterableMapper', () => {
158
124
  nextPromises.push(iterator.next());
159
125
  }
160
126
  // Wait for all the readers to resolve
161
- yield Promise.all(nextPromises);
162
- try {
163
- for (var _m = true, nextPromises_1 = tslib_1.__asyncValues(nextPromises), nextPromises_1_1; nextPromises_1_1 = yield nextPromises_1.next(), _j = nextPromises_1_1.done, !_j; _m = true) {
164
- _l = nextPromises_1_1.value;
165
- _m = false;
166
- const item = _l;
167
- if (item.done === true) {
168
- break;
169
- }
170
- loopCount++;
171
- if (item.value > lastTotal) {
172
- lastTotal = item.value;
173
- }
127
+ await Promise.all(nextPromises);
128
+ for await (const item of nextPromises) {
129
+ if (item.done === true) {
130
+ break;
174
131
  }
175
- }
176
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
177
- finally {
178
- try {
179
- if (!_m && !_j && (_k = nextPromises_1.return)) yield _k.call(nextPromises_1);
132
+ loopCount++;
133
+ if (item.value > lastTotal) {
134
+ lastTotal = item.value;
180
135
  }
181
- finally { if (e_3) throw e_3.error; }
182
136
  }
183
137
  expect(callCount).toBe(max);
184
138
  expect(loopCount).toBe(max);
185
139
  expect(total).toBe((max * (max + 1)) / 2);
186
140
  expect(lastTotal).toBe((max * (max + 1)) / 2);
187
- }));
188
- it('right number run in parallel - simple', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
189
- var _o, e_4, _p, _q;
141
+ });
142
+ it('right number run in parallel - simple', async () => {
190
143
  const startTime = Date.now();
191
144
  const max = 8;
192
145
  const delayBetweenMs = 200;
@@ -203,24 +156,12 @@ describe('IterableMapper', () => {
203
156
  const prefetcher = new iterable_mapper_1.IterableMapper(input, mapper, { concurrency: 2, maxUnread: 4 });
204
157
  let lastSeen = 0;
205
158
  let loopCount = 0;
206
- try {
207
- for (var _r = true, prefetcher_3 = tslib_1.__asyncValues(prefetcher), prefetcher_3_1; prefetcher_3_1 = yield prefetcher_3.next(), _o = prefetcher_3_1.done, !_o; _r = true) {
208
- _q = prefetcher_3_1.value;
209
- _r = false;
210
- const item = _q;
211
- loopCount++;
212
- if (item > lastSeen) {
213
- lastSeen = item;
214
- }
159
+ for await (const item of prefetcher) {
160
+ loopCount++;
161
+ if (item > lastSeen) {
162
+ lastSeen = item;
215
163
  }
216
164
  }
217
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
218
- finally {
219
- try {
220
- if (!_r && !_o && (_p = prefetcher_3.return)) yield _p.call(prefetcher_3);
221
- }
222
- finally { if (e_4) throw e_4.error; }
223
- }
224
165
  expect(loopCount).toBe(max);
225
166
  expect(lastSeen).toBe(max);
226
167
  // Should require at least 2 batches
@@ -228,7 +169,7 @@ describe('IterableMapper', () => {
228
169
  expect(Date.now() - startTime).toBeGreaterThan(4 * delayBetweenMs);
229
170
  // The runners should never stop because the items are consumed immediately
230
171
  expect(startAnotherRunnerSpy).toHaveBeenCalledTimes(0);
231
- }));
172
+ });
232
173
  // T0 - Mapping - Start 1, 2, waiting 200 ms
233
174
  // Q: []
234
175
  // Runners - 2 running
@@ -277,8 +218,7 @@ describe('IterableMapper', () => {
277
218
  // Q: []
278
219
  // T2300 - Iterate - Finished 7
279
220
  // Q: []
280
- it('right number run in parallel - delay in reading', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
281
- var _s, e_5, _t, _u;
221
+ it('right number run in parallel - delay in reading', async () => {
282
222
  const startTime = Date.now();
283
223
  const max = 7;
284
224
  const mapDelayMs = 200;
@@ -296,24 +236,12 @@ describe('IterableMapper', () => {
296
236
  const prefetcher = new iterable_mapper_1.IterableMapper(input, mapper, { concurrency: 2, maxUnread: 4 });
297
237
  let lastSeen = 0;
298
238
  let loopCount = 0;
299
- try {
300
- for (var _v = true, prefetcher_4 = tslib_1.__asyncValues(prefetcher), prefetcher_4_1; prefetcher_4_1 = yield prefetcher_4.next(), _s = prefetcher_4_1.done, !_s; _v = true) {
301
- _u = prefetcher_4_1.value;
302
- _v = false;
303
- const item = _u;
304
- loopCount++;
305
- if (item > lastSeen) {
306
- lastSeen = item;
307
- }
308
- yield sleep(readDelayMs);
239
+ for await (const item of prefetcher) {
240
+ loopCount++;
241
+ if (item > lastSeen) {
242
+ lastSeen = item;
309
243
  }
310
- }
311
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
312
- finally {
313
- try {
314
- if (!_v && !_s && (_t = prefetcher_4.return)) yield _t.call(prefetcher_4);
315
- }
316
- finally { if (e_5) throw e_5.error; }
244
+ await sleep(readDelayMs);
317
245
  }
318
246
  expect(loopCount).toBe(max);
319
247
  expect(lastSeen).toBe(max);
@@ -326,11 +254,10 @@ describe('IterableMapper', () => {
326
254
  expect(Date.now() - startTime).toBeGreaterThan(mapDelayMs + max * readDelayMs);
327
255
  expect(sourceNextSpy).toHaveBeenCalledTimes(max + 1);
328
256
  expect(startAnotherRunnerSpy).toHaveBeenCalledTimes(3);
329
- }));
257
+ });
330
258
  });
331
259
  describe('empty iterables', () => {
332
- it('empty sync iterable works - sync mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
333
- var _a, e_6, _b, _c;
260
+ it('empty sync iterable works - sync mapper', async () => {
334
261
  let total = 0;
335
262
  let callCount = 0;
336
263
  const prefetcher = new iterable_mapper_1.IterableMapper([], (value) => {
@@ -340,77 +267,49 @@ describe('IterableMapper', () => {
340
267
  });
341
268
  let lastTotal = 0;
342
269
  let loopCount = 0;
343
- try {
344
- for (var _d = true, prefetcher_5 = tslib_1.__asyncValues(prefetcher), prefetcher_5_1; prefetcher_5_1 = yield prefetcher_5.next(), _a = prefetcher_5_1.done, !_a; _d = true) {
345
- _c = prefetcher_5_1.value;
346
- _d = false;
347
- const item = _c;
348
- loopCount++;
349
- if (item > lastTotal) {
350
- lastTotal = item;
351
- }
270
+ for await (const item of prefetcher) {
271
+ loopCount++;
272
+ if (item > lastTotal) {
273
+ lastTotal = item;
352
274
  }
353
275
  }
354
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
355
- finally {
356
- try {
357
- if (!_d && !_a && (_b = prefetcher_5.return)) yield _b.call(prefetcher_5);
358
- }
359
- finally { if (e_6) throw e_6.error; }
360
- }
361
276
  expect(callCount).toBe(0);
362
277
  expect(loopCount).toBe(0);
363
278
  expect(total).toBe(0);
364
279
  expect(lastTotal).toBe(0);
365
- }));
366
- it('empty sync iterable works - async mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
367
- var _e, e_7, _f, _g;
280
+ });
281
+ it('empty sync iterable works - async mapper', async () => {
368
282
  let total = 0;
369
283
  let callCount = 0;
370
- const prefetcher = new iterable_mapper_1.IterableMapper([], (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
371
- yield sleep(10);
284
+ const prefetcher = new iterable_mapper_1.IterableMapper([], async (value) => {
285
+ await sleep(10);
372
286
  callCount++;
373
287
  total += value;
374
288
  return total;
375
- }));
289
+ });
376
290
  let lastTotal = 0;
377
291
  let loopCount = 0;
378
- try {
379
- for (var _h = true, prefetcher_6 = tslib_1.__asyncValues(prefetcher), prefetcher_6_1; prefetcher_6_1 = yield prefetcher_6.next(), _e = prefetcher_6_1.done, !_e; _h = true) {
380
- _g = prefetcher_6_1.value;
381
- _h = false;
382
- const item = _g;
383
- loopCount++;
384
- if (item > lastTotal) {
385
- lastTotal = item;
386
- }
387
- }
388
- }
389
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
390
- finally {
391
- try {
392
- if (!_h && !_e && (_f = prefetcher_6.return)) yield _f.call(prefetcher_6);
292
+ for await (const item of prefetcher) {
293
+ loopCount++;
294
+ if (item > lastTotal) {
295
+ lastTotal = item;
393
296
  }
394
- finally { if (e_7) throw e_7.error; }
395
297
  }
396
298
  expect(callCount).toBe(0);
397
299
  expect(loopCount).toBe(0);
398
300
  expect(total).toBe(0);
399
301
  expect(lastTotal).toBe(0);
400
- }));
401
- it('empty async iterable works - sync mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
402
- var _j, e_8, _k, _l;
302
+ });
303
+ it('empty async iterable works - sync mapper', async () => {
403
304
  let total = 0;
404
305
  let callCount = 0;
405
306
  const prefetcher = new iterable_mapper_1.IterableMapper((() => {
406
307
  return {
407
308
  [Symbol.asyncIterator]() {
408
309
  return {
409
- next() {
410
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
411
- yield sleep(100);
412
- return { value: undefined, done: true };
413
- });
310
+ async next() {
311
+ await sleep(100);
312
+ return { value: undefined, done: true };
414
313
  },
415
314
  };
416
315
  },
@@ -422,234 +321,183 @@ describe('IterableMapper', () => {
422
321
  });
423
322
  let lastTotal = 0;
424
323
  let loopCount = 0;
425
- try {
426
- for (var _m = true, prefetcher_7 = tslib_1.__asyncValues(prefetcher), prefetcher_7_1; prefetcher_7_1 = yield prefetcher_7.next(), _j = prefetcher_7_1.done, !_j; _m = true) {
427
- _l = prefetcher_7_1.value;
428
- _m = false;
429
- const item = _l;
430
- loopCount++;
431
- if (item > lastTotal) {
432
- lastTotal = item;
433
- }
324
+ for await (const item of prefetcher) {
325
+ loopCount++;
326
+ if (item > lastTotal) {
327
+ lastTotal = item;
434
328
  }
435
329
  }
436
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
437
- finally {
438
- try {
439
- if (!_m && !_j && (_k = prefetcher_7.return)) yield _k.call(prefetcher_7);
440
- }
441
- finally { if (e_8) throw e_8.error; }
442
- }
443
330
  expect(callCount).toBe(0);
444
331
  expect(loopCount).toBe(0);
445
332
  expect(total).toBe(0);
446
333
  expect(lastTotal).toBe(0);
447
- }));
448
- it('empty async iterable works - async mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
449
- var _o, e_9, _p, _q;
334
+ });
335
+ it('empty async iterable works - async mapper', async () => {
450
336
  let total = 0;
451
337
  let callCount = 0;
452
338
  const prefetcher = new iterable_mapper_1.IterableMapper((() => {
453
339
  return {
454
340
  [Symbol.asyncIterator]() {
455
341
  return {
456
- next() {
457
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
458
- yield sleep(100);
459
- return { value: undefined, done: true };
460
- });
342
+ async next() {
343
+ await sleep(100);
344
+ return { value: undefined, done: true };
461
345
  },
462
346
  };
463
347
  },
464
348
  };
465
- })(), (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
466
- yield sleep(10);
349
+ })(), async (value) => {
350
+ await sleep(10);
467
351
  callCount++;
468
352
  total += value;
469
353
  return total;
470
- }));
354
+ });
471
355
  let lastTotal = 0;
472
356
  let loopCount = 0;
473
- try {
474
- for (var _r = true, prefetcher_8 = tslib_1.__asyncValues(prefetcher), prefetcher_8_1; prefetcher_8_1 = yield prefetcher_8.next(), _o = prefetcher_8_1.done, !_o; _r = true) {
475
- _q = prefetcher_8_1.value;
476
- _r = false;
477
- const item = _q;
478
- loopCount++;
479
- if (item > lastTotal) {
480
- lastTotal = item;
481
- }
482
- }
483
- }
484
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
485
- finally {
486
- try {
487
- if (!_r && !_o && (_p = prefetcher_8.return)) yield _p.call(prefetcher_8);
357
+ for await (const item of prefetcher) {
358
+ loopCount++;
359
+ if (item > lastTotal) {
360
+ lastTotal = item;
488
361
  }
489
- finally { if (e_9) throw e_9.error; }
490
362
  }
491
363
  expect(callCount).toBe(0);
492
364
  expect(loopCount).toBe(0);
493
365
  expect(total).toBe(0);
494
366
  expect(lastTotal).toBe(0);
495
- }));
367
+ });
368
+ });
369
+ describe('order preservation tests', () => {
370
+ it('preserves exact sequential order under high load with concurrency 1, maxUnread 8', async () => {
371
+ const size = 1000;
372
+ const input = Array.from({ length: size }, (_, i) => ({
373
+ value: i + 1,
374
+ ms: Math.floor(Math.random() * 20), // Random delay 0-19ms
375
+ }));
376
+ const mappedOrder = [];
377
+ const iteratedOrder = [];
378
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async ({ value, ms }) => {
379
+ await sleep(ms);
380
+ mappedOrder.push(value);
381
+ return value;
382
+ }, { concurrency: 1, maxUnread: 8 });
383
+ for await (const value of prefetcher) {
384
+ iteratedOrder.push(value);
385
+ // Add some random delay in consuming to create backpressure
386
+ await sleep(Math.floor(Math.random() * 20));
387
+ }
388
+ // Verify exact sequential order preservation
389
+ expect(iteratedOrder).toHaveLength(size);
390
+ expect(mappedOrder).toHaveLength(size);
391
+ // Verify that both mapped and iterated orders match the input sequence
392
+ const expectedOrder = Array.from({ length: size }, (_, i) => i + 1);
393
+ expect(mappedOrder).toEqual(expectedOrder);
394
+ expect(iteratedOrder).toEqual(expectedOrder);
395
+ }, 30000);
496
396
  });
497
397
  describe('concurrency 1, maxUnread 1', () => {
498
398
  const concurrency = 1;
499
399
  const maxUnread = 1;
500
- it('catches exception from mapper - 1st item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
501
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
400
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
401
+ const input = [1, async () => sleep(300, 2), 3];
502
402
  const mappedValues = [];
503
403
  const iteratedValues = [];
504
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
505
- value = typeof value === 'function' ? yield value() : value;
404
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
405
+ value = typeof value === 'function' ? await value() : value;
506
406
  mappedValues.push(value);
507
407
  if (value === 1) {
508
- yield sleep(100);
408
+ await sleep(100);
509
409
  throw new Error('throw on 1st');
510
410
  }
511
411
  return value;
512
- }), { concurrency, maxUnread });
412
+ }, { concurrency, maxUnread });
513
413
  let loopCount = 0;
514
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
515
- var _a, e_10, _b, _c;
516
- try {
517
- for (var _d = true, prefetcher_9 = tslib_1.__asyncValues(prefetcher), prefetcher_9_1; prefetcher_9_1 = yield prefetcher_9.next(), _a = prefetcher_9_1.done, !_a; _d = true) {
518
- _c = prefetcher_9_1.value;
519
- _d = false;
520
- const value = _c;
521
- loopCount++;
522
- iteratedValues.push(value);
523
- }
524
- }
525
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
526
- finally {
527
- try {
528
- if (!_d && !_a && (_b = prefetcher_9.return)) yield _b.call(prefetcher_9);
529
- }
530
- finally { if (e_10) throw e_10.error; }
414
+ await expect(async () => {
415
+ for await (const value of prefetcher) {
416
+ loopCount++;
417
+ iteratedValues.push(value);
531
418
  }
532
- })).rejects.toThrowError('throw on 1st');
533
- yield sleep(500);
419
+ }).rejects.toThrowError('throw on 1st');
420
+ await sleep(500);
534
421
  expect(loopCount).toBe(0);
535
422
  expect(mappedValues).toEqual([1]);
536
423
  expect(iteratedValues).toEqual([]);
537
- }));
538
- it('catches exception from mapper - 2nd item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
539
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
424
+ });
425
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
426
+ const input = [1, async () => sleep(300, 2), 3];
540
427
  const mappedValues = [];
541
428
  const iteratedValues = [];
542
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
543
- value = typeof value === 'function' ? yield value() : value;
429
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
430
+ value = typeof value === 'function' ? await value() : value;
544
431
  mappedValues.push(value);
545
432
  if (value === 2) {
546
433
  throw new Error('throw on 2nd');
547
434
  }
548
- yield sleep(100);
435
+ await sleep(100);
549
436
  return value;
550
- }), { concurrency, maxUnread });
437
+ }, { concurrency, maxUnread });
551
438
  let loopCount = 0;
552
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
553
- var _e, e_11, _f, _g;
554
- try {
555
- for (var _h = true, prefetcher_10 = tslib_1.__asyncValues(prefetcher), prefetcher_10_1; prefetcher_10_1 = yield prefetcher_10.next(), _e = prefetcher_10_1.done, !_e; _h = true) {
556
- _g = prefetcher_10_1.value;
557
- _h = false;
558
- const value = _g;
559
- loopCount++;
560
- iteratedValues.push(value);
561
- }
562
- }
563
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
564
- finally {
565
- try {
566
- if (!_h && !_e && (_f = prefetcher_10.return)) yield _f.call(prefetcher_10);
567
- }
568
- finally { if (e_11) throw e_11.error; }
439
+ await expect(async () => {
440
+ for await (const value of prefetcher) {
441
+ loopCount++;
442
+ iteratedValues.push(value);
569
443
  }
570
- })).rejects.toThrowError('throw on 2nd');
571
- yield sleep(300);
444
+ }).rejects.toThrowError('throw on 2nd');
445
+ await sleep(300);
572
446
  expect(loopCount).toBe(1);
573
447
  expect(mappedValues).toEqual([1, 2]);
574
448
  expect(iteratedValues).toEqual([1]);
575
- }));
576
- it('catches exception from mapper - 1st item - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
577
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
449
+ });
450
+ it('catches exception from mapper - 1st item - stopOnMapperError false', async () => {
451
+ const input = [1, async () => sleep(300, 2), 3];
578
452
  const mappedValues = [];
579
453
  const iteratedValues = [];
580
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
581
- value = typeof value === 'function' ? yield value() : value;
454
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
455
+ value = typeof value === 'function' ? await value() : value;
582
456
  mappedValues.push(value);
583
457
  if (value === 1) {
584
458
  throw new Error('throw on 1st');
585
459
  }
586
- yield sleep(100);
460
+ await sleep(100);
587
461
  return value;
588
- }), { concurrency: 1, stopOnMapperError: false });
462
+ }, { concurrency: 1, stopOnMapperError: false });
589
463
  let loopCount = 0;
590
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
591
- var _j, e_12, _k, _l;
592
- try {
593
- for (var _m = true, prefetcher_11 = tslib_1.__asyncValues(prefetcher), prefetcher_11_1; prefetcher_11_1 = yield prefetcher_11.next(), _j = prefetcher_11_1.done, !_j; _m = true) {
594
- _l = prefetcher_11_1.value;
595
- _m = false;
596
- const value = _l;
597
- loopCount++;
598
- iteratedValues.push(value);
599
- }
600
- }
601
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
602
- finally {
603
- try {
604
- if (!_m && !_j && (_k = prefetcher_11.return)) yield _k.call(prefetcher_11);
605
- }
606
- finally { if (e_12) throw e_12.error; }
464
+ await expect(async () => {
465
+ for await (const value of prefetcher) {
466
+ loopCount++;
467
+ iteratedValues.push(value);
607
468
  }
608
- })).rejects.toThrowError('throw on 1st');
609
- yield sleep(300);
469
+ }).rejects.toThrowError('throw on 1st');
470
+ await sleep(300);
610
471
  expect(loopCount).toBe(2);
611
472
  expect(mappedValues).toEqual([1, 2, 3]);
612
473
  expect(iteratedValues).toEqual([2, 3]);
613
- }));
614
- it('catches exception from mapper - 2nd item - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
615
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
474
+ });
475
+ it('catches exception from mapper - 2nd item - stopOnMapperError false', async () => {
476
+ const input = [1, async () => sleep(300, 2), 3];
616
477
  const mappedValues = [];
617
478
  const iteratedValues = [];
618
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
619
- value = typeof value === 'function' ? yield value() : value;
479
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
480
+ value = typeof value === 'function' ? await value() : value;
620
481
  mappedValues.push(value);
621
482
  if (value === 2) {
622
483
  throw new Error('throw on 2nd');
623
484
  }
624
- yield sleep(100);
485
+ await sleep(100);
625
486
  return value;
626
- }), { concurrency, maxUnread, stopOnMapperError: false });
487
+ }, { concurrency, maxUnread, stopOnMapperError: false });
627
488
  let loopCount = 0;
628
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
629
- var _o, e_13, _p, _q;
630
- try {
631
- for (var _r = true, prefetcher_12 = tslib_1.__asyncValues(prefetcher), prefetcher_12_1; prefetcher_12_1 = yield prefetcher_12.next(), _o = prefetcher_12_1.done, !_o; _r = true) {
632
- _q = prefetcher_12_1.value;
633
- _r = false;
634
- const value = _q;
635
- loopCount++;
636
- iteratedValues.push(value);
637
- }
638
- }
639
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
640
- finally {
641
- try {
642
- if (!_r && !_o && (_p = prefetcher_12.return)) yield _p.call(prefetcher_12);
643
- }
644
- finally { if (e_13) throw e_13.error; }
489
+ await expect(async () => {
490
+ for await (const value of prefetcher) {
491
+ loopCount++;
492
+ iteratedValues.push(value);
645
493
  }
646
- })).rejects.toThrowError('throw on 2nd');
647
- yield sleep(300);
494
+ }).rejects.toThrowError('throw on 2nd');
495
+ await sleep(300);
648
496
  expect(loopCount).toBe(2);
649
497
  expect(mappedValues).toEqual([1, 2, 3]);
650
498
  expect(iteratedValues).toEqual([1, 3]);
651
- }));
652
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
499
+ });
500
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
653
501
  const input = new SleepIterator(10);
654
502
  const mappedValues = [];
655
503
  const iteratedValues = [];
@@ -658,30 +506,17 @@ describe('IterableMapper', () => {
658
506
  throw new Error('throw on each');
659
507
  }, { concurrency, maxUnread, stopOnMapperError: false });
660
508
  let loopCount = 0;
661
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
662
- var _s, e_14, _t, _u;
663
- try {
664
- for (var _v = true, prefetcher_13 = tslib_1.__asyncValues(prefetcher), prefetcher_13_1; prefetcher_13_1 = yield prefetcher_13.next(), _s = prefetcher_13_1.done, !_s; _v = true) {
665
- _u = prefetcher_13_1.value;
666
- _v = false;
667
- const value = _u;
668
- loopCount++;
669
- iteratedValues.push(value);
670
- }
671
- }
672
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
673
- finally {
674
- try {
675
- if (!_v && !_s && (_t = prefetcher_13.return)) yield _t.call(prefetcher_13);
676
- }
677
- finally { if (e_14) throw e_14.error; }
509
+ await expect(async () => {
510
+ for await (const value of prefetcher) {
511
+ loopCount++;
512
+ iteratedValues.push(value);
678
513
  }
679
- })).rejects.toThrowError('throw on each');
514
+ }).rejects.toThrowError('throw on each');
680
515
  expect(loopCount).toBe(0);
681
516
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
682
517
  expect(iteratedValues).toEqual([]);
683
- }));
684
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
518
+ });
519
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
685
520
  const input = new SleepIterator(10);
686
521
  const mappedValues = [];
687
522
  const iteratedValues = [];
@@ -690,257 +525,166 @@ describe('IterableMapper', () => {
690
525
  throw new Error('throw on each');
691
526
  }, { concurrency, maxUnread, stopOnMapperError: true });
692
527
  let loopCount = 0;
693
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
694
- var _w, e_15, _x, _y;
695
- try {
696
- for (var _z = true, prefetcher_14 = tslib_1.__asyncValues(prefetcher), prefetcher_14_1; prefetcher_14_1 = yield prefetcher_14.next(), _w = prefetcher_14_1.done, !_w; _z = true) {
697
- _y = prefetcher_14_1.value;
698
- _z = false;
699
- const value = _y;
700
- loopCount++;
701
- iteratedValues.push(value);
702
- }
703
- }
704
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
705
- finally {
706
- try {
707
- if (!_z && !_w && (_x = prefetcher_14.return)) yield _x.call(prefetcher_14);
708
- }
709
- finally { if (e_15) throw e_15.error; }
528
+ await expect(async () => {
529
+ for await (const value of prefetcher) {
530
+ loopCount++;
531
+ iteratedValues.push(value);
710
532
  }
711
- })).rejects.toThrowError('throw on each');
533
+ }).rejects.toThrowError('throw on each');
712
534
  expect(loopCount).toBe(0);
713
535
  expect(mappedValues).toEqual([1]);
714
536
  expect(iteratedValues).toEqual([]);
715
- }));
716
- it('catches exception from source iterator - 1st item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
537
+ });
538
+ it('catches exception from source iterator - 1st item', async () => {
717
539
  const input = new ThrowingIterator(100, 0, 100);
718
540
  const mappedValues = [];
719
541
  const iteratedValues = [];
720
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
542
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
721
543
  mappedValues.push(value);
722
- yield sleep(100);
544
+ await sleep(100);
723
545
  return value;
724
- }), { concurrency, maxUnread, stopOnMapperError: false });
546
+ }, { concurrency, maxUnread, stopOnMapperError: false });
725
547
  let loopCount = 0;
726
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
727
- var _0, e_16, _1, _2;
728
- try {
729
- for (var _3 = true, prefetcher_15 = tslib_1.__asyncValues(prefetcher), prefetcher_15_1; prefetcher_15_1 = yield prefetcher_15.next(), _0 = prefetcher_15_1.done, !_0; _3 = true) {
730
- _2 = prefetcher_15_1.value;
731
- _3 = false;
732
- const value = _2;
733
- loopCount++;
734
- iteratedValues.push(value);
735
- }
736
- }
737
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
738
- finally {
739
- try {
740
- if (!_3 && !_0 && (_1 = prefetcher_15.return)) yield _1.call(prefetcher_15);
741
- }
742
- finally { if (e_16) throw e_16.error; }
548
+ await expect(async () => {
549
+ for await (const value of prefetcher) {
550
+ loopCount++;
551
+ iteratedValues.push(value);
743
552
  }
744
- })).rejects.toThrowError('throwing on index 0');
745
- yield sleep(500);
553
+ }).rejects.toThrowError('throwing on index 0');
554
+ await sleep(500);
746
555
  expect(loopCount).toBe(0);
747
556
  expect(mappedValues).toEqual([]);
748
557
  expect(iteratedValues).toEqual([]);
749
- }));
558
+ });
750
559
  // The 2nd iterable item throwing is distinct from the 1st when concurrency is 1 because
751
560
  // it means that the sourceNext() is invoked from next() or sourceNext() and not from
752
561
  // the constructor
753
- it('catches exception from source iterator - 2nd item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
562
+ it('catches exception from source iterator - 2nd item', async () => {
754
563
  const input = new ThrowingIterator(100, 1, 100);
755
564
  const mappedValues = [];
756
565
  const iteratedValues = [];
757
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
566
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
758
567
  mappedValues.push(value);
759
- yield sleep(100);
568
+ await sleep(100);
760
569
  return value;
761
- }), { concurrency, maxUnread, stopOnMapperError: false });
570
+ }, { concurrency, maxUnread, stopOnMapperError: false });
762
571
  let loopCount = 0;
763
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
764
- var _4, e_17, _5, _6;
765
- try {
766
- for (var _7 = true, prefetcher_16 = tslib_1.__asyncValues(prefetcher), prefetcher_16_1; prefetcher_16_1 = yield prefetcher_16.next(), _4 = prefetcher_16_1.done, !_4; _7 = true) {
767
- _6 = prefetcher_16_1.value;
768
- _7 = false;
769
- const value = _6;
770
- loopCount++;
771
- iteratedValues.push(value);
772
- }
773
- }
774
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
775
- finally {
776
- try {
777
- if (!_7 && !_4 && (_5 = prefetcher_16.return)) yield _5.call(prefetcher_16);
778
- }
779
- finally { if (e_17) throw e_17.error; }
572
+ await expect(async () => {
573
+ for await (const value of prefetcher) {
574
+ loopCount++;
575
+ iteratedValues.push(value);
780
576
  }
781
- })).rejects.toThrowError('throwing on index 1');
782
- yield sleep(500);
577
+ }).rejects.toThrowError('throwing on index 1');
578
+ await sleep(500);
783
579
  expect(loopCount).toBe(1);
784
580
  expect(mappedValues).toEqual([0]);
785
581
  expect(iteratedValues).toEqual([0]);
786
- }));
582
+ });
787
583
  });
788
584
  describe('concurrency 2, maxUnread 4', () => {
789
585
  const concurrency = 2;
790
586
  const maxUnread = 4;
791
- it('catches exception from mapper - 1st item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
792
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
587
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
588
+ const input = [1, async () => sleep(300, 2), 3];
793
589
  const mappedValues = [];
794
590
  const iteratedValues = [];
795
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
796
- value = typeof value === 'function' ? yield value() : value;
591
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
592
+ value = typeof value === 'function' ? await value() : value;
797
593
  mappedValues.push(value);
798
594
  if (value === 1) {
799
- yield sleep(100);
595
+ await sleep(100);
800
596
  throw new Error('throw on 1st');
801
597
  }
802
598
  return value;
803
- }), { concurrency, maxUnread });
599
+ }, { concurrency, maxUnread });
804
600
  let loopCount = 0;
805
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
806
- var _a, e_18, _b, _c;
807
- try {
808
- for (var _d = true, prefetcher_17 = tslib_1.__asyncValues(prefetcher), prefetcher_17_1; prefetcher_17_1 = yield prefetcher_17.next(), _a = prefetcher_17_1.done, !_a; _d = true) {
809
- _c = prefetcher_17_1.value;
810
- _d = false;
811
- const value = _c;
812
- loopCount++;
813
- iteratedValues.push(value);
814
- }
815
- }
816
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
817
- finally {
818
- try {
819
- if (!_d && !_a && (_b = prefetcher_17.return)) yield _b.call(prefetcher_17);
820
- }
821
- finally { if (e_18) throw e_18.error; }
601
+ await expect(async () => {
602
+ for await (const value of prefetcher) {
603
+ loopCount++;
604
+ iteratedValues.push(value);
822
605
  }
823
- })).rejects.toThrowError('throw on 1st');
824
- yield sleep(500);
606
+ }).rejects.toThrowError('throw on 1st');
607
+ await sleep(500);
825
608
  expect(loopCount).toBe(0);
826
609
  expect(mappedValues).toEqual([1, 2]);
827
610
  expect(iteratedValues).toEqual([]);
828
- }));
829
- it('catches exception from mapper - 2nd item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
830
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
611
+ });
612
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
613
+ const input = [1, async () => sleep(300, 2), 3];
831
614
  const mappedValues = [];
832
615
  const iteratedValues = [];
833
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
834
- value = typeof value === 'function' ? yield value() : value;
616
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
617
+ value = typeof value === 'function' ? await value() : value;
835
618
  mappedValues.push(value);
836
619
  if (value === 2) {
837
620
  throw new Error('throw on 2nd');
838
621
  }
839
- yield sleep(100);
622
+ await sleep(100);
840
623
  return value;
841
- }), { concurrency, maxUnread });
624
+ }, { concurrency, maxUnread });
842
625
  let loopCount = 0;
843
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
844
- var _e, e_19, _f, _g;
845
- try {
846
- for (var _h = true, prefetcher_18 = tslib_1.__asyncValues(prefetcher), prefetcher_18_1; prefetcher_18_1 = yield prefetcher_18.next(), _e = prefetcher_18_1.done, !_e; _h = true) {
847
- _g = prefetcher_18_1.value;
848
- _h = false;
849
- const value = _g;
850
- loopCount++;
851
- iteratedValues.push(value);
852
- }
853
- }
854
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
855
- finally {
856
- try {
857
- if (!_h && !_e && (_f = prefetcher_18.return)) yield _f.call(prefetcher_18);
858
- }
859
- finally { if (e_19) throw e_19.error; }
626
+ await expect(async () => {
627
+ for await (const value of prefetcher) {
628
+ loopCount++;
629
+ iteratedValues.push(value);
860
630
  }
861
- })).rejects.toThrowError('throw on 2nd');
862
- yield sleep(300);
631
+ }).rejects.toThrowError('throw on 2nd');
632
+ await sleep(300);
863
633
  expect(loopCount).toBe(2);
864
634
  expect(mappedValues).toEqual([1, 3, 2]);
865
635
  expect(iteratedValues).toEqual([1, 3]);
866
- }));
867
- it('catches exception from mapper - 1st item - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
868
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
636
+ });
637
+ it('catches exception from mapper - 1st item - stopOnMapperError false', async () => {
638
+ const input = [1, async () => sleep(300, 2), 3];
869
639
  const mappedValues = [];
870
640
  const iteratedValues = [];
871
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
872
- value = typeof value === 'function' ? yield value() : value;
641
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
642
+ value = typeof value === 'function' ? await value() : value;
873
643
  mappedValues.push(value);
874
644
  if (value === 1) {
875
645
  throw new Error('throw on 1st');
876
646
  }
877
- yield sleep(100);
647
+ await sleep(100);
878
648
  return value;
879
- }), { concurrency, maxUnread, stopOnMapperError: false });
649
+ }, { concurrency, maxUnread, stopOnMapperError: false });
880
650
  let loopCount = 0;
881
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
882
- var _j, e_20, _k, _l;
883
- try {
884
- for (var _m = true, prefetcher_19 = tslib_1.__asyncValues(prefetcher), prefetcher_19_1; prefetcher_19_1 = yield prefetcher_19.next(), _j = prefetcher_19_1.done, !_j; _m = true) {
885
- _l = prefetcher_19_1.value;
886
- _m = false;
887
- const value = _l;
888
- loopCount++;
889
- iteratedValues.push(value);
890
- }
891
- }
892
- catch (e_20_1) { e_20 = { error: e_20_1 }; }
893
- finally {
894
- try {
895
- if (!_m && !_j && (_k = prefetcher_19.return)) yield _k.call(prefetcher_19);
896
- }
897
- finally { if (e_20) throw e_20.error; }
651
+ await expect(async () => {
652
+ for await (const value of prefetcher) {
653
+ loopCount++;
654
+ iteratedValues.push(value);
898
655
  }
899
- })).rejects.toThrowError('throw on 1st');
900
- yield sleep(300);
656
+ }).rejects.toThrowError('throw on 1st');
657
+ await sleep(300);
901
658
  expect(loopCount).toBe(2);
902
659
  expect(mappedValues).toEqual([1, 3, 2]);
903
660
  expect(iteratedValues).toEqual([3, 2]);
904
- }));
905
- it('catches exception from mapper - 2nd item - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
906
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
661
+ });
662
+ it('catches exception from mapper - 2nd item - stopOnMapperError false', async () => {
663
+ const input = [1, async () => sleep(300, 2), 3];
907
664
  const mappedValues = [];
908
665
  const iteratedValues = [];
909
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
910
- value = typeof value === 'function' ? yield value() : value;
666
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
667
+ value = typeof value === 'function' ? await value() : value;
911
668
  mappedValues.push(value);
912
669
  if (value === 2) {
913
670
  throw new Error('throw on 2nd');
914
671
  }
915
- yield sleep(100);
672
+ await sleep(100);
916
673
  return value;
917
- }), { concurrency, maxUnread, stopOnMapperError: false });
674
+ }, { concurrency, maxUnread, stopOnMapperError: false });
918
675
  let loopCount = 0;
919
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
920
- var _o, e_21, _p, _q;
921
- try {
922
- for (var _r = true, prefetcher_20 = tslib_1.__asyncValues(prefetcher), prefetcher_20_1; prefetcher_20_1 = yield prefetcher_20.next(), _o = prefetcher_20_1.done, !_o; _r = true) {
923
- _q = prefetcher_20_1.value;
924
- _r = false;
925
- const value = _q;
926
- loopCount++;
927
- iteratedValues.push(value);
928
- }
929
- }
930
- catch (e_21_1) { e_21 = { error: e_21_1 }; }
931
- finally {
932
- try {
933
- if (!_r && !_o && (_p = prefetcher_20.return)) yield _p.call(prefetcher_20);
934
- }
935
- finally { if (e_21) throw e_21.error; }
676
+ await expect(async () => {
677
+ for await (const value of prefetcher) {
678
+ loopCount++;
679
+ iteratedValues.push(value);
936
680
  }
937
- })).rejects.toThrowError('throw on 2nd');
938
- yield sleep(300);
681
+ }).rejects.toThrowError('throw on 2nd');
682
+ await sleep(300);
939
683
  expect(loopCount).toBe(2);
940
684
  expect(mappedValues).toEqual([1, 3, 2]);
941
685
  expect(iteratedValues).toEqual([1, 3]);
942
- }));
943
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
686
+ });
687
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
944
688
  const input = new SleepIterator(10);
945
689
  const mappedValues = [];
946
690
  const iteratedValues = [];
@@ -949,30 +693,17 @@ describe('IterableMapper', () => {
949
693
  throw new Error('throw on each');
950
694
  }, { concurrency, maxUnread, stopOnMapperError: false });
951
695
  let loopCount = 0;
952
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
953
- var _s, e_22, _t, _u;
954
- try {
955
- for (var _v = true, prefetcher_21 = tslib_1.__asyncValues(prefetcher), prefetcher_21_1; prefetcher_21_1 = yield prefetcher_21.next(), _s = prefetcher_21_1.done, !_s; _v = true) {
956
- _u = prefetcher_21_1.value;
957
- _v = false;
958
- const value = _u;
959
- loopCount++;
960
- iteratedValues.push(value);
961
- }
962
- }
963
- catch (e_22_1) { e_22 = { error: e_22_1 }; }
964
- finally {
965
- try {
966
- if (!_v && !_s && (_t = prefetcher_21.return)) yield _t.call(prefetcher_21);
967
- }
968
- finally { if (e_22) throw e_22.error; }
696
+ await expect(async () => {
697
+ for await (const value of prefetcher) {
698
+ loopCount++;
699
+ iteratedValues.push(value);
969
700
  }
970
- })).rejects.toThrowError('throw on each');
701
+ }).rejects.toThrowError('throw on each');
971
702
  expect(loopCount).toBe(0);
972
703
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
973
704
  expect(iteratedValues).toEqual([]);
974
- }));
975
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
705
+ });
706
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
976
707
  const input = new SleepIterator(10);
977
708
  const mappedValues = [];
978
709
  const iteratedValues = [];
@@ -981,138 +712,86 @@ describe('IterableMapper', () => {
981
712
  throw new Error('throw on each');
982
713
  }, { concurrency, maxUnread, stopOnMapperError: true });
983
714
  let loopCount = 0;
984
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
985
- var _w, e_23, _x, _y;
986
- try {
987
- for (var _z = true, prefetcher_22 = tslib_1.__asyncValues(prefetcher), prefetcher_22_1; prefetcher_22_1 = yield prefetcher_22.next(), _w = prefetcher_22_1.done, !_w; _z = true) {
988
- _y = prefetcher_22_1.value;
989
- _z = false;
990
- const value = _y;
991
- loopCount++;
992
- iteratedValues.push(value);
993
- }
994
- }
995
- catch (e_23_1) { e_23 = { error: e_23_1 }; }
996
- finally {
997
- try {
998
- if (!_z && !_w && (_x = prefetcher_22.return)) yield _x.call(prefetcher_22);
999
- }
1000
- finally { if (e_23) throw e_23.error; }
715
+ await expect(async () => {
716
+ for await (const value of prefetcher) {
717
+ loopCount++;
718
+ iteratedValues.push(value);
1001
719
  }
1002
- })).rejects.toThrowError('throw on each');
720
+ }).rejects.toThrowError('throw on each');
1003
721
  expect(loopCount).toBe(0);
1004
722
  expect(mappedValues).toEqual([1]);
1005
723
  expect(iteratedValues).toEqual([]);
1006
- }));
1007
- it('catches exception from source iterator - 1st item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
724
+ });
725
+ it('catches exception from source iterator - 1st item', async () => {
1008
726
  const input = new ThrowingIterator(100, 0, 100);
1009
727
  const mappedValues = [];
1010
728
  const iteratedValues = [];
1011
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
729
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
1012
730
  mappedValues.push(value);
1013
- yield sleep(100);
731
+ await sleep(100);
1014
732
  return value;
1015
- }), { concurrency, maxUnread, stopOnMapperError: false });
733
+ }, { concurrency, maxUnread, stopOnMapperError: false });
1016
734
  let loopCount = 0;
1017
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1018
- var _0, e_24, _1, _2;
1019
- try {
1020
- for (var _3 = true, prefetcher_23 = tslib_1.__asyncValues(prefetcher), prefetcher_23_1; prefetcher_23_1 = yield prefetcher_23.next(), _0 = prefetcher_23_1.done, !_0; _3 = true) {
1021
- _2 = prefetcher_23_1.value;
1022
- _3 = false;
1023
- const value = _2;
1024
- loopCount++;
1025
- iteratedValues.push(value);
1026
- }
1027
- }
1028
- catch (e_24_1) { e_24 = { error: e_24_1 }; }
1029
- finally {
1030
- try {
1031
- if (!_3 && !_0 && (_1 = prefetcher_23.return)) yield _1.call(prefetcher_23);
1032
- }
1033
- finally { if (e_24) throw e_24.error; }
735
+ await expect(async () => {
736
+ for await (const value of prefetcher) {
737
+ loopCount++;
738
+ iteratedValues.push(value);
1034
739
  }
1035
- })).rejects.toThrowError('throwing on index 0');
1036
- yield sleep(500);
740
+ }).rejects.toThrowError('throwing on index 0');
741
+ await sleep(500);
1037
742
  expect(loopCount).toBe(0);
1038
743
  expect(mappedValues).toEqual([]);
1039
744
  expect(iteratedValues).toEqual([]);
1040
- }));
745
+ });
1041
746
  // The 2nd iterable item throwing is distinct from the 1st when concurrency is 1 because
1042
747
  // it means that the sourceNext() is invoked from next() or sourceNext() and not from
1043
748
  // the constructor
1044
- it('catches exception from source iterator - 2nd item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
749
+ it('catches exception from source iterator - 2nd item', async () => {
1045
750
  const input = new ThrowingIterator(100, 1, 100);
1046
751
  const mappedValues = [];
1047
752
  const iteratedValues = [];
1048
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
753
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
1049
754
  mappedValues.push(value);
1050
- yield sleep(100);
755
+ await sleep(100);
1051
756
  return value;
1052
- }), { concurrency, maxUnread, stopOnMapperError: false });
757
+ }, { concurrency, maxUnread, stopOnMapperError: false });
1053
758
  let loopCount = 0;
1054
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1055
- var _4, e_25, _5, _6;
1056
- try {
1057
- for (var _7 = true, prefetcher_24 = tslib_1.__asyncValues(prefetcher), prefetcher_24_1; prefetcher_24_1 = yield prefetcher_24.next(), _4 = prefetcher_24_1.done, !_4; _7 = true) {
1058
- _6 = prefetcher_24_1.value;
1059
- _7 = false;
1060
- const value = _6;
1061
- loopCount++;
1062
- iteratedValues.push(value);
1063
- }
1064
- }
1065
- catch (e_25_1) { e_25 = { error: e_25_1 }; }
1066
- finally {
1067
- try {
1068
- if (!_7 && !_4 && (_5 = prefetcher_24.return)) yield _5.call(prefetcher_24);
1069
- }
1070
- finally { if (e_25) throw e_25.error; }
759
+ await expect(async () => {
760
+ for await (const value of prefetcher) {
761
+ loopCount++;
762
+ iteratedValues.push(value);
1071
763
  }
1072
- })).rejects.toThrowError('throwing on index 1');
1073
- yield sleep(500);
764
+ }).rejects.toThrowError('throwing on index 1');
765
+ await sleep(500);
1074
766
  expect(loopCount).toBe(1);
1075
767
  expect(mappedValues).toEqual([0]);
1076
768
  expect(iteratedValues).toEqual([0]);
1077
- }));
769
+ });
1078
770
  });
1079
771
  describe('concurrency Number.POSITIVE_INFINITY, maxUnread Number.POSITIVE_INFINITY', () => {
1080
772
  const concurrency = Number.POSITIVE_INFINITY;
1081
773
  const maxUnread = Number.POSITIVE_INFINITY;
1082
- it('catches exception from mapper - 1st item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1083
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
774
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
775
+ const input = [1, async () => sleep(300, 2), 3];
1084
776
  const mappedValues = [];
1085
777
  const iteratedValues = [];
1086
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1087
- value = typeof value === 'function' ? yield value() : value;
778
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
779
+ value = typeof value === 'function' ? await value() : value;
1088
780
  mappedValues.push(value);
1089
781
  if (value === 1) {
1090
- yield sleep(100);
782
+ await sleep(100);
1091
783
  throw new Error('throw on 1st');
1092
784
  }
1093
785
  return value;
1094
- }), { concurrency, maxUnread });
786
+ }, { concurrency, maxUnread });
1095
787
  let loopCount = 0;
1096
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1097
- var _a, e_26, _b, _c;
1098
- try {
1099
- for (var _d = true, prefetcher_25 = tslib_1.__asyncValues(prefetcher), prefetcher_25_1; prefetcher_25_1 = yield prefetcher_25.next(), _a = prefetcher_25_1.done, !_a; _d = true) {
1100
- _c = prefetcher_25_1.value;
1101
- _d = false;
1102
- const value = _c;
1103
- loopCount++;
1104
- iteratedValues.push(value);
1105
- }
1106
- }
1107
- catch (e_26_1) { e_26 = { error: e_26_1 }; }
1108
- finally {
1109
- try {
1110
- if (!_d && !_a && (_b = prefetcher_25.return)) yield _b.call(prefetcher_25);
1111
- }
1112
- finally { if (e_26) throw e_26.error; }
788
+ await expect(async () => {
789
+ for await (const value of prefetcher) {
790
+ loopCount++;
791
+ iteratedValues.push(value);
1113
792
  }
1114
- })).rejects.toThrowError('throw on 1st');
1115
- yield sleep(500);
793
+ }).rejects.toThrowError('throw on 1st');
794
+ await sleep(500);
1116
795
  expect(loopCount).toBe(1);
1117
796
  expect(mappedValues).toEqual([1, 3, 2]);
1118
797
  // NOTE: This seems wrong - Item 2 succeeds at being mapped, but after the 1st item throws.
@@ -1126,83 +805,57 @@ describe('IterableMapper', () => {
1126
805
  // then this behavior would match their results exactly (the 1st item would cause the iteration
1127
806
  // loop to break out).
1128
807
  expect(iteratedValues).toEqual([3]);
1129
- }));
1130
- it('catches exception from mapper - 2nd item - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1131
- const input = [1, () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return sleep(300, 2); }), 3];
808
+ });
809
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
810
+ const input = [1, async () => sleep(300, 2), 3];
1132
811
  const mappedValues = [];
1133
812
  const iteratedValues = [];
1134
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1135
- value = typeof value === 'function' ? yield value() : value;
813
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
814
+ value = typeof value === 'function' ? await value() : value;
1136
815
  mappedValues.push(value);
1137
816
  if (value === 2) {
1138
817
  throw new Error('throw on 2nd');
1139
818
  }
1140
- yield sleep(100);
819
+ await sleep(100);
1141
820
  return value;
1142
- }), { concurrency, maxUnread });
821
+ }, { concurrency, maxUnread });
1143
822
  let loopCount = 0;
1144
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1145
- var _e, e_27, _f, _g;
1146
- try {
1147
- for (var _h = true, prefetcher_26 = tslib_1.__asyncValues(prefetcher), prefetcher_26_1; prefetcher_26_1 = yield prefetcher_26.next(), _e = prefetcher_26_1.done, !_e; _h = true) {
1148
- _g = prefetcher_26_1.value;
1149
- _h = false;
1150
- const value = _g;
1151
- loopCount++;
1152
- iteratedValues.push(value);
1153
- }
1154
- }
1155
- catch (e_27_1) { e_27 = { error: e_27_1 }; }
1156
- finally {
1157
- try {
1158
- if (!_h && !_e && (_f = prefetcher_26.return)) yield _f.call(prefetcher_26);
1159
- }
1160
- finally { if (e_27) throw e_27.error; }
823
+ await expect(async () => {
824
+ for await (const value of prefetcher) {
825
+ loopCount++;
826
+ iteratedValues.push(value);
1161
827
  }
1162
- })).rejects.toThrowError('throw on 2nd');
1163
- yield sleep(300);
828
+ }).rejects.toThrowError('throw on 2nd');
829
+ await sleep(300);
1164
830
  expect(loopCount).toBe(2);
1165
831
  expect(mappedValues).toEqual([1, 3, 2]);
1166
832
  expect(iteratedValues).toEqual([1, 3]);
1167
- }));
1168
- it('async mapper works', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1169
- var _j, e_28, _k, _l;
833
+ });
834
+ it('async mapper works', async () => {
1170
835
  const max = 100;
1171
836
  const iterator = new SleepIterator(max);
1172
837
  let total = 0;
1173
838
  let callCount = 0;
1174
- const prefetcher = new iterable_mapper_1.IterableMapper(iterator, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
839
+ const prefetcher = new iterable_mapper_1.IterableMapper(iterator, async (value) => {
1175
840
  callCount++;
1176
841
  total += value;
1177
- yield sleep(10 * (callCount % 6));
842
+ await sleep(10 * (callCount % 6));
1178
843
  return total;
1179
- }), { concurrency, maxUnread });
844
+ }, { concurrency, maxUnread });
1180
845
  let lastTotal = 0;
1181
846
  let loopCount = 0;
1182
- try {
1183
- for (var _m = true, prefetcher_27 = tslib_1.__asyncValues(prefetcher), prefetcher_27_1; prefetcher_27_1 = yield prefetcher_27.next(), _j = prefetcher_27_1.done, !_j; _m = true) {
1184
- _l = prefetcher_27_1.value;
1185
- _m = false;
1186
- const item = _l;
1187
- loopCount++;
1188
- if (item > lastTotal) {
1189
- lastTotal = item;
1190
- }
1191
- }
1192
- }
1193
- catch (e_28_1) { e_28 = { error: e_28_1 }; }
1194
- finally {
1195
- try {
1196
- if (!_m && !_j && (_k = prefetcher_27.return)) yield _k.call(prefetcher_27);
847
+ for await (const item of prefetcher) {
848
+ loopCount++;
849
+ if (item > lastTotal) {
850
+ lastTotal = item;
1197
851
  }
1198
- finally { if (e_28) throw e_28.error; }
1199
852
  }
1200
853
  expect(callCount).toBe(max);
1201
854
  expect(loopCount).toBe(max);
1202
855
  expect(total).toBe((max * (max + 1)) / 2);
1203
856
  expect(lastTotal).toBe((max * (max + 1)) / 2);
1204
- }));
1205
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
857
+ });
858
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
1206
859
  const input = new SleepIterator(10);
1207
860
  const mappedValues = [];
1208
861
  const iteratedValues = [];
@@ -1211,30 +864,17 @@ describe('IterableMapper', () => {
1211
864
  throw new Error('throw on each');
1212
865
  }, { concurrency, maxUnread, stopOnMapperError: true });
1213
866
  let loopCount = 0;
1214
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1215
- var _o, e_29, _p, _q;
1216
- try {
1217
- for (var _r = true, prefetcher_28 = tslib_1.__asyncValues(prefetcher), prefetcher_28_1; prefetcher_28_1 = yield prefetcher_28.next(), _o = prefetcher_28_1.done, !_o; _r = true) {
1218
- _q = prefetcher_28_1.value;
1219
- _r = false;
1220
- const value = _q;
1221
- loopCount++;
1222
- iteratedValues.push(value);
1223
- }
1224
- }
1225
- catch (e_29_1) { e_29 = { error: e_29_1 }; }
1226
- finally {
1227
- try {
1228
- if (!_r && !_o && (_p = prefetcher_28.return)) yield _p.call(prefetcher_28);
1229
- }
1230
- finally { if (e_29) throw e_29.error; }
867
+ await expect(async () => {
868
+ for await (const value of prefetcher) {
869
+ loopCount++;
870
+ iteratedValues.push(value);
1231
871
  }
1232
- })).rejects.toThrowError('throw on each');
872
+ }).rejects.toThrowError('throw on each');
1233
873
  expect(loopCount).toBe(0);
1234
874
  expect(mappedValues).toEqual([1]);
1235
875
  expect(iteratedValues).toEqual([]);
1236
- }));
1237
- it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
876
+ });
877
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
1238
878
  const input = new SleepIterator(10);
1239
879
  const mappedValues = [];
1240
880
  const iteratedValues = [];
@@ -1243,29 +883,16 @@ describe('IterableMapper', () => {
1243
883
  throw new Error('throw on each');
1244
884
  }, { concurrency, maxUnread, stopOnMapperError: false });
1245
885
  let loopCount = 0;
1246
- yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
1247
- var _s, e_30, _t, _u;
1248
- try {
1249
- for (var _v = true, prefetcher_29 = tslib_1.__asyncValues(prefetcher), prefetcher_29_1; prefetcher_29_1 = yield prefetcher_29.next(), _s = prefetcher_29_1.done, !_s; _v = true) {
1250
- _u = prefetcher_29_1.value;
1251
- _v = false;
1252
- const value = _u;
1253
- loopCount++;
1254
- iteratedValues.push(value);
1255
- }
1256
- }
1257
- catch (e_30_1) { e_30 = { error: e_30_1 }; }
1258
- finally {
1259
- try {
1260
- if (!_v && !_s && (_t = prefetcher_29.return)) yield _t.call(prefetcher_29);
1261
- }
1262
- finally { if (e_30) throw e_30.error; }
886
+ await expect(async () => {
887
+ for await (const value of prefetcher) {
888
+ loopCount++;
889
+ iteratedValues.push(value);
1263
890
  }
1264
- })).rejects.toThrowError('throw on each');
891
+ }).rejects.toThrowError('throw on each');
1265
892
  expect(loopCount).toBe(0);
1266
893
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1267
894
  expect(iteratedValues).toEqual([]);
1268
- }));
895
+ });
1269
896
  });
1270
897
  });
1271
898
  //# sourceMappingURL=iterable-mapper.test.js.map