From e84c76acca00c69156fae15b15ebb31931017e58 Mon Sep 17 00:00:00 2001 From: Evan Bonsignori Date: Tue, 13 Feb 2024 13:02:26 -0800 Subject: [PATCH] only run auto run after sync in complete --- src/main.ts | 7 +++++-- src/utils/run-after-sync.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/utils/run-after-sync.ts diff --git a/src/main.ts b/src/main.ts index fa4d42f..ad1838a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,7 @@ import { } from "./settings/settings"; import Core from "./core"; import { getJournalLink, navigateToJournalLink } from "./note-links"; +import { runAfterSync } from "./utils/run-after-sync"; export default class AutoJournal extends Plugin { settings: AutoJournalSettings; @@ -19,8 +20,10 @@ export default class AutoJournal extends Plugin { // Automatically run on startup if enabled if (this.settings.automaticallyRun) { - this.app.workspace.onLayoutReady(() => { - this.run(); + runAfterSync.call(this, () => { + this.app.workspace.onLayoutReady(() => { + this.run(); + }); }); } diff --git a/src/utils/run-after-sync.ts b/src/utils/run-after-sync.ts new file mode 100644 index 0000000..ae139bc --- /dev/null +++ b/src/utils/run-after-sync.ts @@ -0,0 +1,17 @@ +let hasRun = false; + +export function runAfterSync(callBack: any) { + const sync = this.app?.internalPlugins?.plugins?.sync?.instance; + if (!hasRun || !sync || sync.syncStatus?.toLowerCase() === "fully synced") { + callBack(); + hasRun = true; + return; + } + sync.on("status-change", () => { + if (!hasRun && sync?.syncStatus?.toLowerCase() === "fully synced") { + callBack(); + hasRun = true; + return; + } + }); +}