From 98107ba4eac4698ebf4ab918aa208460e03f06eb Mon Sep 17 00:00:00 2001 From: Vijayasantham <70528700+vijayasantham12@users.noreply.github.com> Date: Sat, 30 Mar 2024 21:37:07 +0530 Subject: [PATCH] Improvement of sync_on_save (#502) * Improve sync_on_save * fix typo --------- Co-authored-by: fyears <1142836+fyears@users.noreply.github.com> --- src/main.ts | 84 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 35 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4edd3ce..4e8a48b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import { Platform, requestUrl, requireApiVersion, + Events, } from "obsidian"; import cloneDeep from "lodash/cloneDeep"; import { createElement, RotateCcw, RefreshCcw, FileText } from "lucide"; @@ -149,6 +150,7 @@ export default class RemotelySavePlugin extends Plugin { i18n!: I18n; vaultRandomID!: string; debugServerTemp?: string; + syncEvent?: Events; async syncRun(triggerSource: SyncTriggerSourceType = "manual") { const t = (x: TransItemType, vars?: any) => { @@ -416,6 +418,7 @@ export default class RemotelySavePlugin extends Plugin { this.updateLastSuccessSyncMsg(lastSuccessSyncMillis); } + this.syncEvent?.trigger("SYNC_DONE"); console.info( `${ this.manifest.id @@ -465,6 +468,8 @@ export default class RemotelySavePlugin extends Plugin { this.currSyncMsg = ""; + this.syncEvent = new Events(); + await this.loadSettings(); // MUST after loadSettings and before prepareDB @@ -1126,44 +1131,53 @@ export default class RemotelySavePlugin extends Plugin { }, scheduleTimeFromNow); }; - this.app.workspace.onLayoutReady(() => { - const intervalID = window.setInterval(() => { - const currentFile = this.app.workspace.getActiveFile(); + const checkCurrFileModified = async (caller: "SYNC" | "FILE_CHANGES") => { + const currentFile = this.app.workspace.getActiveFile(); - if (currentFile) { - // get the last modified time of the current file - // if it has been modified within the last syncOnSaveAfterMilliseconds - // then schedule a run for syncOnSaveAfterMilliseconds after it was modified - const lastModified = currentFile.stat.mtime; - const currentTime = Date.now(); - if ( - currentTime - lastModified < - this.settings!.syncOnSaveAfterMilliseconds! - ) { - if ( - !needToRunAgain && - !runScheduled && - this.syncStatus === "idle" - ) { - const scheduleTimeFromNow = - this.settings!.syncOnSaveAfterMilliseconds! - - (currentTime - lastModified); - scheduleSyncOnSave(scheduleTimeFromNow); - } else if ( - needToRunAgain && - !runScheduled && - this.syncStatus === "idle" - ) { - scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!); - needToRunAgain = false; - } else { - needToRunAgain = true; - } + if (currentFile) { + // get the last modified time of the current file + // if it has modified after lastSuccessSync + // then schedule a run for syncOnSaveAfterMilliseconds after it was modified + const lastModified = currentFile.stat.mtime; + const lastSuccessSyncMillis = await getLastSuccessSyncByVault( + this.db, + this.vaultRandomID + ); + if ( + this.syncStatus === "idle" && + lastModified > lastSuccessSyncMillis && + !runScheduled + ) { + scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!); + } else if ( + this.syncStatus === "idle" && + needToRunAgain && + !runScheduled + ) { + scheduleSyncOnSave(this.settings!.syncOnSaveAfterMilliseconds!); + needToRunAgain = false; + } else { + if (caller === "FILE_CHANGES") { + needToRunAgain = true; } } - }, this.settings.syncOnSaveAfterMilliseconds); - this.syncOnSaveIntervalID = intervalID; - this.registerInterval(intervalID); + } + }; + + this.app.workspace.onLayoutReady(() => { + // listen to sync done + this.registerEvent( + this.syncEvent?.on("SYNC_DONE", () => { + checkCurrFileModified("SYNC"); + })! + ); + + // listen to current file save changes + this.registerEvent( + this.app.vault.on("modify", () => { + checkCurrFileModified("FILE_CHANGES"); + }) + ); }); } }