diff options
Diffstat (limited to 'packages/main/src/controllers/initConfig.ts')
-rw-r--r-- | packages/main/src/controllers/initConfig.ts | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/packages/main/src/controllers/initConfig.ts b/packages/main/src/controllers/initConfig.ts index 93be978..2dcabaf 100644 --- a/packages/main/src/controllers/initConfig.ts +++ b/packages/main/src/controllers/initConfig.ts | |||
@@ -18,12 +18,13 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import deepEqual from 'deep-equal'; | ||
21 | import { debounce } from 'lodash-es'; | 22 | import { debounce } from 'lodash-es'; |
22 | import { getSnapshot, onSnapshot } from 'mobx-state-tree'; | 23 | import { getSnapshot, onSnapshot } from 'mobx-state-tree'; |
23 | import ms from 'ms'; | 24 | import ms from 'ms'; |
24 | 25 | ||
25 | import type ConfigPersistenceService from '../services/ConfigPersistenceService.js'; | 26 | import type ConfigPersistenceService from '../services/ConfigPersistenceService'; |
26 | import { Config, ConfigFileIn, ConfigSnapshotOut } from '../stores/Config.js'; | 27 | import { Config, ConfigFileIn, ConfigSnapshotOut } from '../stores/Config'; |
27 | import type Disposer from '../utils/Disposer'; | 28 | import type Disposer from '../utils/Disposer'; |
28 | import { getLogger } from '../utils/log'; | 29 | import { getLogger } from '../utils/log'; |
29 | 30 | ||
@@ -40,6 +41,12 @@ export default async function initConfig( | |||
40 | 41 | ||
41 | let lastSnapshotOnDisk: ConfigSnapshotOut | undefined; | 42 | let lastSnapshotOnDisk: ConfigSnapshotOut | undefined; |
42 | 43 | ||
44 | async function writeConfig(): Promise<void> { | ||
45 | const snapshot = getSnapshot(config); | ||
46 | await persistenceService.writeConfig(snapshot); | ||
47 | lastSnapshotOnDisk = snapshot; | ||
48 | } | ||
49 | |||
43 | async function readConfig(): Promise<boolean> { | 50 | async function readConfig(): Promise<boolean> { |
44 | const result = await persistenceService.readConfig(); | 51 | const result = await persistenceService.readConfig(); |
45 | if (result.found) { | 52 | if (result.found) { |
@@ -49,18 +56,16 @@ export default async function initConfig( | |||
49 | config.loadFromConfigFile(result.data as ConfigFileIn); | 56 | config.loadFromConfigFile(result.data as ConfigFileIn); |
50 | } catch (error) { | 57 | } catch (error) { |
51 | log.error('Failed to apply config snapshot', result.data, error); | 58 | log.error('Failed to apply config snapshot', result.data, error); |
59 | return true; | ||
60 | } | ||
61 | lastSnapshotOnDisk = getSnapshot(config); | ||
62 | if (!deepEqual(result.data, lastSnapshotOnDisk, { strict: true })) { | ||
63 | await writeConfig(); | ||
52 | } | 64 | } |
53 | } | 65 | } |
54 | lastSnapshotOnDisk = getSnapshot(config); | ||
55 | return result.found; | 66 | return result.found; |
56 | } | 67 | } |
57 | 68 | ||
58 | async function writeConfig(): Promise<void> { | ||
59 | const snapshot = getSnapshot(config); | ||
60 | await persistenceService.writeConfig(snapshot); | ||
61 | lastSnapshotOnDisk = snapshot; | ||
62 | } | ||
63 | |||
64 | if (!(await readConfig())) { | 69 | if (!(await readConfig())) { |
65 | log.info('Config file was not found'); | 70 | log.info('Config file was not found'); |
66 | await writeConfig(); | 71 | await writeConfig(); |