aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/UtilExtensions.ts
blob: 792cd00a172f95b37f7882472ebb189ff44e41c5 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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>(batchSize: number, handler: BatchHandlerSync<I>): void {
    if (this.length) {
        let completed = 0;
        const batches = this.batch(batchSize);
        const quota = batches.length;
        for (let batch of batches) {
            const context: BatchContext = {
                completedBatches: completed,
                remainingBatches: quota - completed,
                isFullBatch: batch.length === batchSize
            };
            handler(batch, context);
            completed++;
        }
    }
};

module.exports.ConvertInBatches = function <I, O>(batchSize: number, handler: BatchConverterSync<I, O>): O[] {
    if (!this.length) {
        return [];
    }
    let collector: O[] = [];
    let completed = 0;
    const batches = this.batch(batchSize);
    const quota = batches.length;
    for (let batch of batches) {
        const context: BatchContext = {
            completedBatches: completed,
            remainingBatches: quota - completed,
            isFullBatch: batch.length === batchSize
        };
        collector.push(...handler(batch, context));
        completed++;
    }
    return collector;
};

module.exports.ExecuteInBatchesAsync = async function <I>(batchSize: number, handler: BatchHandler<I>): Promise<void> {
    if (this.length) {
        let completed = 0;
        const batches = this.batch(batchSize);
        const quota = batches.length;
        for (let batch of batches) {
            const context: BatchContext = {
                completedBatches: completed,
                remainingBatches: quota - completed,
                isFullBatch: batch.length === batchSize
            };
            await handler(batch, context);
            completed++;
        }
    }
};

module.exports.ConvertInBatchesAsync = async function <I, O>(batchSize: number, handler: BatchConverter<I, O>): Promise<O[]> {
    if (!this.length) {
        return [];
    }
    let collector: O[] = [];
    let completed = 0;
    const batches = this.batch(batchSize);
    const quota = batches.length;
    for (let batch of batches) {
        const context: BatchContext = {
            completedBatches: completed,
            remainingBatches: quota - completed,
            isFullBatch: batch.length === batchSize
        };
        collector.push(...(await handler(batch, context)));
        completed++;
    }
    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);
    const quota = batches.length;
    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 context: BatchContext = {
                        completedBatches: completed,
                        remainingBatches: quota - completed,
                        isFullBatch: batch.length === batchSize
                    };
                    await handler(batch, context);
                    resolve();
                }, interval * 1000);
            });
            if (++completed === quota) {
                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);
    const quota = batches.length;
    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 context: BatchContext = {
                        completedBatches: completed,
                        remainingBatches: quota - completed,
                        isFullBatch: batch.length === batchSize
                    };
                    collector.push(...(await handler(batch, context)));
                    resolve();
                }, interval * 1000);
            });
            if (++completed === quota) {
                resolve(collector);
                break;
            }
        }
    });
};