From 17fa584ea14da711b42a4de687344aa6e5ecad94 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 23 Nov 2025 15:09:35 +0000 Subject: [PATCH] use allSettled --- .../sync-client/src/persistence/database.ts | 2 +- .../sync-client/src/sync-operations/syncer.ts | 23 +++++-------------- .../src/utils/data-structures/locks.ts | 4 +++- frontend/test-client/src/agent/mock-agent.ts | 2 +- frontend/test-client/src/cli.ts | 6 +++-- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/frontend/sync-client/src/persistence/database.ts b/frontend/sync-client/src/persistence/database.ts index 827cf164..62962dba 100644 --- a/frontend/sync-client/src/persistence/database.ts +++ b/frontend/sync-client/src/persistence/database.ts @@ -183,7 +183,7 @@ export class Database { const currentPromises = entry.updates; entry.updates = [...currentPromises, promise]; - await Promise.all(currentPromises); + await Promise.allSettled(currentPromises); return entry; } diff --git a/frontend/sync-client/src/sync-operations/syncer.ts b/frontend/sync-client/src/sync-operations/syncer.ts index ddfde46c..c8d30c31 100644 --- a/frontend/sync-client/src/sync-operations/syncer.ts +++ b/frontend/sync-client/src/sync-operations/syncer.ts @@ -26,6 +26,8 @@ export class Syncer { private readonly remainingOperationsListeners: (( remainingOperations: number ) => unknown)[] = []; + + // FIFO to limit the number of concurrent sync operations private readonly syncQueue: PQueue; private _isFirstSyncComplete = false; @@ -83,15 +85,6 @@ export class Syncer { this.remainingOperationsListeners.push(listener); } - public removeRemainingOperationsListener( - listener: (remainingOperations: number) => unknown - ): void { - const index = this.remainingOperationsListeners.indexOf(listener); - if (index !== -1) { - this.remainingOperationsListeners.splice(index, 1); - } - } - public async syncLocallyCreatedFile( relativePath: RelativePath ): Promise { @@ -280,10 +273,6 @@ export class Syncer { return this.syncQueue.onEmpty(); } - public async reset(): Promise { - await this.waitUntilFinished(); - } - public async syncRemotelyUpdatedFile( message: WebSocketVaultUpdate ): Promise { @@ -416,7 +405,7 @@ export class Syncer { } } - const updates = Promise.all( + const updates = Promise.allSettled( allLocalFiles.map(async (relativePath) => { if ( this.database.getLatestDocumentByRelativePath(relativePath) @@ -474,7 +463,7 @@ export class Syncer { }) ); - const deletes = Promise.all( + const deletes = Promise.allSettled( locallyPossiblyDeletedFiles.map(async ({ relativePath }) => { this.logger.debug( `Document ${relativePath} has been deleted locally, scheduling sync to delete it` @@ -485,7 +474,7 @@ export class Syncer { }) ); - await Promise.all([updates, deletes]); + await Promise.allSettled([updates, deletes]); } /** @@ -498,7 +487,7 @@ export class Syncer { return; } - const [allLocalFiles, remote] = await Promise.all([ + const [allLocalFiles, remote] = await Promise.allSettled([ this.operations.listFilesRecursively(), this.syncQueue.add(async () => this.syncService.getAll()) ]); diff --git a/frontend/sync-client/src/utils/data-structures/locks.ts b/frontend/sync-client/src/utils/data-structures/locks.ts index e835a4a3..4e510943 100644 --- a/frontend/sync-client/src/utils/data-structures/locks.ts +++ b/frontend/sync-client/src/utils/data-structures/locks.ts @@ -54,7 +54,9 @@ export class Locks { const uniqueKeys = Array.from(new Set(keys)); uniqueKeys.sort((a, b) => String(a).localeCompare(String(b))); // Ensure consistent order to prevent deadlocks - await Promise.all(uniqueKeys.map(async (key) => this.waitForLock(key))); + await Promise.allSettled( + uniqueKeys.map(async (key) => this.waitForLock(key)) + ); try { return await fn(); diff --git a/frontend/test-client/src/agent/mock-agent.ts b/frontend/test-client/src/agent/mock-agent.ts index a6ced45d..980da34b 100644 --- a/frontend/test-client/src/agent/mock-agent.ts +++ b/frontend/test-client/src/agent/mock-agent.ts @@ -127,7 +127,7 @@ export class MockAgent extends MockClient { public async finish(): Promise { await this.client.setSetting("isSyncEnabled", true); - await Promise.all(this.pendingActions); + await Promise.allSettled(this.pendingActions); await this.client.waitAndStop(); } diff --git a/frontend/test-client/src/cli.ts b/frontend/test-client/src/cli.ts index 4a3aab4f..578dab0a 100644 --- a/frontend/test-client/src/cli.ts +++ b/frontend/test-client/src/cli.ts @@ -53,11 +53,13 @@ async function runTest({ } try { - await Promise.all(clients.map(async (client) => client.init())); + await Promise.allSettled(clients.map(async (client) => client.init())); for (let i = 0; i < iterations; i++) { console.info(`Iteration ${i + 1}/${iterations}`); - await Promise.all(clients.map(async (client) => client.act())); + await Promise.allSettled( + clients.map(async (client) => client.act()) + ); await sleep(100); }