Skip to content

Commit

Permalink
[Refactor] Desktop config store back to class
Browse files Browse the repository at this point in the history
  • Loading branch information
webfiltered committed Dec 2, 2024
1 parent d1a1d51 commit ce2d921
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/main-process/comfyDesktopApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DownloadManager } from '../models/DownloadManager';
import { VirtualEnvironment } from '../virtualEnvironment';
import { InstallWizard } from '../install/installWizard';
import { Terminal } from '../terminal';
import { useDesktopStore } from '../store/store';
import { DesktopConfig } from '../store/desktopConfig';
import { InstallationValidator } from '../install/installationValidator';
import { restoreCustomNodes } from '../services/backup';

Expand Down Expand Up @@ -146,7 +146,7 @@ export class ComfyDesktopApp {
return new Promise<string>((resolve) => {
ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, async (event, installOptions: InstallOptions) => {
const installWizard = new InstallWizard(installOptions);
const { store } = useDesktopStore();
const { store } = DesktopConfig;
store.set('basePath', installWizard.basePath);

await installWizard.install();
Expand Down Expand Up @@ -187,7 +187,7 @@ export class ComfyDesktopApp {
this.appWindow.send(IPC_CHANNELS.LOG_MESSAGE, data);
},
});
const { store } = useDesktopStore();
const { store } = DesktopConfig;
if (!store.get('Comfy-Desktop.RestoredCustomNodes', false)) {
try {
await restoreCustomNodes(virtualEnvironment, this.appWindow);
Expand All @@ -205,7 +205,7 @@ export class ComfyDesktopApp {
}

static async create(appWindow: AppWindow): Promise<ComfyDesktopApp> {
const { store } = useDesktopStore();
const { store } = DesktopConfig;
// Migrate settings from old version if required
const installState = store.get('installState') ?? (await ComfyDesktopApp.migrateInstallState());

Expand All @@ -228,7 +228,7 @@ export class ComfyDesktopApp {
const basePath = await ComfyDesktopApp.loadBasePath();

// Migrate config
const { store } = useDesktopStore();
const { store } = DesktopConfig;
const upgraded = 'upgraded';
store.set('installState', upgraded);
store.set('basePath', basePath);
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AppInfoHandlers } from './handlers/appInfoHandlers';
import { ComfyDesktopApp } from './main-process/comfyDesktopApp';
import { LevelOption } from 'electron-log';
import SentryLogging from './services/sentry';
import { useDesktopStore } from './store/store';
import { DesktopConfig } from './store/desktopConfig';

dotenv.config();
log.initialize();
Expand Down Expand Up @@ -41,7 +41,7 @@ if (!gotTheLock) {
app.on('ready', async () => {
log.debug('App ready');

const store = await useDesktopStore().loadStore();
const store = await DesktopConfig.load();
if (store) {
startApp();
} else {
Expand Down
27 changes: 13 additions & 14 deletions src/store/store.ts → src/store/desktopConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ import path from 'node:path';
import fs from 'fs/promises';
import type { DesktopSettings } from '.';

let currentStore: ElectronStore<DesktopSettings>;

/** Generic wrapper class to load electron stores and handle errors. */
export function useDesktopStore() {
const store = currentStore;
/** Handles loading of electron-store config, pre-window errors, and provides a non-null interface for the store. */
export class DesktopConfig {
static #store: ElectronStore<DesktopSettings> | undefined;
static get store(): ElectronStore<DesktopSettings> {
const store = this.#store;
if (!store) throw new Error('Cannot access store before initialization.');
return store;
}

async function loadStore(
static async load(
options?: ConstructorParameters<typeof ElectronStore<DesktopSettings>>[0]
): Promise<ElectronStore<DesktopSettings> | undefined> {
try {
currentStore = new ElectronStore<DesktopSettings>(options);
return currentStore;
DesktopConfig.#store = new ElectronStore<DesktopSettings>(options);

return DesktopConfig.#store;
} catch (error) {
const configFilePath = path.join(getUserDataOrQuit(), `${options?.name ?? 'config'}.json`);

Expand All @@ -39,7 +43,7 @@ export function useDesktopStore() {
await tryDeleteConfigFile(configFilePath);

// Causing a stack overflow from this recursion would take immense patience.
return loadStore(options);
return DesktopConfig.load(options);
}
}

Expand All @@ -52,11 +56,6 @@ export function useDesktopStore() {
}
}
}

return {
store,
loadStore,
};
}

function showResetPrompt(configFilePath: string): Promise<Electron.MessageBoxReturnValue> {
Expand Down

0 comments on commit ce2d921

Please sign in to comment.