diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-26 19:59:04 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-26 20:02:08 +0100 |
commit | 4ef4306cf401829905f764845ed78ac072fb94b6 (patch) | |
tree | 980a91a11cb1ac5f730d72c385e542edd0617d82 /packages/shared | |
parent | refactor: Clarify main process architecture (diff) | |
download | sophie-4ef4306cf401829905f764845ed78ac072fb94b6.tar.gz sophie-4ef4306cf401829905f764845ed78ac072fb94b6.tar.zst sophie-4ef4306cf401829905f764845ed78ac072fb94b6.zip |
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.
Diffstat (limited to 'packages/shared')
-rw-r--r-- | packages/shared/src/index.ts | 4 | ||||
-rw-r--r-- | packages/shared/src/stores/Config.ts | 8 | ||||
-rw-r--r-- | packages/shared/src/stores/SharedStore.ts | 10 |
3 files changed, 7 insertions, 15 deletions
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 { | |||
35 | } from './schemas'; | 35 | } from './schemas'; |
36 | 36 | ||
37 | export type { Config, ConfigSnapshotIn, ConfigSnapshotOut } from './stores/Config'; | 37 | export type { Config, ConfigSnapshotIn, ConfigSnapshotOut } from './stores/Config'; |
38 | export { config, defaultConfig } from './stores/Config'; | 38 | export { config } from './stores/Config'; |
39 | 39 | ||
40 | export type { | 40 | export type { |
41 | SharedStore, | 41 | SharedStore, |
@@ -43,4 +43,4 @@ export type { | |||
43 | SharedStoreSnapshotIn, | 43 | SharedStoreSnapshotIn, |
44 | SharedStoreSnapshotOut, | 44 | SharedStoreSnapshotOut, |
45 | } from './stores/SharedStore'; | 45 | } from './stores/SharedStore'; |
46 | export { emptySharedStore, sharedStore } from './stores/SharedStore'; | 46 | 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 { | |||
27 | 27 | ||
28 | import { themeSource } from '../schemas'; | 28 | import { themeSource } from '../schemas'; |
29 | 29 | ||
30 | export const config = types.model("Config", { | 30 | export const config = types.model('Config', { |
31 | themeSource: types.enumeration(themeSource.options), | 31 | themeSource: types.optional(types.enumeration(themeSource.options), 'system'), |
32 | }); | 32 | }); |
33 | 33 | ||
34 | export const defaultConfig: ConfigSnapshotIn = { | ||
35 | themeSource: 'system', | ||
36 | }; | ||
37 | |||
38 | export interface Config extends Instance<typeof config> {} | 34 | export interface Config extends Instance<typeof config> {} |
39 | 35 | ||
40 | export interface ConfigSnapshotIn extends SnapshotIn<typeof config> {} | 36 | export interface ConfigSnapshotIn extends SnapshotIn<typeof config> {} |
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 { | |||
26 | SnapshotOut, | 26 | SnapshotOut, |
27 | } from 'mobx-state-tree'; | 27 | } from 'mobx-state-tree'; |
28 | 28 | ||
29 | import { config, defaultConfig } from './Config'; | 29 | import { config } from './Config'; |
30 | 30 | ||
31 | export const sharedStore = types.model("SharedStore", { | 31 | export const sharedStore = types.model('SharedStore', { |
32 | config, | 32 | config: types.optional(config, {}), |
33 | shouldUseDarkColors: true, | 33 | shouldUseDarkColors: true, |
34 | }); | 34 | }); |
35 | 35 | ||
36 | export const emptySharedStore: SharedStoreSnapshotIn = { | ||
37 | config: defaultConfig, | ||
38 | }; | ||
39 | |||
40 | export interface SharedStore extends Instance<typeof sharedStore> {} | 36 | export interface SharedStore extends Instance<typeof sharedStore> {} |
41 | 37 | ||
42 | export interface SharedStoreSnapshotIn extends SnapshotIn<typeof sharedStore> {} | 38 | export interface SharedStoreSnapshotIn extends SnapshotIn<typeof sharedStore> {} |