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.
This commit is contained in:
Leo Yao 2021-12-25 22:26:59 -05:00
parent f6fd30c385
commit ba22ec7db9
4 changed files with 27 additions and 18 deletions

View file

@ -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() {

View file

@ -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<void> {

View file

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

View file

@ -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,