fix: prevent calling service before init

This commit is contained in:
Carlos Valente
2024-06-23 11:46:11 +02:00
committed by Carlos Valente
parent 6c20cb5e99
commit cbf3d566b9
@@ -14,21 +14,24 @@ interface Config {
class AppStateService {
private config: Low<Config>;
private pathToFile: string;
private didInit = false;
constructor(appStatePath: string) {
this.pathToFile = appStatePath;
const adapter = new JSONFile<Config>(this.pathToFile);
this.config = new Low<Config>(adapter, null);
this.init();
}
private async init() {
await this.config.read();
await this.config.write();
this.didInit = true;
}
async get(): Promise<Config> {
if (!this.didInit) {
await this.init();
}
await this.config.read();
return this.config.data;
}
@@ -36,6 +39,10 @@ class AppStateService {
async updateDatabaseConfig(filename: string): Promise<void> {
if (isTest) return;
if (!this.didInit) {
await this.init();
}
this.config.data.lastLoadedProject = filename;
await this.config.write();
}