From ba22ec7db97a8354a42f3c7afb77a678fb5d95a4 Mon Sep 17 00:00:00 2001 From: Leo Yao <45076643+leoccyao@users.noreply.github.com> Date: Sat, 25 Dec 2021 22:26:59 -0500 Subject: [PATCH] Fix stats file creation when stats collection off I noticed that despite having collectStats set to false, upon vault or plugin load, the .vault-stats file would keep coming back. This is due to: In the plugin onload(), a BarManager is created In the BarManager constructor, a DataManager is created In the DataManager constructor, the .vault-stats file is created and then written to, regardless of the user's collectStats setting. We add an additional check in the DataManager constructor for the collectStats setting, propagating settings through constructors as necessary. --- src/data/collector.ts | 5 ++++- src/data/manager.ts | 33 +++++++++++++++++++-------------- src/main.ts | 3 ++- src/status/manager.ts | 4 ++-- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/data/collector.ts b/src/data/collector.ts index 195d45f..43e1c02 100644 --- a/src/data/collector.ts +++ b/src/data/collector.ts @@ -1,14 +1,17 @@ import moment from "moment"; import type { MetadataCache, TFile, Vault } from "obsidian"; +import type { BetterWordCountSettings } from "src/settings/settings"; import { getCharacterCount, getSentenceCount, getWordCount } from "./stats"; export class DataCollector { private vault: Vault; private metadataCache: MetadataCache; + private settings: BetterWordCountSettings; - constructor(vault: Vault, metadataCache: MetadataCache) { + constructor(vault: Vault, metadataCache: MetadataCache, settings: BetterWordCountSettings) { this.vault = vault; this.metadataCache = metadataCache; + this.settings = settings; } getTotalFileCount() { diff --git a/src/data/manager.ts b/src/data/manager.ts index 8162202..5eea5ed 100644 --- a/src/data/manager.ts +++ b/src/data/manager.ts @@ -1,6 +1,7 @@ import moment from "moment"; import { debounce, Debouncer, MetadataCache, TFile, Vault } from "obsidian"; import { STATS_FILE } from "src/constants"; +import type { BetterWordCountSettings } from "src/settings/settings"; import { DataCollector } from "./collector"; import { getCharacterCount, getSentenceCount, getWordCount } from "./stats"; @@ -41,34 +42,38 @@ export interface TotalCounts { export class DataManager { private vault: Vault; private metadataCache: MetadataCache; + private settings: BetterWordCountSettings; private history: History; private today: string; private collector: DataCollector; private todayCounts: TodayCounts; public debounceChange: Debouncer<[file: TFile, data: string]>; - constructor(vault: Vault, metadataCache: MetadataCache) { + constructor(vault: Vault, metadataCache: MetadataCache, settings: BetterWordCountSettings) { this.vault = vault; this.metadataCache = metadataCache; - this.collector = new DataCollector(vault, metadataCache); + this.settings = settings; + this.collector = new DataCollector(vault, metadataCache, settings); this.debounceChange = debounce( (file: TFile, data: string) => this.change(file, data), 1000, false ); - this.vault.adapter.exists(".vault-stats").then(async (exists) => { - if (!exists) { - this.vault.adapter.write(".vault-stats", "{}"); - } - - this.history = Object.assign( - JSON.parse(await this.vault.adapter.read(".vault-stats")) - ); - - this.updateToday(); - this.update(); - }); + if(this.settings.collectStats){ + this.vault.adapter.exists(".vault-stats").then(async (exists) => { + if (!exists) { + this.vault.adapter.write(".vault-stats", "{}"); + } + + this.history = Object.assign( + JSON.parse(await this.vault.adapter.read(".vault-stats")) + ); + + this.updateToday(); + this.update(); + }); + } } async update(): Promise { diff --git a/src/main.ts b/src/main.ts index 0a9347b..7a40f63 100644 --- a/src/main.ts +++ b/src/main.ts @@ -38,7 +38,8 @@ export default class BetterWordCount extends Plugin { if (this.settings.collectStats) { this.dataManager = new DataManager( this.app.vault, - this.app.metadataCache + this.app.metadataCache, + this.settings ); } diff --git a/src/status/manager.ts b/src/status/manager.ts index 655432e..e438580 100644 --- a/src/status/manager.ts +++ b/src/status/manager.ts @@ -30,8 +30,8 @@ export class BarManager { this.statusBar = statusBar; this.settings = settings; this.vault = vault; - this.dataCollector = new DataCollector(vault, metadataCache); - this.dataManager = new DataManager(vault, metadataCache); + this.dataCollector = new DataCollector(vault, metadataCache, settings); + this.dataManager = new DataManager(vault, metadataCache, settings); this.deboucer = debounce( (text: string) => this.updateStatusBar(text), 20,