Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

chore: nc concurrent utils

Fendy Heryantoca8f97a9fd1d
diff --git a/packages/nocodb/src/utils/NcConcurrent.ts b/packages/nocodb/src/utils/NcConcurrent.ts
new file mode 100644
index 0000000000..f50eea7423
--- /dev/null
+++ b/packages/nocodb/src/utils/NcConcurrent.ts
@@ -0,0 +1,39 @@
+import PQueue from 'p-queue';
+
+interface HandlerType<T> {
+ (): Promise<T>;
+}
+export const NcConcurrent = async <T>(
+ handlers: HandlerType<T>[],
+ { concurrency }: { concurrency?: number } = { concurrency: 3 },
+) => {
+ const result: T[] = [];
+ let error: Error;
+ const abortController = new AbortController();
+
+ const queue = new PQueue({ concurrency });
+ for (let i = 0; i < handlers.length; i++) {
+ const handler = handlers[i];
+ queue.add(async () => {
+ if (abortController.signal?.aborted) {
+ return;
+ }
+ try {
+ const handlerResult = await handler();
+ if (typeof handlerResult !== 'undefined') {
+ result[i] = handlerResult;
+ }
+ } catch (err) {
+ error = err;
+ abortController.abort();
+ queue.clear();
+ }
+ });
+ }
+ await queue.onIdle();
+ if (error) {
+ throw error;
+ }
+
+ return result;
+};
diff --git a/packages/nocodb/tests/unit/helpersTest/NcConcurrent.test.ts b/packages/nocodb/tests/unit/helpersTest/NcConcurrent.test.ts
new file mode 100644
index 0000000000..cc2a9eda6d
--- /dev/null
+++ b/packages/nocodb/tests/unit/helpersTest/NcConcurrent.test.ts
@@ -0,0 +1,45 @@
+import 'mocha';
+import { expect } from 'chai';
+import { NcConcurrent } from '~/utils/NcConcurrent';
+
+const task = (index: number, willThrow?: boolean) => async () => {
+ const delay = Math.floor(Math.random() * (50 - 10 + 1)) + 10;
+ await new Promise<void>((resolve) => {
+ setTimeout(() => {
+ resolve();
+ }, delay);
+ });
+ if (willThrow) {
+ throw new Error('Unexpected error at ' + index);
+ }
+ return [index, new Date().getTime()];
+};
+
+function NcConcurrentTests() {
+ it(`will handle 10 concurrent processes`, async () => {
+ const tasks = Array.from({ length: 10 }).map((_val, i) => task(i));
+ const result = await NcConcurrent(tasks);
+
+ // expect ordered result
+ expect(result.map((r) => r[0]).join('_')).to.equal(
+ Array.from({ length: 10 })
+ .map((_val, i) => i)
+ .join('_'),
+ );
+ });
+ it(`will handle 10 concurrent processes with error`, async () => {
+ const tasks = Array.from({ length: 10 }).map((_val, i) => task(i, i === 2));
+ let isCatched = false;
+ try {
+ await NcConcurrent(tasks);
+ } catch (ex: any) {
+ expect(ex.message).to.eq('Unexpected error at 2');
+ isCatched = true;
+ }
+ expect(isCatched).to.eq(true);
+ });
+}
+
+export function NcConcurrentTest() {
+ describe('NcConcurrentTest', NcConcurrentTests);
+}
diff --git a/packages/nocodb/tests/unit/helpersTest/index.test.ts b/packages/nocodb/tests/unit/helpersTest/index.test.ts
index 4da8a8136a..6f4819cb85 100644
--- a/packages/nocodb/tests/unit/helpersTest/index.test.ts
+++ b/packages/nocodb/tests/unit/helpersTest/index.test.ts
@@ -1,8 +1,10 @@
import { runOnSet } from '../utils/runOnSet';
+import { NcConcurrentTest } from './NcConcurrent.test';
import { stringHelperTest } from './stringHelpers.test';
function _helperTests() {
stringHelperTest();
+ NcConcurrentTest();
}
export const helperTests = runOnSet(1, function () {
describe('helpersTest', _helperTests);