1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
module.exports.Batch = function <T>(batchSize: number): T[][] {
const batches: T[][] = [];
let i = 0;
while (i < this.length) {
const cap = Math.min(i + batchSize, this.length);
batches.push(this.slice(i, cap));
i = cap;
}
return batches;
};
module.exports.ExecuteBatches = function <I, O>(batchSize: number, handler: BatchHandlerSync<I>): void {
if (this.length) {
for (let batch of this.batch(batchSize)) {
const isFullBatch = batch.length === batchSize;
handler(batch, isFullBatch);
}
}
};
module.exports.ConvertInBatches = function <I, O>(batchSize: number, handler: BatchConverterSync<I, O>): O[] {
if (!this.length) {
return [];
}
let collector: O[] = [];
for (let batch of this.batch(batchSize)) {
const isFullBatch = batch.length === batchSize;
collector.push(...handler(batch, isFullBatch));
}
return collector;
};
module.exports.ExecuteInBatchesAsync = async function <I>(batchSize: number, handler: BatchHandler<I>): Promise<void> {
if (this.length) {
for (let batch of this.batch(batchSize)) {
const isFullBatch = batch.length === batchSize;
await handler(batch, isFullBatch);
}
}
};
module.exports.ConvertInBatchesAsync = async function <I, O>(batchSize: number, handler: BatchConverter<I, O>): Promise<O[]> {
if (!this.length) {
return [];
}
let collector: O[] = [];
for (let batch of this.batch(batchSize)) {
const isFullBatch = batch.length === batchSize;
collector.push(...(await handler(batch, isFullBatch)));
}
return collector;
};
module.exports.ExecuteInBatchesAtInterval = async function <I>(batchSize: number, handler: BatchHandler<I>, interval: number): Promise<void> {
if (!this.length) {
return;
}
const batches = this.batch(batchSize);
return new Promise<void>(async resolve => {
const iterator = batches[Symbol.iterator]();
let completed = 0;
while (true) {
const next = iterator.next();
await new Promise<void>(resolve => {
setTimeout(async () => {
const batch = next.value;
const isFullBatch = batch.length === batchSize;
await handler(batch, isFullBatch);
resolve();
}, interval * 1000);
});
if (++completed === batches.length) {
break;
}
}
resolve();
});
};
module.exports.ConvertInBatchesAtInterval = async function <I, O>(batchSize: number, handler: BatchConverter<I, O>, interval: number): Promise<O[]> {
if (!this.length) {
return [];
}
let collector: O[] = [];
const batches = this.batch(batchSize);
return new Promise<O[]>(async resolve => {
const iterator = batches[Symbol.iterator]();
let completed = 0;
while (true) {
const next = iterator.next();
await new Promise<void>(resolve => {
setTimeout(async () => {
const batch = next.value;
const isFullBatch = batch.length === batchSize;
collector.push(...(await handler(batch, isFullBatch)));
resolve();
}, interval * 1000);
});
if (++completed === batches.length) {
resolve(collector);
break;
}
}
});
};
|