@shutterstock/p-map-iterable 0.0.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.
Files changed (55) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +110 -0
  3. package/dist/blocking-queue.d.ts +57 -0
  4. package/dist/blocking-queue.d.ts.map +1 -0
  5. package/dist/blocking-queue.js +193 -0
  6. package/dist/blocking-queue.js.map +1 -0
  7. package/dist/blocking-queue.test.d.ts +2 -0
  8. package/dist/blocking-queue.test.d.ts.map +1 -0
  9. package/dist/blocking-queue.test.js +175 -0
  10. package/dist/blocking-queue.test.js.map +1 -0
  11. package/dist/index.d.ts +8 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +16 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/iterable-mapper.d.ts +104 -0
  16. package/dist/iterable-mapper.d.ts.map +1 -0
  17. package/dist/iterable-mapper.js +321 -0
  18. package/dist/iterable-mapper.js.map +1 -0
  19. package/dist/iterable-mapper.test.d.ts +2 -0
  20. package/dist/iterable-mapper.test.d.ts.map +1 -0
  21. package/dist/iterable-mapper.test.js +1421 -0
  22. package/dist/iterable-mapper.test.js.map +1 -0
  23. package/dist/iterable-queue-mapper-simple.d.ts +49 -0
  24. package/dist/iterable-queue-mapper-simple.d.ts.map +1 -0
  25. package/dist/iterable-queue-mapper-simple.js +92 -0
  26. package/dist/iterable-queue-mapper-simple.js.map +1 -0
  27. package/dist/iterable-queue-mapper-simple.test.d.ts +2 -0
  28. package/dist/iterable-queue-mapper-simple.test.d.ts.map +1 -0
  29. package/dist/iterable-queue-mapper-simple.test.js +103 -0
  30. package/dist/iterable-queue-mapper-simple.test.js.map +1 -0
  31. package/dist/iterable-queue-mapper.d.ts +73 -0
  32. package/dist/iterable-queue-mapper.d.ts.map +1 -0
  33. package/dist/iterable-queue-mapper.js +72 -0
  34. package/dist/iterable-queue-mapper.js.map +1 -0
  35. package/dist/iterable-queue-mapper.test.d.ts +2 -0
  36. package/dist/iterable-queue-mapper.test.d.ts.map +1 -0
  37. package/dist/iterable-queue-mapper.test.js +174 -0
  38. package/dist/iterable-queue-mapper.test.js.map +1 -0
  39. package/dist/iterable-queue.d.ts +26 -0
  40. package/dist/iterable-queue.d.ts.map +1 -0
  41. package/dist/iterable-queue.js +34 -0
  42. package/dist/iterable-queue.js.map +1 -0
  43. package/dist/iterable-queue.test.d.ts +2 -0
  44. package/dist/iterable-queue.test.d.ts.map +1 -0
  45. package/dist/iterable-queue.test.js +252 -0
  46. package/dist/iterable-queue.test.js.map +1 -0
  47. package/dist/queue.d.ts +9 -0
  48. package/dist/queue.d.ts.map +1 -0
  49. package/dist/queue.js +33 -0
  50. package/dist/queue.js.map +1 -0
  51. package/dist/queue.test.d.ts +2 -0
  52. package/dist/queue.test.d.ts.map +1 -0
  53. package/dist/queue.test.js +75 -0
  54. package/dist/queue.test.js.map +1 -0
  55. package/package.json +45 -0
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /// <reference types="jest" />
5
+ const iterable_queue_mapper_1 = require("./iterable-queue-mapper");
6
+ const util_1 = require("util");
7
+ const sleep = (0, util_1.promisify)(setTimeout);
8
+ function mapper({ value, ms }) {
9
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
10
+ yield sleep(ms);
11
+ return value;
12
+ });
13
+ }
14
+ describe('IterableQueueMapper', () => {
15
+ beforeAll(() => {
16
+ // nothing
17
+ });
18
+ beforeEach(() => {
19
+ jest.clearAllMocks();
20
+ });
21
+ describe('simple tests', () => {
22
+ it('simple mapper works - sync mapper', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
23
+ var _a, e_1, _b, _c;
24
+ const max = 100;
25
+ let total = 0;
26
+ let callCount = 0;
27
+ const prefetcher = new iterable_queue_mapper_1.IterableQueueMapper((value) => {
28
+ callCount++;
29
+ total += value;
30
+ return total;
31
+ });
32
+ // Enqueue items in the background
33
+ void (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
34
+ for (let i = 0; i < max; i++) {
35
+ yield sleep(1 * (i % 10));
36
+ yield prefetcher.enqueue(i + 1);
37
+ }
38
+ prefetcher.done();
39
+ }))();
40
+ let lastTotal = 0;
41
+ let loopCount = 0;
42
+ try {
43
+ 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;) {
44
+ _c = prefetcher_1_1.value;
45
+ _d = false;
46
+ try {
47
+ const item = _c;
48
+ loopCount++;
49
+ if (item > lastTotal) {
50
+ lastTotal = item;
51
+ }
52
+ }
53
+ finally {
54
+ _d = true;
55
+ }
56
+ }
57
+ }
58
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
59
+ finally {
60
+ try {
61
+ if (!_d && !_a && (_b = prefetcher_1.return)) yield _b.call(prefetcher_1);
62
+ }
63
+ finally { if (e_1) throw e_1.error; }
64
+ }
65
+ expect(callCount).toBe(max);
66
+ expect(loopCount).toBe(max);
67
+ expect(total).toBe((max * (max + 1)) / 2);
68
+ expect(lastTotal).toBe((max * (max + 1)) / 2);
69
+ }));
70
+ it('right number run in parallel - simple', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
71
+ var _e, e_2, _f, _g;
72
+ const startTime = Date.now();
73
+ const max = 8;
74
+ const delayBetweenMs = 200;
75
+ const input = [
76
+ { value: 1, ms: delayBetweenMs },
77
+ { value: 2, ms: delayBetweenMs },
78
+ { value: 3, ms: delayBetweenMs },
79
+ { value: 4, ms: delayBetweenMs },
80
+ { value: 5, ms: delayBetweenMs },
81
+ { value: 6, ms: delayBetweenMs },
82
+ { value: 7, ms: delayBetweenMs },
83
+ { value: 8, ms: delayBetweenMs },
84
+ ];
85
+ const prefetcher = new iterable_queue_mapper_1.IterableQueueMapper(mapper, { concurrency: 2, maxUnread: 4 });
86
+ // Enqueue items in the background
87
+ void (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
88
+ for (const item of input) {
89
+ yield prefetcher.enqueue(item);
90
+ }
91
+ prefetcher.done();
92
+ }))();
93
+ let lastTotal = 0;
94
+ let loopCount = 0;
95
+ try {
96
+ 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;) {
97
+ _g = prefetcher_2_1.value;
98
+ _h = false;
99
+ try {
100
+ const item = _g;
101
+ loopCount++;
102
+ if (item > lastTotal) {
103
+ lastTotal = item;
104
+ }
105
+ }
106
+ finally {
107
+ _h = true;
108
+ }
109
+ }
110
+ }
111
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
112
+ finally {
113
+ try {
114
+ if (!_h && !_e && (_f = prefetcher_2.return)) yield _f.call(prefetcher_2);
115
+ }
116
+ finally { if (e_2) throw e_2.error; }
117
+ }
118
+ expect(loopCount).toBe(max);
119
+ expect(lastTotal).toBe(max);
120
+ // Should require at least 4 batches
121
+ expect(Date.now() - startTime).toBeLessThan(5 * delayBetweenMs);
122
+ expect(Date.now() - startTime).toBeGreaterThan(4 * delayBetweenMs);
123
+ }));
124
+ it('right number run in parallel - complex', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
125
+ var _j, e_3, _k, _l;
126
+ const prefetcher = new iterable_queue_mapper_1.IterableQueueMapper(mapper, { concurrency: 4, maxUnread: 4 });
127
+ const delayBetweenMs = 500;
128
+ // First 4 added should not wait at all
129
+ const startTime = Date.now();
130
+ yield prefetcher.enqueue({ value: 1, ms: delayBetweenMs });
131
+ yield prefetcher.enqueue({ value: 2, ms: delayBetweenMs });
132
+ yield prefetcher.enqueue({ value: 3, ms: delayBetweenMs });
133
+ yield prefetcher.enqueue({ value: 4, ms: delayBetweenMs });
134
+ expect(Date.now() - startTime).toBeLessThan(delayBetweenMs);
135
+ // Next one added should have had to wait for at least one wait period
136
+ void (() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
137
+ yield prefetcher.enqueue({ value: 5, ms: delayBetweenMs });
138
+ expect(Date.now() - startTime).toBeGreaterThanOrEqual(delayBetweenMs);
139
+ prefetcher.done();
140
+ }))();
141
+ let lastSeen = 0;
142
+ let loopCount = 0;
143
+ try {
144
+ for (var _m = true, prefetcher_3 = tslib_1.__asyncValues(prefetcher), prefetcher_3_1; prefetcher_3_1 = yield prefetcher_3.next(), _j = prefetcher_3_1.done, !_j;) {
145
+ _l = prefetcher_3_1.value;
146
+ _m = false;
147
+ try {
148
+ const item = _l;
149
+ loopCount++;
150
+ if (item > lastSeen) {
151
+ lastSeen = item;
152
+ }
153
+ }
154
+ finally {
155
+ _m = true;
156
+ }
157
+ }
158
+ }
159
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
160
+ finally {
161
+ try {
162
+ if (!_m && !_j && (_k = prefetcher_3.return)) yield _k.call(prefetcher_3);
163
+ }
164
+ finally { if (e_3) throw e_3.error; }
165
+ }
166
+ expect(loopCount).toBe(5);
167
+ expect(lastSeen).toBe(5);
168
+ // Should require at least 2 batches
169
+ expect(Date.now() - startTime).toBeLessThan(2.5 * delayBetweenMs);
170
+ expect(Date.now() - startTime).toBeGreaterThan(2 * delayBetweenMs);
171
+ }));
172
+ });
173
+ });
174
+ //# sourceMappingURL=iterable-queue-mapper.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iterable-queue-mapper.test.js","sourceRoot":"","sources":["../src/iterable-queue-mapper.test.ts"],"names":[],"mappings":";;;AAAA,8BAA8B;AAC9B,mEAA8D;AAC9D,+BAAiC;AACjC,MAAM,KAAK,GAAG,IAAA,gBAAS,EAAC,UAAU,CAAC,CAAC;AAEpC,SAAe,MAAM,CAAC,EAAE,KAAK,EAAE,EAAE,EAAiC;;QAChE,MAAM,KAAK,CAAC,EAAE,CAAC,CAAC;QAChB,OAAO,KAAK,CAAC;IACf,CAAC;CAAA;AAED,QAAQ,CAAC,qBAAqB,EAAE,GAA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AoC;YACpC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,CAAC,YAAY,CAAC,GAAG,GAAG,cAAc,CAAC,CAAC;YAClE,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,CAAC,eAAe,CAAC,CAAC,GAAG,cAAc,CAAC,CAAC;QACrE,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { BlockingQueue } from './blocking-queue';
2
+ interface IterableQueueOptions {
3
+ /**
4
+ * Number of pending unread iterable items.
5
+ *
6
+ * Must be an integer from 0 and up or `Infinity`, must be >= `concurrency`.
7
+ *
8
+ * @default 8
9
+ */
10
+ readonly maxUnread?: number;
11
+ }
12
+ export declare class IterableQueue<Element> extends BlockingQueue<Element> implements AsyncIterable<Element> {
13
+ /**
14
+ *
15
+ */
16
+ constructor(options?: IterableQueueOptions);
17
+ [Symbol.asyncIterator](): AsyncIterator<Element>;
18
+ /**
19
+ * Used by the iterator returned from [Symbol.asyncIterator]
20
+ * Called every time an item is needed
21
+ * @returns Iterator result
22
+ */
23
+ next(): Promise<IteratorResult<Element>>;
24
+ }
25
+ export {};
26
+ //# sourceMappingURL=iterable-queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iterable-queue.d.ts","sourceRoot":"","sources":["../src/iterable-queue.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,UAAU,oBAAoB;IAC5B;;;;;;OAMG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,qBAAa,aAAa,CAAC,OAAO,CAChC,SAAQ,aAAa,CAAC,OAAO,CAC7B,YAAW,aAAa,CAAC,OAAO,CAAC;IAEjC;;OAEG;gBACS,OAAO,GAAE,oBAAyB;IAIvC,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,OAAO,CAAC;IAIvD;;;;OAIG;IACU,IAAI,IAAI,OAAO,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;CAWtD"}
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IterableQueue = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const blocking_queue_1 = require("./blocking-queue");
6
+ class IterableQueue extends blocking_queue_1.BlockingQueue {
7
+ /**
8
+ *
9
+ */
10
+ constructor(options = {}) {
11
+ super(options);
12
+ }
13
+ [Symbol.asyncIterator]() {
14
+ return this;
15
+ }
16
+ /**
17
+ * Used by the iterator returned from [Symbol.asyncIterator]
18
+ * Called every time an item is needed
19
+ * @returns Iterator result
20
+ */
21
+ next() {
22
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
23
+ // Check if queue has an item
24
+ const item = yield this.dequeue();
25
+ if (item === undefined) {
26
+ // We finished - There were no more items
27
+ return { value: undefined, done: true };
28
+ }
29
+ return { value: item, done: false };
30
+ });
31
+ }
32
+ }
33
+ exports.IterableQueue = IterableQueue;
34
+ //# sourceMappingURL=iterable-queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iterable-queue.js","sourceRoot":"","sources":["../src/iterable-queue.ts"],"names":[],"mappings":";;;;AAAA,qDAAiD;AAajD,MAAa,aACX,SAAQ,8BAAsB;IAG9B;;OAEG;IACH,YAAY,UAAgC,EAAE;QAC5C,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;IAEM,CAAC,MAAM,CAAC,aAAa,CAAC;QAC3B,OAAO,IAAI,CAAC;IACd,CAAC;IAED;;;;OAIG;IACU,IAAI;;YACf,6BAA6B;YAC7B,MAAM,IAAI,GAAwB,MAAM,IAAI,CAAC,OAAO,EAAE,CAAC;YAEvD,IAAI,IAAI,KAAK,SAAS,EAAE;gBACtB,yCAAyC;gBACzC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;aACzC;YAED,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;QACtC,CAAC;KAAA;CACF;AA/BD,sCA+BC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=iterable-queue.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iterable-queue.test.d.ts","sourceRoot":"","sources":["../src/iterable-queue.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ /// <reference types="jest" />
5
+ const iterable_queue_1 = require("./iterable-queue");
6
+ describe('IterableQueue', () => {
7
+ beforeAll(() => {
8
+ // nothing
9
+ });
10
+ beforeEach(() => {
11
+ jest.clearAllMocks();
12
+ });
13
+ describe('maxUnread: 0', () => {
14
+ it('single item enqueue / iterate works and ends', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
15
+ var _a, e_1, _b, _c;
16
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 0 });
17
+ void queue.enqueue(1);
18
+ queue.done();
19
+ let iteratedCount = 0;
20
+ try {
21
+ for (var _d = true, queue_1 = tslib_1.__asyncValues(queue), queue_1_1; queue_1_1 = yield queue_1.next(), _a = queue_1_1.done, !_a;) {
22
+ _c = queue_1_1.value;
23
+ _d = false;
24
+ try {
25
+ const item = _c;
26
+ expect(item).toBe(1);
27
+ iteratedCount++;
28
+ }
29
+ finally {
30
+ _d = true;
31
+ }
32
+ }
33
+ }
34
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
35
+ finally {
36
+ try {
37
+ if (!_d && !_a && (_b = queue_1.return)) yield _b.call(queue_1);
38
+ }
39
+ finally { if (e_1) throw e_1.error; }
40
+ }
41
+ expect(iteratedCount).toBe(1);
42
+ }));
43
+ it('full queue blocks enqueue until dequeue', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
44
+ var _e, e_2, _f, _g;
45
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 0 });
46
+ const items = [1, 2];
47
+ // Add first item
48
+ void queue.enqueue(items[0]);
49
+ // Do not wait for second item to add (it will wait until an enqueue completes)
50
+ setTimeout(() => {
51
+ void queue.enqueue(items[1]);
52
+ }, 2000);
53
+ setTimeout(() => {
54
+ queue.done();
55
+ }, 3000);
56
+ const startTime = Date.now();
57
+ let iteratedCount = 0;
58
+ try {
59
+ for (var _h = true, queue_2 = tslib_1.__asyncValues(queue), queue_2_1; queue_2_1 = yield queue_2.next(), _e = queue_2_1.done, !_e;) {
60
+ _g = queue_2_1.value;
61
+ _h = false;
62
+ try {
63
+ const item = _g;
64
+ expect(item).toBe(items[iteratedCount]);
65
+ iteratedCount++;
66
+ }
67
+ finally {
68
+ _h = true;
69
+ }
70
+ }
71
+ }
72
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
73
+ finally {
74
+ try {
75
+ if (!_h && !_e && (_f = queue_2.return)) yield _f.call(queue_2);
76
+ }
77
+ finally { if (e_2) throw e_2.error; }
78
+ }
79
+ expect(iteratedCount).toBe(2);
80
+ const duration = Date.now() - startTime;
81
+ expect(duration).toBeGreaterThanOrEqual(3000);
82
+ }));
83
+ it('mixed dequeue / iterator works', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
84
+ var _j, e_3, _k, _l;
85
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 0 });
86
+ const items = [1, 2];
87
+ // Wait for first item to add
88
+ void queue.enqueue(items[0]);
89
+ // Do not wait for second item to add (it will wait until an enqueue completes)
90
+ setTimeout(() => {
91
+ void queue.enqueue(items[1]);
92
+ }, 2000);
93
+ setTimeout(() => {
94
+ queue.done();
95
+ }, 3000);
96
+ const startTime = Date.now();
97
+ let iteratedCount = 0;
98
+ const firstItem = yield queue.dequeue();
99
+ expect(firstItem).toBe(items[iteratedCount]);
100
+ iteratedCount++;
101
+ try {
102
+ for (var _m = true, queue_3 = tslib_1.__asyncValues(queue), queue_3_1; queue_3_1 = yield queue_3.next(), _j = queue_3_1.done, !_j;) {
103
+ _l = queue_3_1.value;
104
+ _m = false;
105
+ try {
106
+ const item = _l;
107
+ expect(item).toBe(items[iteratedCount]);
108
+ iteratedCount++;
109
+ expect(Date.now() - startTime).toBeGreaterThanOrEqual(2000);
110
+ }
111
+ finally {
112
+ _m = true;
113
+ }
114
+ }
115
+ }
116
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
117
+ finally {
118
+ try {
119
+ if (!_m && !_j && (_k = queue_3.return)) yield _k.call(queue_3);
120
+ }
121
+ finally { if (e_3) throw e_3.error; }
122
+ }
123
+ expect(iteratedCount).toBe(2);
124
+ const duration = Date.now() - startTime;
125
+ expect(duration).toBeGreaterThanOrEqual(3000);
126
+ }));
127
+ });
128
+ describe('maxUnread: 1', () => {
129
+ it('single item enqueue / iterate works and ends', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
130
+ var _a, e_4, _b, _c;
131
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 1 });
132
+ yield queue.enqueue(1);
133
+ queue.done();
134
+ let iteratedCount = 0;
135
+ try {
136
+ for (var _d = true, queue_4 = tslib_1.__asyncValues(queue), queue_4_1; queue_4_1 = yield queue_4.next(), _a = queue_4_1.done, !_a;) {
137
+ _c = queue_4_1.value;
138
+ _d = false;
139
+ try {
140
+ const item = _c;
141
+ expect(item).toBe(1);
142
+ iteratedCount++;
143
+ }
144
+ finally {
145
+ _d = true;
146
+ }
147
+ }
148
+ }
149
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
150
+ finally {
151
+ try {
152
+ if (!_d && !_a && (_b = queue_4.return)) yield _b.call(queue_4);
153
+ }
154
+ finally { if (e_4) throw e_4.error; }
155
+ }
156
+ expect(iteratedCount).toBe(1);
157
+ }));
158
+ it('enqueue after done throws', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
159
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 1 });
160
+ yield queue.enqueue(1);
161
+ const item = yield queue.dequeue();
162
+ queue.done();
163
+ yield expect(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () { return queue.enqueue(2); })).rejects.toThrowError('`enqueue` called after `done` called');
164
+ expect(item).toBe(1);
165
+ }));
166
+ it('full queue blocks enqueue until dequeue', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
167
+ var _e, e_5, _f, _g;
168
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 1 });
169
+ const items = [1, 2];
170
+ // Wait for first item to add
171
+ yield queue.enqueue(items[0]);
172
+ // Do not wait for second item to add (it will wait until an enqueue completes)
173
+ setTimeout(() => {
174
+ void queue.enqueue(items[1]);
175
+ }, 2000);
176
+ setTimeout(() => {
177
+ queue.done();
178
+ }, 3000);
179
+ const startTime = Date.now();
180
+ let iteratedCount = 0;
181
+ try {
182
+ for (var _h = true, queue_5 = tslib_1.__asyncValues(queue), queue_5_1; queue_5_1 = yield queue_5.next(), _e = queue_5_1.done, !_e;) {
183
+ _g = queue_5_1.value;
184
+ _h = false;
185
+ try {
186
+ const item = _g;
187
+ expect(item).toBe(items[iteratedCount]);
188
+ iteratedCount++;
189
+ }
190
+ finally {
191
+ _h = true;
192
+ }
193
+ }
194
+ }
195
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
196
+ finally {
197
+ try {
198
+ if (!_h && !_e && (_f = queue_5.return)) yield _f.call(queue_5);
199
+ }
200
+ finally { if (e_5) throw e_5.error; }
201
+ }
202
+ expect(iteratedCount).toBe(2);
203
+ const duration = Date.now() - startTime;
204
+ expect(duration).toBeGreaterThanOrEqual(3000);
205
+ }));
206
+ it('mixed dequeue / iterator works', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
207
+ var _j, e_6, _k, _l;
208
+ const queue = new iterable_queue_1.IterableQueue({ maxUnread: 1 });
209
+ const items = [1, 2];
210
+ // Wait for first item to add
211
+ yield queue.enqueue(items[0]);
212
+ // Do not wait for second item to add (it will wait until an enqueue completes)
213
+ setTimeout(() => {
214
+ void queue.enqueue(items[1]);
215
+ }, 2000);
216
+ setTimeout(() => {
217
+ queue.done();
218
+ }, 3000);
219
+ const startTime = Date.now();
220
+ let iteratedCount = 0;
221
+ const firstItem = yield queue.dequeue();
222
+ expect(firstItem).toBe(items[iteratedCount]);
223
+ iteratedCount++;
224
+ try {
225
+ for (var _m = true, queue_6 = tslib_1.__asyncValues(queue), queue_6_1; queue_6_1 = yield queue_6.next(), _j = queue_6_1.done, !_j;) {
226
+ _l = queue_6_1.value;
227
+ _m = false;
228
+ try {
229
+ const item = _l;
230
+ expect(item).toBe(items[iteratedCount]);
231
+ iteratedCount++;
232
+ expect(Date.now() - startTime).toBeGreaterThanOrEqual(2000);
233
+ }
234
+ finally {
235
+ _m = true;
236
+ }
237
+ }
238
+ }
239
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
240
+ finally {
241
+ try {
242
+ if (!_m && !_j && (_k = queue_6.return)) yield _k.call(queue_6);
243
+ }
244
+ finally { if (e_6) throw e_6.error; }
245
+ }
246
+ expect(iteratedCount).toBe(2);
247
+ const duration = Date.now() - startTime;
248
+ expect(duration).toBeGreaterThanOrEqual(3000);
249
+ }));
250
+ });
251
+ });
252
+ //# sourceMappingURL=iterable-queue.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"iterable-queue.test.js","sourceRoot":"","sources":["../src/iterable-queue.test.ts"],"names":[],"mappings":";;;AAAA,8BAA8B;AAC9B,qDAAiD;AAEjD,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,SAAS,CAAC,GAAG,EAAE;QACb,UAAU;IACZ,CAAC,CAAC,CAAC;IAEH,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,CAAC,aAAa,EAAE,CAAC;IACvB,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,cAAc,EAAE,GAAG,EAAE;QAC5B,EAAE,CAAC,8CAA8C,EAAE,GAAS,EAAE;;YAC5D,MAAM,KA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AAK;oBAAL,WAAK;;wBAAnB,MAAM,IAAI,KAAA,CAAA;wBACnB,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC;wBACxC,aAAa,EAAE,CAAC;wBAChB,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,CAAC,sBAAsB,CAAC,IAAI,CAAC,CAAC;;;;;iBAC7D;;;;;;;;;YAED,MAAM,CAAC,aAAa,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YAE9B,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;YACxC,MAAM,CAAC,QAAQ,CAAC,CAAC,sBAAsB,CAAC,IAAI,CAAC,CAAC;QAChD,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -0,0 +1,9 @@
1
+ export declare class Queue<T> {
2
+ private _queue;
3
+ private _tail;
4
+ private _head;
5
+ get length(): number;
6
+ enqueue(element: T): void;
7
+ dequeue(): T | undefined;
8
+ }
9
+ //# sourceMappingURL=queue.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.d.ts","sourceRoot":"","sources":["../src/queue.ts"],"names":[],"mappings":"AAIA,qBAAa,KAAK,CAAC,CAAC;IAClB,OAAO,CAAC,MAAM,CAA8B;IAC5C,OAAO,CAAC,KAAK,CAAK;IAClB,OAAO,CAAC,KAAK,CAAK;IAElB,IAAW,MAAM,IAAI,MAAM,CAE1B;IAGM,OAAO,CAAC,OAAO,EAAE,CAAC,GAAG,IAAI;IAQzB,OAAO,IAAI,CAAC,GAAG,SAAS;CAOhC"}
package/dist/queue.js ADDED
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ //
3
+ // 2021-08-27 - Based on simple example here: https://www.telerik.com/blogs/stack-queue-javascript
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Queue = void 0;
7
+ class Queue {
8
+ constructor() {
9
+ this._queue = {};
10
+ this._tail = 0;
11
+ this._head = 0;
12
+ }
13
+ get length() {
14
+ return this._tail - this._head;
15
+ }
16
+ // Add an element to the end of the queue.
17
+ enqueue(element) {
18
+ if (element === undefined) {
19
+ throw new TypeError('cannot enqueue `undefined`');
20
+ }
21
+ this._queue[this._tail++] = element;
22
+ }
23
+ // Delete the first element of the queue.
24
+ dequeue() {
25
+ if (this._tail === this._head)
26
+ return undefined;
27
+ const element = this._queue[this._head];
28
+ delete this._queue[this._head++];
29
+ return element;
30
+ }
31
+ }
32
+ exports.Queue = Queue;
33
+ //# sourceMappingURL=queue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.js","sourceRoot":"","sources":["../src/queue.ts"],"names":[],"mappings":";AAAA,EAAE;AACF,kGAAkG;AAClG,EAAE;;;AAEF,MAAa,KAAK;IAAlB;QACU,WAAM,GAA2B,EAAE,CAAC;QACpC,UAAK,GAAG,CAAC,CAAC;QACV,UAAK,GAAG,CAAC,CAAC;IAsBpB,CAAC;IApBC,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;IACjC,CAAC;IAED,0CAA0C;IACnC,OAAO,CAAC,OAAU;QACvB,IAAI,OAAO,KAAK,SAAS,EAAE;YACzB,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAC;SACnD;QACD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,OAAO,CAAC;IACtC,CAAC;IAED,yCAAyC;IAClC,OAAO;QACZ,IAAI,IAAI,CAAC,KAAK,KAAK,IAAI,CAAC,KAAK;YAAE,OAAO,SAAS,CAAC;QAEhD,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACxC,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QACjC,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAzBD,sBAyBC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=queue.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queue.test.d.ts","sourceRoot":"","sources":["../src/queue.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /// <reference types="jest" />
4
+ const queue_1 = require("./queue");
5
+ describe('Queue', () => {
6
+ // const sourceNextSpy = jest.spyOn(pMapIterable.prototype as any, 'sourceNext');
7
+ // const startAnotherRunnerSpy = jest.spyOn(pMapIterable.prototype as any, 'startAnotherRunner');
8
+ beforeAll(() => {
9
+ // nothing
10
+ });
11
+ beforeEach(() => {
12
+ jest.clearAllMocks();
13
+ });
14
+ it('dequeue empty returns undefined', () => {
15
+ const q = new queue_1.Queue();
16
+ expect(q.dequeue()).toBeUndefined();
17
+ });
18
+ it('enqueue undefined throws', () => {
19
+ const q = new queue_1.Queue();
20
+ expect(() => q.enqueue(undefined)).toThrowError('cannot enqueue `undefined`');
21
+ expect(q.length).toBe(0);
22
+ });
23
+ it('enqueue, dequeue, dequeue returns undefined', () => {
24
+ const q = new queue_1.Queue();
25
+ q.enqueue('one');
26
+ expect(q.length).toBe(1);
27
+ expect(q.dequeue()).toBe('one');
28
+ expect(q.length).toBe(0);
29
+ expect(q.dequeue()).toBeUndefined();
30
+ });
31
+ it('ordering of several items is correct', () => {
32
+ const input = [1, 2, 3, 4, 5, 6];
33
+ const q = new queue_1.Queue();
34
+ for (const item of input) {
35
+ q.enqueue(item);
36
+ }
37
+ const inputCount = input.length;
38
+ let loopCount = 0;
39
+ // eslint-disable-next-line no-constant-condition
40
+ while (true) {
41
+ const item = q.dequeue();
42
+ if (item === undefined)
43
+ break;
44
+ expect(q.length).toBe(inputCount - loopCount - 1);
45
+ loopCount++;
46
+ expect(item).toBe(input.shift());
47
+ }
48
+ expect(loopCount).toBe(inputCount);
49
+ expect(q.length).toBe(0);
50
+ });
51
+ it('interleaved enqueue/dequeue work correctly', () => {
52
+ const q = new queue_1.Queue();
53
+ const totalValues = 1000;
54
+ let value = 0;
55
+ let expectedRemovedValue = 0;
56
+ for (let i = 0; i < totalValues; i++) {
57
+ // 2 forward, 1 back
58
+ expect(q.length).toBe(i);
59
+ q.enqueue(value++); // 0 first loop
60
+ expect(q.length).toBe(i + 1);
61
+ q.enqueue(value++); // 1 first loop
62
+ expect(q.length).toBe(i + 2);
63
+ expect(q.dequeue()).toBe(expectedRemovedValue++); // 0 first loop
64
+ expect(q.length).toBe(i + 1);
65
+ }
66
+ expect(q.length).toBe(totalValues);
67
+ // Remove all the rest of the items
68
+ for (let i = 0; i < totalValues; i++) {
69
+ expect(q.dequeue()).toBe(expectedRemovedValue++); // totalValues first loop
70
+ }
71
+ expect(q.length).toBe(0);
72
+ expect(q.dequeue()).toBeUndefined();
73
+ });
74
+ });
75
+ //# sourceMappingURL=queue.test.js.map