Skip to content

Commit

Permalink
move crons listeners to settings and fix restarting them on change in…
Browse files Browse the repository at this point in the history
…tervals
  • Loading branch information
Heiss committed Aug 1, 2024
1 parent 86d9654 commit feacf50
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
11 changes: 0 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 4 additions & 0 deletions src/settings/VaultTaskCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
46 changes: 42 additions & 4 deletions src/settings/mainSetting.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -30,7 +30,8 @@ export interface VikunjaPluginSettings {
availableViews: ModelsProjectView[],
selectedView: number,
selectBucketForDoneTasks: number,
cache: Map<number, PluginTask>, // do not touch! Only via processing/processor.ts
cache: Map<number, PluginTask>, // do not touch! Only via settings/VaultTaskCache.ts
saveCacheToDiskFrequency: number,
}

export const DEFAULT_SETTINGS: VikunjaPluginSettings = {
Expand Down Expand Up @@ -59,15 +60,21 @@ export const DEFAULT_SETTINGS: VikunjaPluginSettings = {
selectedView: 0,
selectBucketForDoneTasks: 0,
cache: new Map<number, PluginTask>(),
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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit feacf50

Please sign in to comment.