aboutsummaryrefslogtreecommitdiff
path: root/src/client/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util')
-rw-r--r--src/client/util/Import & Export/DirectoryImportBox.tsx4
-rw-r--r--src/client/util/UtilExtensions.ts92
2 files changed, 74 insertions, 22 deletions
diff --git a/src/client/util/Import & Export/DirectoryImportBox.tsx b/src/client/util/Import & Export/DirectoryImportBox.tsx
index 1ae0e7525..a625e06c0 100644
--- a/src/client/util/Import & Export/DirectoryImportBox.tsx
+++ b/src/client/util/Import & Export/DirectoryImportBox.tsx
@@ -95,7 +95,7 @@ export default class DirectoryImportBox extends React.Component<FieldViewProps>
let sizes: number[] = [];
let modifiedDates: number[] = [];
- const localUpload = async (batch: File[]) => {
+ const uploadLocally = async (batch: File[]) => {
sizes.push(...batch.map(file => file.size));
modifiedDates.push(...batch.map(file => file.lastModified));
@@ -107,7 +107,7 @@ export default class DirectoryImportBox extends React.Component<FieldViewProps>
return (await fetch(Utils.prepend(RouteStore.upload), parameters)).json();
};
- const uploads = await validated.batchAction<FileResponse>(15, localUpload);
+ const uploads = await validated.convertInBatchesAsync<FileResponse>(15, uploadLocally);
await Promise.all(uploads.map(async upload => {
const type = upload.type;
diff --git a/src/client/util/UtilExtensions.ts b/src/client/util/UtilExtensions.ts
index 0bf9f4e97..3eeec6ca7 100644
--- a/src/client/util/UtilExtensions.ts
+++ b/src/client/util/UtilExtensions.ts
@@ -9,33 +9,85 @@ module.exports.Batch = function <T>(batchSize: number): T[][] {
return batches;
};
-module.exports.BatchAction = async function <I, O>(batchSize: number, handler: BatchHandler<I, O>, interval?: number): Promise<O[]> {
+module.exports.ExecuteBatches = function <I, O>(batchSize: number, handler: BatchHandlerSync<I>): void {
+ if (this.length) {
+ for (let batch of this.batch(batchSize)) {
+ handler(batch);
+ }
+ }
+};
+
+module.exports.ConvertInBatches = function <I, O>(batchSize: number, handler: BatchConverterSync<I, O>): O[] {
if (!this.length) {
return [];
}
let collector: O[] = [];
- const batches = this.batch(batchSize);
- if (!interval || batches.length === 1) {
- for (let batch of batches) {
- collector.push(...(await handler(batch)));
+ for (let batch of this.batch(batchSize)) {
+ collector.push(...handler(batch));
+ }
+ 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)) {
+ await handler(batch);
}
- } else {
- return new Promise<O[]>(resolve => {
- const iterator = batches[Symbol.iterator]();
- let completed = 0;
- const tag = setInterval(async () => {
- const next = iterator.next();
- if (next.done) {
- clearInterval(tag);
- return;
+ }
+};
+
+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)) {
+ collector.push(...(await handler(batch)));
+ }
+ 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>(resolve => {
+ const iterator = batches[Symbol.iterator]();
+ let completed = 0;
+ const tag = setInterval(async () => {
+ const next = iterator.next();
+ if (next.done) {
+ clearInterval(tag);
+ } else {
+ await handler(next.value);
+ if (++completed === batches.length) {
+ resolve();
}
- const batch = next.value;
- collector.push(...(await handler(batch)));
+ }
+ }, interval * 1000);
+ });
+};
+
+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[]>(resolve => {
+ const iterator = batches[Symbol.iterator]();
+ let completed = 0;
+ const tag = setInterval(async () => {
+ const next = iterator.next();
+ if (next.done) {
+ clearInterval(tag);
+ } else {
+ collector.push(...(await handler(next.value)));
if (++completed === batches.length) {
resolve(collector);
}
- }, interval);
- });
- }
- return collector;
+ }
+ }, interval * 1000);
+ });
}; \ No newline at end of file