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/shared/src/stores/Config.ts | 8 ++------ packages/shared/src/stores/SharedStore.ts | 10 +++------- 2 files changed, 5 insertions(+), 13 deletions(-) (limited to 'packages/shared/src/stores') 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-54-g00ecf