Skip to content

Commit

Permalink
refactor: make sure to follow OO24 guidelines for performance
Browse files Browse the repository at this point in the history
- only register event listener when view is ready
- check for instance of view when accessing it
  • Loading branch information
Kageetai committed Oct 22, 2024
1 parent 2289076 commit b37893a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 50 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 12 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
}
82 changes: 42 additions & 40 deletions src/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit b37893a

Please sign in to comment.