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/index.ts | 4 ++-- packages/shared/src/stores/Config.ts | 8 ++------ packages/shared/src/stores/SharedStore.ts | 10 +++------- 3 files changed, 7 insertions(+), 15 deletions(-) (limited to 'packages/shared/src') 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