Added debouncing

This commit is contained in:
dhruvik7
2021-02-14 21:16:24 -05:00
parent 16823529f9
commit f94b207a77

View File

@@ -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);
}
}