diff --git a/main.ts b/main.ts index 7f21eb1..3be3b21 100644 --- a/main.ts +++ b/main.ts @@ -72,17 +72,6 @@ export default class VikunjaPlugin extends Plugin { this.registerDomEvent(document, 'keyup', this.handleUpDownEvent.bind(this)); this.registerDomEvent(document, 'click', this.handleClickEvent.bind(this)); this.registerEvent(this.app.workspace.on('editor-change', this.handleEditorChange.bind(this))); - - // When registering intervals, this function will automatically clear the interval when the plugin is disabled. - this.registerInterval(window - .setInterval(async () => { - // this runs anyway, also when cron not enabled, to be dynamically enabled by settings without disable/enable plugin - if (this.settings.enableCron) { - await this.processor.exec() - } - }, - this.settings.cronInterval * 1000) - ); } private setupAPIs() { diff --git a/src/settings/VaultTaskCache.ts b/src/settings/VaultTaskCache.ts index dfd3622..54b17d8 100644 --- a/src/settings/VaultTaskCache.ts +++ b/src/settings/VaultTaskCache.ts @@ -16,6 +16,10 @@ export default class VaultTaskCache { this.plugin = plugin; } + async saveCacheToDisk() { + await this.plugin.saveSettings(); + } + update(local: PluginTask) { if (local.task.id === undefined) { throw new Error("VaultTaskCache: Task id is not defined"); diff --git a/src/settings/mainSetting.ts b/src/settings/mainSetting.ts index 3a65b13..9ba38cc 100644 --- a/src/settings/mainSetting.ts +++ b/src/settings/mainSetting.ts @@ -1,7 +1,7 @@ import {App, Notice, PluginSettingTab, Setting} from "obsidian"; import VikunjaPlugin from "../../main"; import {backendToFindTasks, chooseOutputFile, supportedTasksPluginsFormat} from "../enums"; -import {ModelsProject, ModelsProjectView, ModelsTask} from "../../vikunja_sdk"; +import {ModelsProject, ModelsProjectView} from "../../vikunja_sdk"; import {appHasDailyNotesPluginLoaded} from "obsidian-daily-notes-interface"; import {PluginTask} from "../vaultSearcher/vaultSearcher"; @@ -30,7 +30,8 @@ export interface VikunjaPluginSettings { availableViews: ModelsProjectView[], selectedView: number, selectBucketForDoneTasks: number, - cache: Map, // do not touch! Only via processing/processor.ts + cache: Map, // do not touch! Only via settings/VaultTaskCache.ts + saveCacheToDiskFrequency: number, } export const DEFAULT_SETTINGS: VikunjaPluginSettings = { @@ -59,15 +60,21 @@ export const DEFAULT_SETTINGS: VikunjaPluginSettings = { selectedView: 0, selectBucketForDoneTasks: 0, cache: new Map(), + saveCacheToDiskFrequency: 10, } export class MainSetting extends PluginSettingTab { plugin: VikunjaPlugin; projects: ModelsProject[] = []; + private cacheListener: number; + private cronListener: number; constructor(app: App, plugin: VikunjaPlugin) { super(app, plugin); this.plugin = plugin; + + this.startCacheListener(); + this.startCronListener(); } display(): void { @@ -187,10 +194,22 @@ export class MainSetting extends PluginSettingTab { this.plugin.settings.cronInterval = parseInt(value); await this.plugin.saveSettings(); } - )) - ; + )); } + new Setting(containerEl) + .setName("Save cache to disk frequency") + .setDesc("Set the interval in minutes to save the cache to disk. Lower values will result in more frequent saves, but may cause performance issues. Limits are 1 to 60. This will be enforced by the plugin.") + .addText(text => text + .setValue(this.plugin.settings.saveCacheToDiskFrequency.toString()) + .onChange(async (value: string) => { + this.plugin.settings.saveCacheToDiskFrequency = Math.max(Math.min(parseInt(value), 60), 1); + await this.plugin.saveSettings(); + this.startCacheListener(); + } + ) + ) + new Setting(containerEl).setHeading().setName('Vikunja Settings').setDesc('Settings to connect to Vikunja.'); const hostDesc = document.createDocumentFragment(); @@ -551,6 +570,25 @@ export class MainSetting extends PluginSettingTab { this.display(); } + private startCacheListener() { + window.clearInterval(this.cacheListener); + this.cacheListener = window.setInterval(this.plugin.cache.saveCacheToDisk.bind(this), this.plugin.settings.saveCacheToDiskFrequency * 60 * 1000); + this.plugin.registerInterval(this.cacheListener); + } + + private startCronListener() { + window.clearInterval(this.cronListener); + this.cronListener = window + .setInterval(async () => { + // this runs anyway, also when cron not enabled, to be dynamically enabled by settings without disable/enable plugin + if (this.plugin.settings.enableCron) { + await this.plugin.processor.exec() + } + }, + this.plugin.settings.cronInterval * 1000) + this.plugin.registerInterval(this.cronListener); + } + private resetApis() { // TODO: Implement an event to reload API configurations this.plugin.tasksApi.init();