diff --git a/main.ts b/main.ts index 6c8d2a4..6f046e4 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { TFile, Plugin, MarkdownView } from 'obsidian'; +import { TFile, Plugin, MarkdownView, debounce, Debouncer } from 'obsidian'; interface WordCount { initial: number; @@ -20,6 +20,7 @@ export default class DailyStats extends Plugin { statusBarEl: HTMLElement; currentWordCount: number; today: string; + debouncedUpdate: Debouncer<[contents: string, filepath: string]>; async onload() { await this.loadSettings(); @@ -32,6 +33,10 @@ export default class DailyStats extends Plugin { this.currentWordCount = 0; } + this.debouncedUpdate = debounce((contents: string, filepath: string) => { + this.updateWordCount(contents, filepath); + }, 400, false); + this.registerEvent( this.app.workspace.on("quit", this.onunload.bind(this)) ); @@ -58,7 +63,7 @@ export default class DailyStats extends Plugin { onQuickPreview(file: TFile, contents: string) { if (this.app.workspace.getActiveViewOfType(MarkdownView)) { - this.updateWordCount(contents, file.path); + this.debouncedUpdate(contents, file.path); } }