From b37893a55eaf1b10cab49feac7b968bba6a66213 Mon Sep 17 00:00:00 2001 From: Kageetai Date: Tue, 22 Oct 2024 15:43:45 +0200 Subject: [PATCH] refactor: make sure to follow OO24 guidelines for performance - only register event listener when view is ready - check for instance of view when accessing it --- manifest.json | 2 +- package.json | 2 +- src/main.ts | 19 +++++++----- src/view.tsx | 82 ++++++++++++++++++++++++++------------------------- versions.json | 3 +- 5 files changed, 58 insertions(+), 50 deletions(-) diff --git a/manifest.json b/manifest.json index eaa16a3..53085ff 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "journal-review", "name": "Journal Review", - "version": "2.4.2", + "version": "2.4.3", "minAppVersion": "0.15.0", "description": "Review your daily notes on their anniversaries, like \"what happened today last year\".", "author": "Kageetai", diff --git a/package.json b/package.json index 56f11c9..597cad1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "obsidian-plugin-journal-review", - "version": "2.4.2", + "version": "2.4.3", "description": "Review your daily notes on their anniversaries, like \"what happened today last year\"\n\n", "main": "main.js", "scripts": { diff --git a/src/main.ts b/src/main.ts index a174833..63c379e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -96,7 +96,9 @@ export default class JournalReviewPlugin extends Plugin { } // "Reveal" the leaf in case it is in a collapsed sidebar - workspace.revealLeaf(leaf!); + await workspace.revealLeaf(leaf!); + + this.renderView(); } onunload() { @@ -127,14 +129,17 @@ export default class JournalReviewPlugin extends Plugin { this.settings = Object.assign({}, DEFAULT_SETTINGS, parsedData); } - async saveSettings() { - await this.saveData(this.settings); + renderView() { + const leaf = this.app.workspace.getLeavesOfType(VIEW_TYPE).first(); - // rerender view - ( - this.app.workspace.getLeavesOfType(VIEW_TYPE)[0]?.view as OnThisDayView - )?.renderView(); + if (leaf && leaf.view instanceof OnThisDayView) { + leaf.view.renderView(); + } + } + async saveSettings() { + await this.saveData(this.settings); + this.renderView(); this.setupFocusListener(); } } diff --git a/src/view.tsx b/src/view.tsx index b17ec54..932ce9f 100644 --- a/src/view.tsx +++ b/src/view.tsx @@ -20,49 +20,51 @@ export default class OnThisDayView extends ItemView { this.settings = settings; - this.registerEvent( - this.app.vault.on("create", (file: TFile) => { - if (getDateFromFile(file, "day")) { - this.debouncedRenderView(); - } - }), - ); - this.registerEvent( - this.app.vault.on("delete", (file: TFile) => { - if (getDateFromFile(file, "day")) { - this.debouncedRenderView(); - } - }), - ); - this.registerEvent( - this.app.vault.on("rename", (file: TFile) => { - if (getDateFromFile(file, "day")) { - this.debouncedRenderView(); - } - }), - ); - - this.registerEvent( - this.app.workspace.on("file-open", (file) => { - const dateFromFile = file && getDateFromFile(file, "day"); + this.app.workspace.onLayoutReady(() => { + this.registerEvent( + this.app.vault.on("create", (file: TFile) => { + if (getDateFromFile(file, "day")) { + this.debouncedRenderView(); + } + }), + ); + this.registerEvent( + this.app.vault.on("delete", (file: TFile) => { + if (getDateFromFile(file, "day")) { + this.debouncedRenderView(); + } + }), + ); + this.registerEvent( + this.app.vault.on("rename", (file: TFile) => { + if (getDateFromFile(file, "day")) { + this.debouncedRenderView(); + } + }), + ); - if (this.settings.renderOnFileSwitch && dateFromFile) { - this.debouncedRenderView(dateFromFile); - } - }), - ); + this.registerEvent( + this.app.workspace.on("file-open", (file) => { + const dateFromFile = file && getDateFromFile(file, "day"); - // rerender at midnight - this.registerInterval( - window.setInterval( - () => { - if (new Date().getHours() === 0) { - this.renderView(); + if (this.settings.renderOnFileSwitch && dateFromFile) { + this.debouncedRenderView(dateFromFile); } - }, - 60 * 60 * 1000, - ), - ); + }), + ); + + // rerender at midnight + this.registerInterval( + window.setInterval( + () => { + if (new Date().getHours() === 0) { + this.renderView(); + } + }, + 60 * 60 * 1000, + ), + ); + }); } getViewType() { diff --git a/versions.json b/versions.json index ed44f3f..c04f0d5 100644 --- a/versions.json +++ b/versions.json @@ -20,5 +20,6 @@ "2.3.2": "0.15.0", "2.4.0": "0.15.0", "2.4.1": "0.15.0", - "2.4.2": "0.15.0" + "2.4.2": "0.15.0", + "2.4.3": "0.15.0" }