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 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) (limited to 'packages/main') 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 {} -- cgit v1.2.3-70-g09d2