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:
parent
f6fd30c385
commit
ba22ec7db9
4 changed files with 27 additions and 18 deletions
|
@ -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() {
|
||||
|
|
|
@ -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> {
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue