From 8cfbaa1bdac418f1ccc7715d5339a007d8ccf0bc Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 22 Mar 2025 17:21:59 +0000 Subject: [PATCH] Fix reset flow --- .../sync-client/src/services/connection-status.ts | 5 ++++- frontend/sync-client/src/sync-client.ts | 5 +++-- frontend/sync-client/src/sync-operations/syncer.ts | 13 +++++++++++++ .../src/sync-operations/unrestricted-syncer.ts | 8 ++++++++ 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/frontend/sync-client/src/services/connection-status.ts b/frontend/sync-client/src/services/connection-status.ts index 39e945b..572d889 100644 --- a/frontend/sync-client/src/services/connection-status.ts +++ b/frontend/sync-client/src/services/connection-status.ts @@ -39,8 +39,11 @@ export class ConnectionStatus { return input.url; } - public reset(): void { + public startReset(): void { this.rejectUntil(new SyncResetError()); + } + + public finishReset(): void { [this.until, this.resolveUntil, this.rejectUntil] = createPromise(); } diff --git a/frontend/sync-client/src/sync-client.ts b/frontend/sync-client/src/sync-client.ts index b07a169..fa3dbe3 100644 --- a/frontend/sync-client/src/sync-client.ts +++ b/frontend/sync-client/src/sync-client.ts @@ -162,8 +162,8 @@ export class SyncClient { } public async waitAndStop(): Promise { - await this.syncer.waitForSyncQueue(); await this.syncer.applyRemoteChangesLocally(); + await this.syncer.waitForSyncQueue(); this.stop(); } @@ -172,11 +172,12 @@ export class SyncClient { /// The SyncClient can be used again after calling this method. public async reset(): Promise { this.stop(); - this.connectionStatus.reset(); + this.connectionStatus.startReset(); await this.syncer.reset(); this.history.reset(); this.database.reset(); this._logger.reset(); + this.connectionStatus.finishReset(); void this.start(); } diff --git a/frontend/sync-client/src/sync-operations/syncer.ts b/frontend/sync-client/src/sync-operations/syncer.ts index 861dac0..bec932a 100644 --- a/frontend/sync-client/src/sync-operations/syncer.ts +++ b/frontend/sync-client/src/sync-operations/syncer.ts @@ -11,6 +11,7 @@ import type { FileOperations } from "../file-operations/file-operations"; import { findMatchingFile } from "../utils/find-matching-file"; import { UnrestrictedSyncer } from "./unrestricted-syncer"; import { createPromise } from "../utils/create-promise"; +import { SyncResetError } from "../services/sync-reset-error"; export class Syncer { private readonly remainingOperationsListeners: (( @@ -203,6 +204,12 @@ export class Syncer { await this.runningScheduleSyncForOfflineChanges; this.logger.info(`All local changes have been applied remotely`); } catch (e) { + if (e instanceof SyncResetError) { + this.logger.info( + "Failed to apply local changes remotely due to a reset" + ); + return; + } this.logger.error( `Not all local changes have been applied remotely: ${e}` ); @@ -226,6 +233,12 @@ export class Syncer { await this.runningApplyRemoteChangesLocally; this.logger.info("All remote changes have been applied locally"); } catch (e) { + if (e instanceof SyncResetError) { + this.logger.info( + "Failed to apply remote changes locally due to a reset" + ); + return; + } this.logger.error(`Failed to apply remote changes locally: ${e}`); throw e; } finally { diff --git a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts index dffd35b..e6426fa 100644 --- a/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts +++ b/frontend/sync-client/src/sync-operations/unrestricted-syncer.ts @@ -16,6 +16,7 @@ import type { FileOperations } from "../file-operations/file-operations"; import { DocumentLocks } from "../file-operations/document-locks"; import { createPromise } from "../utils/create-promise"; import { FileNotFoundError } from "../file-operations/file-not-found-error"; +import { SyncResetError } from "../services/sync-reset-error"; export class UnrestrictedSyncer { private readonly locks: DocumentLocks; @@ -402,6 +403,13 @@ export class UnrestrictedSyncer { this.logger.info( `Skip ${syncSource.toLocaleLowerCase()} file because it no longer exists when trying to ${syncType.toLocaleLowerCase()} it` ); + return; + } + if (e instanceof SyncResetError) { + this.logger.info( + `Interrupting sync operation because of a reset` + ); + return; } else { this.history.addHistoryEntry({ status: SyncStatus.ERROR,