import { Low } from 'lowdb'; import { JSONFile } from 'lowdb/node'; import { appStatePath, isTest } from '../../setup/index.js'; interface Config { lastLoadedProject: string; } /** * Manages Ontime's runtime memory between boots */ class AppState { private config: Low; private pathToFile: string; private didInit = false; constructor(appStatePath: string) { this.pathToFile = appStatePath; const adapter = new JSONFile(this.pathToFile); this.config = new Low(adapter, null); } private async init() { await this.config.read(); await this.config.write(); this.didInit = true; } private async get(): Promise { if (!this.didInit) { await this.init(); } await this.config.read(); return this.config.data; } async isLastLoadedProject(projectName: string): Promise { const lastLoaded = await this.getLastLoadedProject(); return lastLoaded === projectName; } async getLastLoadedProject(): Promise { const data = await this.get(); return data.lastLoadedProject; } async updateDatabaseConfig(filename: string): Promise { if (isTest) return; if (!this.didInit) { await this.init(); } this.config.data.lastLoadedProject = filename; await this.config.write(); } } export const appStateProvider = new AppState(appStatePath);