From 7d28b46ca7631be860f5c3b4c25da7c033f9417a Mon Sep 17 00:00:00 2001 From: Henri Jamet <42291955+hjamet@users.noreply.github.com> Date: Sun, 29 Dec 2024 13:23:08 +0100 Subject: [PATCH] Implement sync interval setting and enhance synchronization timer in Khoj plugin - Added a new setting for users to configure the sync interval in minutes, allowing for more flexible automatic synchronization. - Introduced methods to start and restart the synchronization timer based on the configured interval. - Updated the synchronization logic to use the user-defined interval instead of a fixed 60 minutes. - Improved code readability and organization by refactoring the sync timer logic. --- src/interface/obsidian/src/main.ts | 32 +++++++++++++++++++++----- src/interface/obsidian/src/settings.ts | 24 +++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index f5f80cfc..bb4eecbd 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -43,8 +43,8 @@ export default class Khoj extends Plugin { this.app.vault, this.settings, this.settings.lastSync, - false, // regenerate = false pour ne synchroniser que les nouvelles modifications - true // userTriggered = true pour afficher une notification + false, + true ); } }); @@ -59,12 +59,32 @@ export default class Khoj extends Plugin { // Add a settings tab so the user can configure khoj this.addSettingTab(new KhojSettingTab(this.app, this)); - // Add scheduled job to update index every 60 minutes + // Démarrer le timer de synchronisation + this.startSyncTimer(); + } + + // Méthode pour démarrer le timer de synchronisation + private startSyncTimer() { + // Nettoyer l'ancien timer s'il existe + if (this.indexingTimer) { + clearInterval(this.indexingTimer); + } + + // Démarrer un nouveau timer avec l'intervalle configuré this.indexingTimer = setInterval(async () => { if (this.settings.autoConfigure) { - this.settings.lastSync = await updateContentIndex(this.app.vault, this.settings, this.settings.lastSync); + this.settings.lastSync = await updateContentIndex( + this.app.vault, + this.settings, + this.settings.lastSync + ); } - }, 60 * 60 * 1000); + }, this.settings.syncInterval * 60 * 1000); // Convertir les minutes en millisecondes + } + + // Méthode publique pour redémarrer le timer (appelée depuis les paramètres) + public restartSyncTimer() { + this.startSyncTimer(); } async loadSettings() { @@ -77,7 +97,7 @@ export default class Khoj extends Plugin { } async saveSettings() { - this.saveData(this.settings); + await this.saveData(this.settings); } async onunload() { diff --git a/src/interface/obsidian/src/settings.ts b/src/interface/obsidian/src/settings.ts index 62467b88..54dbb812 100644 --- a/src/interface/obsidian/src/settings.ts +++ b/src/interface/obsidian/src/settings.ts @@ -26,6 +26,7 @@ export interface KhojSetting { syncFileType: SyncFileTypes; userInfo: UserInfo | null; syncFolders: string[]; + syncInterval: number; } export const DEFAULT_SETTINGS: KhojSetting = { @@ -42,6 +43,7 @@ export const DEFAULT_SETTINGS: KhojSetting = { }, userInfo: null, syncFolders: [], + syncInterval: 60, } export class KhojSettingTab extends PluginSettingTab { @@ -158,6 +160,28 @@ export class KhojSettingTab extends PluginSettingTab { await this.plugin.saveSettings(); })); + // Add setting for sync interval + const syncIntervalValues = [1, 5, 10, 20, 30, 45, 60, 120, 1440]; + new Setting(containerEl) + .setName('Sync Interval') + .setDesc('Minutes between automatic synchronizations') + .addDropdown(dropdown => dropdown + .addOptions(Object.fromEntries( + syncIntervalValues.map(value => [ + value.toString(), + value === 1 ? '1 minute' : + value === 1440 ? '24 hours' : + `${value} minutes` + ]) + )) + .setValue(this.plugin.settings.syncInterval.toString()) + .onChange(async (value) => { + this.plugin.settings.syncInterval = parseInt(value); + await this.plugin.saveSettings(); + // Redémarrer le timer avec le nouvel intervalle + this.plugin.restartSyncTimer(); + })); + // Add setting to manage sync folders const syncFoldersContainer = containerEl.createDiv('sync-folders-container'); const foldersSetting = new Setting(syncFoldersContainer)