@shutterstock/p-map-iterable 1.0.10 → 1.1.0

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,155 @@ 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
- }
434
- }
435
- }
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);
324
+ for await (const item of prefetcher) {
325
+ loopCount++;
326
+ if (item > lastTotal) {
327
+ lastTotal = item;
440
328
  }
441
- finally { if (e_8) throw e_8.error; }
442
329
  }
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
- }
357
+ for await (const item of prefetcher) {
358
+ loopCount++;
359
+ if (item > lastTotal) {
360
+ lastTotal = item;
482
361
  }
483
362
  }
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);
488
- }
489
- finally { if (e_9) throw e_9.error; }
490
- }
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
+ });
496
368
  });
497
369
  describe('concurrency 1, maxUnread 1', () => {
498
370
  const concurrency = 1;
499
371
  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];
372
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
373
+ const input = [1, async () => sleep(300, 2), 3];
502
374
  const mappedValues = [];
503
375
  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;
376
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
377
+ value = typeof value === 'function' ? await value() : value;
506
378
  mappedValues.push(value);
507
379
  if (value === 1) {
508
- yield sleep(100);
380
+ await sleep(100);
509
381
  throw new Error('throw on 1st');
510
382
  }
511
383
  return value;
512
- }), { concurrency, maxUnread });
384
+ }, { concurrency, maxUnread });
513
385
  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; }
386
+ await expect(async () => {
387
+ for await (const value of prefetcher) {
388
+ loopCount++;
389
+ iteratedValues.push(value);
531
390
  }
532
- })).rejects.toThrowError('throw on 1st');
533
- yield sleep(500);
391
+ }).rejects.toThrowError('throw on 1st');
392
+ await sleep(500);
534
393
  expect(loopCount).toBe(0);
535
394
  expect(mappedValues).toEqual([1]);
536
395
  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];
396
+ });
397
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
398
+ const input = [1, async () => sleep(300, 2), 3];
540
399
  const mappedValues = [];
541
400
  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;
401
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
402
+ value = typeof value === 'function' ? await value() : value;
544
403
  mappedValues.push(value);
545
404
  if (value === 2) {
546
405
  throw new Error('throw on 2nd');
547
406
  }
548
- yield sleep(100);
407
+ await sleep(100);
549
408
  return value;
550
- }), { concurrency, maxUnread });
409
+ }, { concurrency, maxUnread });
551
410
  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; }
411
+ await expect(async () => {
412
+ for await (const value of prefetcher) {
413
+ loopCount++;
414
+ iteratedValues.push(value);
569
415
  }
570
- })).rejects.toThrowError('throw on 2nd');
571
- yield sleep(300);
416
+ }).rejects.toThrowError('throw on 2nd');
417
+ await sleep(300);
572
418
  expect(loopCount).toBe(1);
573
419
  expect(mappedValues).toEqual([1, 2]);
574
420
  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];
421
+ });
422
+ it('catches exception from mapper - 1st item - stopOnMapperError false', async () => {
423
+ const input = [1, async () => sleep(300, 2), 3];
578
424
  const mappedValues = [];
579
425
  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;
426
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
427
+ value = typeof value === 'function' ? await value() : value;
582
428
  mappedValues.push(value);
583
429
  if (value === 1) {
584
430
  throw new Error('throw on 1st');
585
431
  }
586
- yield sleep(100);
432
+ await sleep(100);
587
433
  return value;
588
- }), { concurrency: 1, stopOnMapperError: false });
434
+ }, { concurrency: 1, stopOnMapperError: false });
589
435
  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; }
436
+ await expect(async () => {
437
+ for await (const value of prefetcher) {
438
+ loopCount++;
439
+ iteratedValues.push(value);
607
440
  }
608
- })).rejects.toThrowError('throw on 1st');
609
- yield sleep(300);
441
+ }).rejects.toThrowError('throw on 1st');
442
+ await sleep(300);
610
443
  expect(loopCount).toBe(2);
611
444
  expect(mappedValues).toEqual([1, 2, 3]);
612
445
  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];
446
+ });
447
+ it('catches exception from mapper - 2nd item - stopOnMapperError false', async () => {
448
+ const input = [1, async () => sleep(300, 2), 3];
616
449
  const mappedValues = [];
617
450
  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;
451
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
452
+ value = typeof value === 'function' ? await value() : value;
620
453
  mappedValues.push(value);
621
454
  if (value === 2) {
622
455
  throw new Error('throw on 2nd');
623
456
  }
624
- yield sleep(100);
457
+ await sleep(100);
625
458
  return value;
626
- }), { concurrency, maxUnread, stopOnMapperError: false });
459
+ }, { concurrency, maxUnread, stopOnMapperError: false });
627
460
  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; }
461
+ await expect(async () => {
462
+ for await (const value of prefetcher) {
463
+ loopCount++;
464
+ iteratedValues.push(value);
645
465
  }
646
- })).rejects.toThrowError('throw on 2nd');
647
- yield sleep(300);
466
+ }).rejects.toThrowError('throw on 2nd');
467
+ await sleep(300);
648
468
  expect(loopCount).toBe(2);
649
469
  expect(mappedValues).toEqual([1, 2, 3]);
650
470
  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* () {
471
+ });
472
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
653
473
  const input = new SleepIterator(10);
654
474
  const mappedValues = [];
655
475
  const iteratedValues = [];
@@ -658,30 +478,17 @@ describe('IterableMapper', () => {
658
478
  throw new Error('throw on each');
659
479
  }, { concurrency, maxUnread, stopOnMapperError: false });
660
480
  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; }
481
+ await expect(async () => {
482
+ for await (const value of prefetcher) {
483
+ loopCount++;
484
+ iteratedValues.push(value);
678
485
  }
679
- })).rejects.toThrowError('throw on each');
486
+ }).rejects.toThrowError('throw on each');
680
487
  expect(loopCount).toBe(0);
681
488
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
682
489
  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* () {
490
+ });
491
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
685
492
  const input = new SleepIterator(10);
686
493
  const mappedValues = [];
687
494
  const iteratedValues = [];
@@ -690,257 +497,166 @@ describe('IterableMapper', () => {
690
497
  throw new Error('throw on each');
691
498
  }, { concurrency, maxUnread, stopOnMapperError: true });
692
499
  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; }
500
+ await expect(async () => {
501
+ for await (const value of prefetcher) {
502
+ loopCount++;
503
+ iteratedValues.push(value);
710
504
  }
711
- })).rejects.toThrowError('throw on each');
505
+ }).rejects.toThrowError('throw on each');
712
506
  expect(loopCount).toBe(0);
713
507
  expect(mappedValues).toEqual([1]);
714
508
  expect(iteratedValues).toEqual([]);
715
- }));
716
- it('catches exception from source iterator - 1st item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
509
+ });
510
+ it('catches exception from source iterator - 1st item', async () => {
717
511
  const input = new ThrowingIterator(100, 0, 100);
718
512
  const mappedValues = [];
719
513
  const iteratedValues = [];
720
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
514
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
721
515
  mappedValues.push(value);
722
- yield sleep(100);
516
+ await sleep(100);
723
517
  return value;
724
- }), { concurrency, maxUnread, stopOnMapperError: false });
518
+ }, { concurrency, maxUnread, stopOnMapperError: false });
725
519
  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; }
520
+ await expect(async () => {
521
+ for await (const value of prefetcher) {
522
+ loopCount++;
523
+ iteratedValues.push(value);
743
524
  }
744
- })).rejects.toThrowError('throwing on index 0');
745
- yield sleep(500);
525
+ }).rejects.toThrowError('throwing on index 0');
526
+ await sleep(500);
746
527
  expect(loopCount).toBe(0);
747
528
  expect(mappedValues).toEqual([]);
748
529
  expect(iteratedValues).toEqual([]);
749
- }));
530
+ });
750
531
  // The 2nd iterable item throwing is distinct from the 1st when concurrency is 1 because
751
532
  // it means that the sourceNext() is invoked from next() or sourceNext() and not from
752
533
  // the constructor
753
- it('catches exception from source iterator - 2nd item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
534
+ it('catches exception from source iterator - 2nd item', async () => {
754
535
  const input = new ThrowingIterator(100, 1, 100);
755
536
  const mappedValues = [];
756
537
  const iteratedValues = [];
757
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
538
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
758
539
  mappedValues.push(value);
759
- yield sleep(100);
540
+ await sleep(100);
760
541
  return value;
761
- }), { concurrency, maxUnread, stopOnMapperError: false });
542
+ }, { concurrency, maxUnread, stopOnMapperError: false });
762
543
  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; }
544
+ await expect(async () => {
545
+ for await (const value of prefetcher) {
546
+ loopCount++;
547
+ iteratedValues.push(value);
780
548
  }
781
- })).rejects.toThrowError('throwing on index 1');
782
- yield sleep(500);
549
+ }).rejects.toThrowError('throwing on index 1');
550
+ await sleep(500);
783
551
  expect(loopCount).toBe(1);
784
552
  expect(mappedValues).toEqual([0]);
785
553
  expect(iteratedValues).toEqual([0]);
786
- }));
554
+ });
787
555
  });
788
556
  describe('concurrency 2, maxUnread 4', () => {
789
557
  const concurrency = 2;
790
558
  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];
559
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
560
+ const input = [1, async () => sleep(300, 2), 3];
793
561
  const mappedValues = [];
794
562
  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;
563
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
564
+ value = typeof value === 'function' ? await value() : value;
797
565
  mappedValues.push(value);
798
566
  if (value === 1) {
799
- yield sleep(100);
567
+ await sleep(100);
800
568
  throw new Error('throw on 1st');
801
569
  }
802
570
  return value;
803
- }), { concurrency, maxUnread });
571
+ }, { concurrency, maxUnread });
804
572
  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; }
573
+ await expect(async () => {
574
+ for await (const value of prefetcher) {
575
+ loopCount++;
576
+ iteratedValues.push(value);
822
577
  }
823
- })).rejects.toThrowError('throw on 1st');
824
- yield sleep(500);
578
+ }).rejects.toThrowError('throw on 1st');
579
+ await sleep(500);
825
580
  expect(loopCount).toBe(0);
826
581
  expect(mappedValues).toEqual([1, 2]);
827
582
  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];
583
+ });
584
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
585
+ const input = [1, async () => sleep(300, 2), 3];
831
586
  const mappedValues = [];
832
587
  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;
588
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
589
+ value = typeof value === 'function' ? await value() : value;
835
590
  mappedValues.push(value);
836
591
  if (value === 2) {
837
592
  throw new Error('throw on 2nd');
838
593
  }
839
- yield sleep(100);
594
+ await sleep(100);
840
595
  return value;
841
- }), { concurrency, maxUnread });
596
+ }, { concurrency, maxUnread });
842
597
  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; }
598
+ await expect(async () => {
599
+ for await (const value of prefetcher) {
600
+ loopCount++;
601
+ iteratedValues.push(value);
860
602
  }
861
- })).rejects.toThrowError('throw on 2nd');
862
- yield sleep(300);
603
+ }).rejects.toThrowError('throw on 2nd');
604
+ await sleep(300);
863
605
  expect(loopCount).toBe(2);
864
606
  expect(mappedValues).toEqual([1, 3, 2]);
865
607
  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];
608
+ });
609
+ it('catches exception from mapper - 1st item - stopOnMapperError false', async () => {
610
+ const input = [1, async () => sleep(300, 2), 3];
869
611
  const mappedValues = [];
870
612
  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;
613
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
614
+ value = typeof value === 'function' ? await value() : value;
873
615
  mappedValues.push(value);
874
616
  if (value === 1) {
875
617
  throw new Error('throw on 1st');
876
618
  }
877
- yield sleep(100);
619
+ await sleep(100);
878
620
  return value;
879
- }), { concurrency, maxUnread, stopOnMapperError: false });
621
+ }, { concurrency, maxUnread, stopOnMapperError: false });
880
622
  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; }
623
+ await expect(async () => {
624
+ for await (const value of prefetcher) {
625
+ loopCount++;
626
+ iteratedValues.push(value);
898
627
  }
899
- })).rejects.toThrowError('throw on 1st');
900
- yield sleep(300);
628
+ }).rejects.toThrowError('throw on 1st');
629
+ await sleep(300);
901
630
  expect(loopCount).toBe(2);
902
631
  expect(mappedValues).toEqual([1, 3, 2]);
903
632
  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];
633
+ });
634
+ it('catches exception from mapper - 2nd item - stopOnMapperError false', async () => {
635
+ const input = [1, async () => sleep(300, 2), 3];
907
636
  const mappedValues = [];
908
637
  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;
638
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
639
+ value = typeof value === 'function' ? await value() : value;
911
640
  mappedValues.push(value);
912
641
  if (value === 2) {
913
642
  throw new Error('throw on 2nd');
914
643
  }
915
- yield sleep(100);
644
+ await sleep(100);
916
645
  return value;
917
- }), { concurrency, maxUnread, stopOnMapperError: false });
646
+ }, { concurrency, maxUnread, stopOnMapperError: false });
918
647
  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; }
648
+ await expect(async () => {
649
+ for await (const value of prefetcher) {
650
+ loopCount++;
651
+ iteratedValues.push(value);
936
652
  }
937
- })).rejects.toThrowError('throw on 2nd');
938
- yield sleep(300);
653
+ }).rejects.toThrowError('throw on 2nd');
654
+ await sleep(300);
939
655
  expect(loopCount).toBe(2);
940
656
  expect(mappedValues).toEqual([1, 3, 2]);
941
657
  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* () {
658
+ });
659
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
944
660
  const input = new SleepIterator(10);
945
661
  const mappedValues = [];
946
662
  const iteratedValues = [];
@@ -949,30 +665,17 @@ describe('IterableMapper', () => {
949
665
  throw new Error('throw on each');
950
666
  }, { concurrency, maxUnread, stopOnMapperError: false });
951
667
  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; }
668
+ await expect(async () => {
669
+ for await (const value of prefetcher) {
670
+ loopCount++;
671
+ iteratedValues.push(value);
969
672
  }
970
- })).rejects.toThrowError('throw on each');
673
+ }).rejects.toThrowError('throw on each');
971
674
  expect(loopCount).toBe(0);
972
675
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
973
676
  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* () {
677
+ });
678
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
976
679
  const input = new SleepIterator(10);
977
680
  const mappedValues = [];
978
681
  const iteratedValues = [];
@@ -981,138 +684,86 @@ describe('IterableMapper', () => {
981
684
  throw new Error('throw on each');
982
685
  }, { concurrency, maxUnread, stopOnMapperError: true });
983
686
  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; }
687
+ await expect(async () => {
688
+ for await (const value of prefetcher) {
689
+ loopCount++;
690
+ iteratedValues.push(value);
1001
691
  }
1002
- })).rejects.toThrowError('throw on each');
692
+ }).rejects.toThrowError('throw on each');
1003
693
  expect(loopCount).toBe(0);
1004
694
  expect(mappedValues).toEqual([1]);
1005
695
  expect(iteratedValues).toEqual([]);
1006
- }));
1007
- it('catches exception from source iterator - 1st item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
696
+ });
697
+ it('catches exception from source iterator - 1st item', async () => {
1008
698
  const input = new ThrowingIterator(100, 0, 100);
1009
699
  const mappedValues = [];
1010
700
  const iteratedValues = [];
1011
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
701
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
1012
702
  mappedValues.push(value);
1013
- yield sleep(100);
703
+ await sleep(100);
1014
704
  return value;
1015
- }), { concurrency, maxUnread, stopOnMapperError: false });
705
+ }, { concurrency, maxUnread, stopOnMapperError: false });
1016
706
  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; }
707
+ await expect(async () => {
708
+ for await (const value of prefetcher) {
709
+ loopCount++;
710
+ iteratedValues.push(value);
1034
711
  }
1035
- })).rejects.toThrowError('throwing on index 0');
1036
- yield sleep(500);
712
+ }).rejects.toThrowError('throwing on index 0');
713
+ await sleep(500);
1037
714
  expect(loopCount).toBe(0);
1038
715
  expect(mappedValues).toEqual([]);
1039
716
  expect(iteratedValues).toEqual([]);
1040
- }));
717
+ });
1041
718
  // The 2nd iterable item throwing is distinct from the 1st when concurrency is 1 because
1042
719
  // it means that the sourceNext() is invoked from next() or sourceNext() and not from
1043
720
  // the constructor
1044
- it('catches exception from source iterator - 2nd item', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
721
+ it('catches exception from source iterator - 2nd item', async () => {
1045
722
  const input = new ThrowingIterator(100, 1, 100);
1046
723
  const mappedValues = [];
1047
724
  const iteratedValues = [];
1048
- const prefetcher = new iterable_mapper_1.IterableMapper(input, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
725
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
1049
726
  mappedValues.push(value);
1050
- yield sleep(100);
727
+ await sleep(100);
1051
728
  return value;
1052
- }), { concurrency, maxUnread, stopOnMapperError: false });
729
+ }, { concurrency, maxUnread, stopOnMapperError: false });
1053
730
  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; }
731
+ await expect(async () => {
732
+ for await (const value of prefetcher) {
733
+ loopCount++;
734
+ iteratedValues.push(value);
1071
735
  }
1072
- })).rejects.toThrowError('throwing on index 1');
1073
- yield sleep(500);
736
+ }).rejects.toThrowError('throwing on index 1');
737
+ await sleep(500);
1074
738
  expect(loopCount).toBe(1);
1075
739
  expect(mappedValues).toEqual([0]);
1076
740
  expect(iteratedValues).toEqual([0]);
1077
- }));
741
+ });
1078
742
  });
1079
743
  describe('concurrency Number.POSITIVE_INFINITY, maxUnread Number.POSITIVE_INFINITY', () => {
1080
744
  const concurrency = Number.POSITIVE_INFINITY;
1081
745
  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];
746
+ it('catches exception from mapper - 1st item - stopOnMapperError true', async () => {
747
+ const input = [1, async () => sleep(300, 2), 3];
1084
748
  const mappedValues = [];
1085
749
  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;
750
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
751
+ value = typeof value === 'function' ? await value() : value;
1088
752
  mappedValues.push(value);
1089
753
  if (value === 1) {
1090
- yield sleep(100);
754
+ await sleep(100);
1091
755
  throw new Error('throw on 1st');
1092
756
  }
1093
757
  return value;
1094
- }), { concurrency, maxUnread });
758
+ }, { concurrency, maxUnread });
1095
759
  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; }
760
+ await expect(async () => {
761
+ for await (const value of prefetcher) {
762
+ loopCount++;
763
+ iteratedValues.push(value);
1113
764
  }
1114
- })).rejects.toThrowError('throw on 1st');
1115
- yield sleep(500);
765
+ }).rejects.toThrowError('throw on 1st');
766
+ await sleep(500);
1116
767
  expect(loopCount).toBe(1);
1117
768
  expect(mappedValues).toEqual([1, 3, 2]);
1118
769
  // NOTE: This seems wrong - Item 2 succeeds at being mapped, but after the 1st item throws.
@@ -1126,83 +777,57 @@ describe('IterableMapper', () => {
1126
777
  // then this behavior would match their results exactly (the 1st item would cause the iteration
1127
778
  // loop to break out).
1128
779
  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];
780
+ });
781
+ it('catches exception from mapper - 2nd item - stopOnMapperError true', async () => {
782
+ const input = [1, async () => sleep(300, 2), 3];
1132
783
  const mappedValues = [];
1133
784
  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;
785
+ const prefetcher = new iterable_mapper_1.IterableMapper(input, async (value) => {
786
+ value = typeof value === 'function' ? await value() : value;
1136
787
  mappedValues.push(value);
1137
788
  if (value === 2) {
1138
789
  throw new Error('throw on 2nd');
1139
790
  }
1140
- yield sleep(100);
791
+ await sleep(100);
1141
792
  return value;
1142
- }), { concurrency, maxUnread });
793
+ }, { concurrency, maxUnread });
1143
794
  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; }
795
+ await expect(async () => {
796
+ for await (const value of prefetcher) {
797
+ loopCount++;
798
+ iteratedValues.push(value);
1161
799
  }
1162
- })).rejects.toThrowError('throw on 2nd');
1163
- yield sleep(300);
800
+ }).rejects.toThrowError('throw on 2nd');
801
+ await sleep(300);
1164
802
  expect(loopCount).toBe(2);
1165
803
  expect(mappedValues).toEqual([1, 3, 2]);
1166
804
  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;
805
+ });
806
+ it('async mapper works', async () => {
1170
807
  const max = 100;
1171
808
  const iterator = new SleepIterator(max);
1172
809
  let total = 0;
1173
810
  let callCount = 0;
1174
- const prefetcher = new iterable_mapper_1.IterableMapper(iterator, (value) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
811
+ const prefetcher = new iterable_mapper_1.IterableMapper(iterator, async (value) => {
1175
812
  callCount++;
1176
813
  total += value;
1177
- yield sleep(10 * (callCount % 6));
814
+ await sleep(10 * (callCount % 6));
1178
815
  return total;
1179
- }), { concurrency, maxUnread });
816
+ }, { concurrency, maxUnread });
1180
817
  let lastTotal = 0;
1181
818
  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);
819
+ for await (const item of prefetcher) {
820
+ loopCount++;
821
+ if (item > lastTotal) {
822
+ lastTotal = item;
1197
823
  }
1198
- finally { if (e_28) throw e_28.error; }
1199
824
  }
1200
825
  expect(callCount).toBe(max);
1201
826
  expect(loopCount).toBe(max);
1202
827
  expect(total).toBe((max * (max + 1)) / 2);
1203
828
  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* () {
829
+ });
830
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError true', async () => {
1206
831
  const input = new SleepIterator(10);
1207
832
  const mappedValues = [];
1208
833
  const iteratedValues = [];
@@ -1211,30 +836,17 @@ describe('IterableMapper', () => {
1211
836
  throw new Error('throw on each');
1212
837
  }, { concurrency, maxUnread, stopOnMapperError: true });
1213
838
  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; }
839
+ await expect(async () => {
840
+ for await (const value of prefetcher) {
841
+ loopCount++;
842
+ iteratedValues.push(value);
1231
843
  }
1232
- })).rejects.toThrowError('throw on each');
844
+ }).rejects.toThrowError('throw on each');
1233
845
  expect(loopCount).toBe(0);
1234
846
  expect(mappedValues).toEqual([1]);
1235
847
  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* () {
848
+ });
849
+ it('catches exception from mapper - 10 of 10 items - stopOnMapperError false', async () => {
1238
850
  const input = new SleepIterator(10);
1239
851
  const mappedValues = [];
1240
852
  const iteratedValues = [];
@@ -1243,29 +855,16 @@ describe('IterableMapper', () => {
1243
855
  throw new Error('throw on each');
1244
856
  }, { concurrency, maxUnread, stopOnMapperError: false });
1245
857
  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; }
858
+ await expect(async () => {
859
+ for await (const value of prefetcher) {
860
+ loopCount++;
861
+ iteratedValues.push(value);
1263
862
  }
1264
- })).rejects.toThrowError('throw on each');
863
+ }).rejects.toThrowError('throw on each');
1265
864
  expect(loopCount).toBe(0);
1266
865
  expect(mappedValues).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
1267
866
  expect(iteratedValues).toEqual([]);
1268
- }));
867
+ });
1269
868
  });
1270
869
  });
1271
870
  //# sourceMappingURL=iterable-mapper.test.js.map