From 4ef4306cf401829905f764845ed78ac072fb94b6 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sun, 26 Dec 2021 19:59:04 +0100 Subject: refactor: Make all stores optional This reduces boilerplate and helps with config file robustness: if a field is missing from the config file, it will be replaced with its default value. --- packages/main/src/stores/MainStore.ts | 13 +++++-------- packages/main/src/stores/SharedStore.ts | 4 ++-- packages/renderer/src/stores/RootStore.ts | 7 ++----- packages/shared/src/index.ts | 4 ++-- packages/shared/src/stores/Config.ts | 8 ++------ packages/shared/src/stores/SharedStore.ts | 10 +++------- 6 files changed, 16 insertions(+), 30 deletions(-) (limited to 'packages') diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts index 4b85c22..bab03c2 100644 --- a/packages/main/src/stores/MainStore.ts +++ b/packages/main/src/stores/MainStore.ts @@ -19,19 +19,19 @@ */ import { applySnapshot, Instance, types } from 'mobx-state-tree'; -import { BrowserViewBounds, emptySharedStore } from '@sophie/shared'; +import { BrowserViewBounds } from '@sophie/shared'; import type { Config } from './Config'; import { sharedStore } from './SharedStore'; export const mainStore = types.model('MainStore', { - browserViewBounds: types.model('BrowserViewBounds', { + browserViewBounds: types.optional(types.model('BrowserViewBounds', { x: 0, y: 0, width: 0, height: 0, - }), - shared: sharedStore, + }), {}), + shared: types.optional(sharedStore, {}), }).views((self) => ({ get config(): Config { return self.shared.config; @@ -48,8 +48,5 @@ export const mainStore = types.model('MainStore', { export interface MainStore extends Instance {} export function createMainStore(): MainStore { - return mainStore.create({ - browserViewBounds: {}, - shared: emptySharedStore, - }); + return mainStore.create(); } diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts index 04dda32..e20150d 100644 --- a/packages/main/src/stores/SharedStore.ts +++ b/packages/main/src/stores/SharedStore.ts @@ -18,7 +18,7 @@ * SPDX-License-Identifier: AGPL-3.0-only */ -import { Instance } from 'mobx-state-tree'; +import { Instance, types } from 'mobx-state-tree'; import { sharedStore as originalSharedStore } from '@sophie/shared'; import { config } from './Config'; @@ -26,7 +26,7 @@ import { config } from './Config'; export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut } from '@sophie/shared'; export const sharedStore = originalSharedStore.props({ - config, + config: types.optional(config, {}), }); export interface SharedStore extends Instance {} diff --git a/packages/renderer/src/stores/RootStore.ts b/packages/renderer/src/stores/RootStore.ts index 20e0afa..f7f37f0 100644 --- a/packages/renderer/src/stores/RootStore.ts +++ b/packages/renderer/src/stores/RootStore.ts @@ -28,7 +28,6 @@ import { } from 'mobx-state-tree'; import { BrowserViewBounds, - emptySharedStore, sharedStore, SophieRenderer, ThemeSource, @@ -50,7 +49,7 @@ export function getEnv(model: IAnyStateTreeNode): RootEnv { } export const rootStore = types.model('RootStore', { - shared: sharedStore, + shared: types.optional(sharedStore, {}), }).actions((self) => ({ setBrowserViewBounds(bounds: BrowserViewBounds) { getEnv(self).ipc.setBrowserViewBounds(bounds); @@ -78,9 +77,7 @@ export interface RootStore extends Instance {} * @param ipc The `sophieRenderer` context bridge. */ export function createAndConnectRootStore(ipc: SophieRenderer): RootStore { - const store = rootStore.create({ - shared: emptySharedStore, - }, { + const store = rootStore.create({}, { ipc, }); diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 046d28d..713984e 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -35,7 +35,7 @@ export { } from './schemas'; export type { Config, ConfigSnapshotIn, ConfigSnapshotOut } from './stores/Config'; -export { config, defaultConfig } from './stores/Config'; +export { config } from './stores/Config'; export type { SharedStore, @@ -43,4 +43,4 @@ export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut, } from './stores/SharedStore'; -export { emptySharedStore, sharedStore } from './stores/SharedStore'; +export { sharedStore } from './stores/SharedStore'; diff --git a/packages/shared/src/stores/Config.ts b/packages/shared/src/stores/Config.ts index 1a9f924..432945c 100644 --- a/packages/shared/src/stores/Config.ts +++ b/packages/shared/src/stores/Config.ts @@ -27,14 +27,10 @@ import { import { themeSource } from '../schemas'; -export const config = types.model("Config", { - themeSource: types.enumeration(themeSource.options), +export const config = types.model('Config', { + themeSource: types.optional(types.enumeration(themeSource.options), 'system'), }); -export const defaultConfig: ConfigSnapshotIn = { - themeSource: 'system', -}; - export interface Config extends Instance {} export interface ConfigSnapshotIn extends SnapshotIn {} diff --git a/packages/shared/src/stores/SharedStore.ts b/packages/shared/src/stores/SharedStore.ts index 9f0afb1..cfff6d5 100644 --- a/packages/shared/src/stores/SharedStore.ts +++ b/packages/shared/src/stores/SharedStore.ts @@ -26,17 +26,13 @@ import { SnapshotOut, } from 'mobx-state-tree'; -import { config, defaultConfig } from './Config'; +import { config } from './Config'; -export const sharedStore = types.model("SharedStore", { - config, +export const sharedStore = types.model('SharedStore', { + config: types.optional(config, {}), shouldUseDarkColors: true, }); -export const emptySharedStore: SharedStoreSnapshotIn = { - config: defaultConfig, -}; - export interface SharedStore extends Instance {} export interface SharedStoreSnapshotIn extends SnapshotIn {} -- cgit v1.2.3-70-g09d2