From e56cdad02c00adf3b779d9de62d460e78be204a6 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 26 Dec 2021 19:17:23 +0100 Subject: refactor: Clarify main process architecture * stores: reactive data structures to hold application state * controllers: subscribe to store changes and call store actions in response to external events from services * services: integrate with the nodejs and electron environment (should be mocked for unit testing) --- packages/main/src/stores/Config.ts | 87 +------------------------------------- 1 file changed, 2 insertions(+), 85 deletions(-) (limited to 'packages/main/src/stores/Config.ts') diff --git a/packages/main/src/stores/Config.ts b/packages/main/src/stores/Config.ts index eb53635..7d1168f 100644 --- a/packages/main/src/stores/Config.ts +++ b/packages/main/src/stores/Config.ts @@ -18,15 +18,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { debounce } from 'lodash'; -import { - applySnapshot, - flow, - getSnapshot, - IDisposer, - Instance, - onSnapshot, -} from 'mobx-state-tree'; +import { Instance } from 'mobx-state-tree'; import { config as originalConfig, ConfigSnapshotIn, @@ -34,86 +26,11 @@ import { ThemeSource, } from '@sophie/shared'; -import { CONFIG_DEBOUNCE_TIME, ReadConfigResult } from '../services/ConfigPersistence'; -import { getEnv } from '../services/MainEnv'; - export const config = originalConfig.actions((self) => ({ setThemeSource(mode: ThemeSource) { self.themeSource = mode; }, -})).actions((self) => { - let lastSnapshotOnDisk: ConfigSnapshotOut | null = null; - let writingConfig = false; - let configMtime: Date | null = null; - let onSnapshotDisposer: IDisposer | null = null; - let watcherDisposer: IDisposer | null = null; - - function dispose() { - onSnapshotDisposer?.(); - watcherDisposer?.(); - } - - const actions: { - beforeDetach(): void, - readConfig(): Promise; - writeConfig(): Promise; - initConfig(): Promise; - } = { - beforeDetach() { - dispose(); - }, - readConfig: flow(function*() { - const result: ReadConfigResult = yield getEnv(self).configPersistence.readConfig(); - if (result.found) { - try { - applySnapshot(self, result.data); - lastSnapshotOnDisk = getSnapshot(self); - console.log('Loaded config'); - } catch (err) { - console.error('Failed to read config', result.data, err); - } - } - return result.found; - }), - writeConfig: flow(function*() { - const snapshot = getSnapshot(self); - writingConfig = true; - try { - configMtime = yield getEnv(self).configPersistence.writeConfig(snapshot); - lastSnapshotOnDisk = snapshot; - console.log('Wrote config'); - } finally { - writingConfig = false; - } - }), - initConfig: flow(function*() { - dispose(); - const foundConfig: boolean = yield actions.readConfig(); - if (!foundConfig) { - console.log('Creating new config file'); - try { - yield actions.writeConfig(); - } catch (err) { - console.error('Failed to initialize config'); - } - } - onSnapshotDisposer = onSnapshot(self, debounce((snapshot) => { - // We can compare snapshots by reference, since it is only recreated on store changes. - if (lastSnapshotOnDisk !== snapshot) { - actions.writeConfig().catch((err) => { - console.log('Failed to write config on config change', err); - }) - } - }, CONFIG_DEBOUNCE_TIME)); - watcherDisposer = getEnv(self).configPersistence.watchConfig(async (mtime) => { - if (!writingConfig && (configMtime === null || mtime > configMtime)) { - await actions.readConfig(); - } - }); - }), - }; - return actions; -}); +})); export interface Config extends Instance {} -- cgit v1.2.3-54-g00ecf