aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/UtilExtensions.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util/UtilExtensions.ts')
-rw-r--r--src/client/util/UtilExtensions.ts39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/client/util/UtilExtensions.ts b/src/client/util/UtilExtensions.ts
new file mode 100644
index 000000000..1e277b242
--- /dev/null
+++ b/src/client/util/UtilExtensions.ts
@@ -0,0 +1,39 @@
+module.exports.Batch = async function <T>(parameters: BatchParameters<T>) {
+ const { size, action } = parameters;
+ const batches: T[][] = [];
+ let i = 0;
+ while (i < this.length) {
+ const cap = Math.min(i + size, this.length);
+ batches.push(this.slice(i, cap));
+ i = cap;
+ }
+ console.log(`Beginning action on ${this.length} elements, split into ${batches.length} groups => ${batches.map(batch => batch.length).join(", ")}`);
+ if (action) {
+ const { handler, interval } = action;
+ if (!interval || batches.length === 1) {
+ for (let batch of batches) {
+ await handler(batch);
+ }
+ } else {
+ return new Promise<T[][]>(resolve => {
+ const iterator = batches[Symbol.iterator]();
+ const quota = batches.length;
+ let completed = 0;
+ const tag = setInterval(async () => {
+ const next = iterator.next();
+ if (next.done) {
+ clearInterval(tag);
+ return;
+ }
+ const batch = next.value;
+ console.log(`Handling next batch with ${batch.length} elements`);
+ await handler(batch);
+ if (++completed === quota) {
+ resolve(batches);
+ }
+ }, interval);
+ });
+ }
+ }
+ return batches;
+}; \ No newline at end of file