From d7ff0f0bedea8e3eff2a3bc2b9776ee6fb5fe652 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sun, 15 Dec 2024 11:53:20 +0000 Subject: [PATCH] Register polling fetcher --- plugin/src/plugin.ts | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/plugin/src/plugin.ts b/plugin/src/plugin.ts index 2a01192b..b0d39986 100644 --- a/plugin/src/plugin.ts +++ b/plugin/src/plugin.ts @@ -19,8 +19,11 @@ import { Logger } from "./logger.js"; import { SyncEventHandler } from "./events/sync-event-handler.js"; import { SyncServer } from "./services/sync_service.js"; import { Database } from "./database/database.js"; +import { applyRemoteChangesLocally } from "./apply-remote-changes-locally.js"; export default class SyncPlugin extends Plugin { + private remoteListenerIntervalId: number | null = null; + async onload() { Logger.getInstance().info('Starting plugin "Sample Plugin"'); @@ -67,9 +70,19 @@ export default class SyncPlugin extends Plugin { ), ].forEach((event) => this.registerEvent(event)); - this.registerInterval( - window.setInterval(() => console.log("setInterval"), 5 * 60 * 1000) + this.registerRemoteEventListener( + database, + syncServer, + database.getSettings().fetchChangesUpdateInterval ); + database.addOnSettingsChangeHandlers((settings) => { + this.registerRemoteEventListener( + database, + syncServer, + settings.fetchChangesUpdateInterval + ); + }); + this.registerView(SyncView.TYPE, (leaf) => new SyncView(leaf)); const ribbonIconEl = this.addRibbonIcon( @@ -101,4 +114,26 @@ export default class SyncPlugin extends Plugin { // "Reveal" the leaf in case it is in a collapsed sidebar workspace.revealLeaf(leaf!); } + + unload(): void { + if (this.remoteListenerIntervalId) { + window.clearInterval(this.remoteListenerIntervalId); + } + } + + private registerRemoteEventListener( + database: Database, + syncServer: SyncServer, + intervalMs: number + ) { + if (this.remoteListenerIntervalId) { + window.clearInterval(this.remoteListenerIntervalId); + } + + this.remoteListenerIntervalId = window.setInterval( + () => + applyRemoteChangesLocally(database, syncServer, this.app.vault), + intervalMs + ); + } }