diff options
Diffstat (limited to 'packages/main/src/stores/SharedStore.ts')
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 112 |
1 files changed, 102 insertions, 10 deletions
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts index 76ce05c..861c8ce 100644 --- a/packages/main/src/stores/SharedStore.ts +++ b/packages/main/src/stores/SharedStore.ts | |||
@@ -19,19 +19,111 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { sharedStore as originalSharedStore } from '@sophie/shared'; | 21 | import { sharedStore as originalSharedStore } from '@sophie/shared'; |
22 | import { Instance, types } from 'mobx-state-tree'; | 22 | import { |
23 | applySnapshot, | ||
24 | getSnapshot, | ||
25 | Instance, | ||
26 | resolveIdentifier, | ||
27 | types, | ||
28 | } from 'mobx-state-tree'; | ||
23 | 29 | ||
24 | import { config } from './Config'; | 30 | import SettingsWithId from '../utils/SettingsWithId'; |
25 | import { runtimeService } from './RuntimeService'; | 31 | import { getLogger } from '../utils/log'; |
32 | import overrideProps from '../utils/overrideProps'; | ||
33 | |||
34 | import { globalSettings, GlobalSettingsSnapshotIn } from './GlobalSettings'; | ||
35 | import { addMissingProfileIds, profile, ProfileConfig } from './Profile'; | ||
36 | import { | ||
37 | addMissingServiceIdsAndProfiles, | ||
38 | service, | ||
39 | ServiceConfig, | ||
40 | } from './Service'; | ||
41 | |||
42 | const log = getLogger('sharedStore'); | ||
43 | |||
44 | export interface Config extends GlobalSettingsSnapshotIn { | ||
45 | profiles?: ProfileConfig[] | undefined; | ||
46 | |||
47 | services?: ServiceConfig[] | undefined; | ||
48 | } | ||
49 | |||
50 | function getConfigs<T>(models: { config: T }[]): T[] | undefined { | ||
51 | return models.length === 0 ? undefined : models.map((model) => model.config); | ||
52 | } | ||
53 | |||
54 | function reconcileSettings<T>( | ||
55 | originalSnapshots: SettingsWithId<T>[], | ||
56 | settingsSnapshotsWithId: SettingsWithId<T>[], | ||
57 | ): SettingsWithId<T>[] { | ||
58 | const idToOriginalSnapshots = new Map( | ||
59 | originalSnapshots.map((originalSnapshot) => [ | ||
60 | originalSnapshot.id, | ||
61 | originalSnapshot, | ||
62 | ]), | ||
63 | ); | ||
64 | return settingsSnapshotsWithId.map(({ id, settings }) => ({ | ||
65 | ...idToOriginalSnapshots.get(id), | ||
66 | id, | ||
67 | settings, | ||
68 | })); | ||
69 | } | ||
70 | |||
71 | export const sharedStore = overrideProps(originalSharedStore, { | ||
72 | settings: types.optional(globalSettings, {}), | ||
73 | profiles: types.array(profile), | ||
74 | services: types.array(service), | ||
75 | selectedService: types.safeReference(service), | ||
76 | }) | ||
77 | .views((self) => ({ | ||
78 | get config(): Config { | ||
79 | const { settings, profiles, services } = self; | ||
80 | const globalSettingsConfig = getSnapshot(settings); | ||
81 | return { | ||
82 | ...globalSettingsConfig, | ||
83 | profiles: getConfigs(profiles), | ||
84 | services: getConfigs(services), | ||
85 | }; | ||
86 | }, | ||
87 | })) | ||
88 | .actions((self) => ({ | ||
89 | loadConfig(config: Config): void { | ||
90 | const snapshot = getSnapshot(self); | ||
91 | const { profiles, services, ...settings } = config; | ||
92 | const profileSettingsSnapshots = addMissingProfileIds(profiles); | ||
93 | const serviceSettingsSnapshots = addMissingServiceIdsAndProfiles( | ||
94 | services, | ||
95 | profileSettingsSnapshots, | ||
96 | ); | ||
97 | applySnapshot(self, { | ||
98 | ...snapshot, | ||
99 | settings, | ||
100 | profiles: reconcileSettings( | ||
101 | snapshot.profiles, | ||
102 | profileSettingsSnapshots, | ||
103 | ), | ||
104 | services: reconcileSettings( | ||
105 | snapshot.services, | ||
106 | serviceSettingsSnapshots, | ||
107 | ), | ||
108 | }); | ||
109 | }, | ||
110 | setShouldUseDarkColors(shouldUseDarkColors: boolean): void { | ||
111 | self.shouldUseDarkColors = shouldUseDarkColors; | ||
112 | }, | ||
113 | setSelectedServiceId(serviceId: string): void { | ||
114 | const serviceInstance = resolveIdentifier(service, self, serviceId); | ||
115 | if (serviceInstance === undefined) { | ||
116 | log.warn('Trying to select unknown service', serviceId); | ||
117 | return; | ||
118 | } | ||
119 | self.selectedService = serviceInstance; | ||
120 | log.debug('Selected service', serviceId); | ||
121 | }, | ||
122 | })); | ||
123 | |||
124 | export interface SharedStore extends Instance<typeof sharedStore> {} | ||
26 | 125 | ||
27 | export type { | 126 | export type { |
28 | SharedStoreSnapshotIn, | 127 | SharedStoreSnapshotIn, |
29 | SharedStoreSnapshotOut, | 128 | SharedStoreSnapshotOut, |
30 | } from '@sophie/shared'; | 129 | } from '@sophie/shared'; |
31 | |||
32 | export const sharedStore = originalSharedStore.props({ | ||
33 | config: types.optional(config, {}), | ||
34 | runtimeServices: types.map(runtimeService), | ||
35 | }); | ||
36 | |||
37 | export interface SharedStore extends Instance<typeof sharedStore> {} | ||