From b5a9a2b4a26ca2cd7c8e02c4fbf6978dd69e06cf Mon Sep 17 00:00:00 2001 From: Liran Cohen Date: Wed, 4 Sep 2024 11:04:28 -0400 Subject: [PATCH] sync start simplification --- packages/agent/src/sync-engine-level.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/agent/src/sync-engine-level.ts b/packages/agent/src/sync-engine-level.ts index de38864ef..d77166536 100644 --- a/packages/agent/src/sync-engine-level.ts +++ b/packages/agent/src/sync-engine-level.ts @@ -289,12 +289,20 @@ export class SyncEngineLevel implements SyncEngine { } } - public startSync({ interval }: { + public async startSync({ interval }: { interval: string }): Promise { // Convert the interval string to milliseconds. const intervalMilliseconds = ms(interval); + if (this._syncIntervalId) { + this.stopSync(); + } + + if (!this._syncLock) { + await this.sync(); + } + return new Promise((resolve, reject) => { const intervalSync = async () => { if (this._syncLock) { @@ -317,10 +325,6 @@ export class SyncEngineLevel implements SyncEngine { } }; - if (this._syncIntervalId) { - clearInterval(this._syncIntervalId); - } - this._syncIntervalId = setInterval(intervalSync, intervalMilliseconds); }); }