diff options
Diffstat (limited to 'packages/main/src/stores/SharedStore.ts')
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 63 |
1 files changed, 17 insertions, 46 deletions
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts index 9ec7963..499d1ee 100644 --- a/packages/main/src/stores/SharedStore.ts +++ b/packages/main/src/stores/SharedStore.ts | |||
@@ -55,25 +55,20 @@ function getConfigs<T>(models: { config: T }[]): T[] | undefined { | |||
55 | return models.length === 0 ? undefined : models.map((model) => model.config); | 55 | return models.length === 0 ? undefined : models.map((model) => model.config); |
56 | } | 56 | } |
57 | 57 | ||
58 | type TypeWithSettings<C> = IType< | 58 | function applySettings< |
59 | { id: string; settings: C }, | 59 | C, |
60 | unknown, | 60 | D extends IType< |
61 | { settings: IStateTreeNode<IType<C, unknown, unknown>> } | 61 | { id: string; settings: C }, |
62 | >; | 62 | unknown, |
63 | 63 | { settings: IStateTreeNode<IType<C, unknown, unknown>> } | |
64 | function deleteStaleModels<C, D extends TypeWithSettings<C>>( | 64 | >, |
65 | >( | ||
66 | current: IMSTArray<IReferenceType<D>>, | ||
65 | currentById: IMSTMap<D>, | 67 | currentById: IMSTMap<D>, |
66 | toApplyById: Map<string, C>, | 68 | toApply: [string, C][], |
67 | ): void { | 69 | ): void { |
70 | const toApplyById = new Map(toApply); | ||
68 | const toDelete = new Set(currentById.keys()); | 71 | const toDelete = new Set(currentById.keys()); |
69 | toApplyById.forEach((_settings, id) => toDelete.delete(id)); | ||
70 | toDelete.forEach((id) => currentById.delete(id)); | ||
71 | } | ||
72 | |||
73 | function applySettings<C, D extends TypeWithSettings<C>>( | ||
74 | currentById: IMSTMap<D>, | ||
75 | toApplyById: Map<string, C>, | ||
76 | ): void { | ||
77 | toApplyById.forEach((settingsSnapshot, id) => { | 72 | toApplyById.forEach((settingsSnapshot, id) => { |
78 | const model = currentById.get(id); | 73 | const model = currentById.get(id); |
79 | if (model === undefined) { | 74 | if (model === undefined) { |
@@ -82,16 +77,13 @@ function applySettings<C, D extends TypeWithSettings<C>>( | |||
82 | settings: settingsSnapshot, | 77 | settings: settingsSnapshot, |
83 | }); | 78 | }); |
84 | } else { | 79 | } else { |
80 | toDelete.delete(id); | ||
85 | applySnapshot(model.settings, settingsSnapshot); | 81 | applySnapshot(model.settings, settingsSnapshot); |
86 | } | 82 | } |
87 | }); | 83 | }); |
88 | } | 84 | current.clear(); |
89 | 85 | toDelete.forEach((id) => currentById.delete(id)); | |
90 | function pushReferences<C, D extends TypeWithSettings<C>>( | 86 | current.push(...toApply.map(([id]) => id)); |
91 | list: IMSTArray<IReferenceType<D>>, | ||
92 | toApply: [string, C][], | ||
93 | ): void { | ||
94 | list.push(...toApply.map(([id]) => id)); | ||
95 | } | 87 | } |
96 | 88 | ||
97 | export const sharedStore = overrideProps(originalSharedStore, { | 89 | export const sharedStore = overrideProps(originalSharedStore, { |
@@ -115,9 +107,6 @@ export const sharedStore = overrideProps(originalSharedStore, { | |||
115 | })) | 107 | })) |
116 | .actions((self) => ({ | 108 | .actions((self) => ({ |
117 | loadConfig(config: Config): void { | 109 | loadConfig(config: Config): void { |
118 | // `onPatch` will send store changes piecemeal without any attention to | ||
119 | // transaction boundaries. We must make sure that any state communicated to the | ||
120 | // renderer process is actually valid. | ||
121 | const { | 110 | const { |
122 | profiles, | 111 | profiles, |
123 | profilesById, | 112 | profilesById, |
@@ -137,27 +126,9 @@ export const sharedStore = overrideProps(originalSharedStore, { | |||
137 | servicesConfig, | 126 | servicesConfig, |
138 | profilesToApply, | 127 | profilesToApply, |
139 | ); | 128 | ); |
140 | const profilesToApplyById = new Map(profilesToApply); | 129 | applySettings(profiles, profilesById, profilesToApply); |
141 | const servicesToApplyById = new Map(servicesToApply); | 130 | applySettings(services, servicesById, servicesToApply); |
142 | // First remove any references to profiles and services that might be deleted. | ||
143 | self.selectedService = undefined; | ||
144 | services.clear(); | ||
145 | profiles.clear(); | ||
146 | // Delete all services that may depend on profiles that will be delete. | ||
147 | deleteStaleModels(servicesById, servicesToApplyById); | ||
148 | // Update existing profiles and add new profiles. | ||
149 | applySettings(profilesById, profilesToApplyById); | ||
150 | // Update existing services and add new services. This will make sure that no service | ||
151 | // depends on a profile that will be deleted. | ||
152 | applySettings(servicesById, servicesToApplyById); | ||
153 | // Now it's safe to delete stale profiles. | ||
154 | deleteStaleModels(profilesById, profilesToApplyById); | ||
155 | // We are ready to build new profile and service lists from the new models. | ||
156 | pushReferences(profiles, profilesToApply); | ||
157 | pushReferences(services, servicesToApply); | ||
158 | // Global settings may refer to particular profiles or services. | ||
159 | applySnapshot(settings, settingsToApply); | 131 | applySnapshot(settings, settingsToApply); |
160 | // Restore service selection (if applicable). | ||
161 | let newSelectedService; | 132 | let newSelectedService; |
162 | if (selectedServiceId !== undefined) { | 133 | if (selectedServiceId !== undefined) { |
163 | newSelectedService = servicesById.get(selectedServiceId); | 134 | newSelectedService = servicesById.get(selectedServiceId); |