From fe5d0bb29e850e36693cae5594adf16f764aedf9 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 26 Dec 2021 17:29:58 +0100 Subject: refactor: Config persistence architecture The architecture in the main process is split into 3 main parts: * services: interfaces for services are injected into the stores through the MainEnv interface (for testability) * services/impl: electron-specific implementations of services * stores: the actions of the stores can invoke (asynchronous) services --- packages/main/src/stores/MainStore.ts | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 packages/main/src/stores/MainStore.ts (limited to 'packages/main/src/stores/MainStore.ts') diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts new file mode 100644 index 0000000..ee215a7 --- /dev/null +++ b/packages/main/src/stores/MainStore.ts @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { applySnapshot, Instance, types } from 'mobx-state-tree'; +import { BrowserViewBounds, emptySharedStore } from '@sophie/shared'; + +import type { Config } from './Config'; +import { MainEnv } from '../services/MainEnv'; +import { sharedStore } from './SharedStore'; + +export const mainStore = types.model('MainStore', { + browserViewBounds: types.model('BrowserViewBounds', { + x: 0, + y: 0, + width: 0, + height: 0, + }), + shared: sharedStore, +}).views((self) => ({ + get config(): Config { + return self.shared.config; + }, +})).actions((self) => ({ + setBrowserViewBounds(bounds: BrowserViewBounds): void { + applySnapshot(self.browserViewBounds, bounds); + }, + setShouldUseDarkColors(shouldUseDarkColors: boolean): void { + self.shared.shouldUseDarkColors = shouldUseDarkColors; + } +})); + +export interface RootStore extends Instance {} + +export function createMainStore(env: MainEnv): RootStore { + return mainStore.create( + { + browserViewBounds: {}, + shared: emptySharedStore, + }, + env, + ); +} -- cgit v1.2.3-70-g09d2