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/main/src | |
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/main/src')
-rw-r--r-- | packages/main/src/stores/MainStore.ts | 13 | ||||
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 4 |
2 files changed, 7 insertions, 10 deletions
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 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { applySnapshot, Instance, types } from 'mobx-state-tree'; | 21 | import { applySnapshot, Instance, types } from 'mobx-state-tree'; |
22 | import { BrowserViewBounds, emptySharedStore } from '@sophie/shared'; | 22 | import { BrowserViewBounds } from '@sophie/shared'; |
23 | 23 | ||
24 | import type { Config } from './Config'; | 24 | import type { Config } from './Config'; |
25 | import { sharedStore } from './SharedStore'; | 25 | import { sharedStore } from './SharedStore'; |
26 | 26 | ||
27 | export const mainStore = types.model('MainStore', { | 27 | export const mainStore = types.model('MainStore', { |
28 | browserViewBounds: types.model('BrowserViewBounds', { | 28 | browserViewBounds: types.optional(types.model('BrowserViewBounds', { |
29 | x: 0, | 29 | x: 0, |
30 | y: 0, | 30 | y: 0, |
31 | width: 0, | 31 | width: 0, |
32 | height: 0, | 32 | height: 0, |
33 | }), | 33 | }), {}), |
34 | shared: sharedStore, | 34 | shared: types.optional(sharedStore, {}), |
35 | }).views((self) => ({ | 35 | }).views((self) => ({ |
36 | get config(): Config { | 36 | get config(): Config { |
37 | return self.shared.config; | 37 | return self.shared.config; |
@@ -48,8 +48,5 @@ export const mainStore = types.model('MainStore', { | |||
48 | export interface MainStore extends Instance<typeof mainStore> {} | 48 | export interface MainStore extends Instance<typeof mainStore> {} |
49 | 49 | ||
50 | export function createMainStore(): MainStore { | 50 | export function createMainStore(): MainStore { |
51 | return mainStore.create({ | 51 | return mainStore.create(); |
52 | browserViewBounds: {}, | ||
53 | shared: emptySharedStore, | ||
54 | }); | ||
55 | } | 52 | } |
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 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { Instance } from 'mobx-state-tree'; | 21 | import { Instance, types } from 'mobx-state-tree'; |
22 | import { sharedStore as originalSharedStore } from '@sophie/shared'; | 22 | import { sharedStore as originalSharedStore } from '@sophie/shared'; |
23 | 23 | ||
24 | import { config } from './Config'; | 24 | import { config } from './Config'; |
@@ -26,7 +26,7 @@ import { config } from './Config'; | |||
26 | export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut } from '@sophie/shared'; | 26 | export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut } from '@sophie/shared'; |
27 | 27 | ||
28 | export const sharedStore = originalSharedStore.props({ | 28 | export const sharedStore = originalSharedStore.props({ |
29 | config, | 29 | config: types.optional(config, {}), |
30 | }); | 30 | }); |
31 | 31 | ||
32 | export interface SharedStore extends Instance<typeof sharedStore> {} | 32 | export interface SharedStore extends Instance<typeof sharedStore> {} |