diff options
Diffstat (limited to 'packages/main/src/reactions/synchronizeConfig.ts')
-rw-r--r-- | packages/main/src/reactions/synchronizeConfig.ts | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/packages/main/src/reactions/synchronizeConfig.ts b/packages/main/src/reactions/synchronizeConfig.ts new file mode 100644 index 0000000..480cc1a --- /dev/null +++ b/packages/main/src/reactions/synchronizeConfig.ts | |||
@@ -0,0 +1,98 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import deepEqual from 'deep-equal'; | ||
22 | import { debounce } from 'lodash-es'; | ||
23 | import { reaction } from 'mobx'; | ||
24 | import ms from 'ms'; | ||
25 | |||
26 | import type ConfigRepository from '../infrastructure/config/ConfigRepository'; | ||
27 | import type SharedStore from '../stores/SharedStore'; | ||
28 | import type { Config } from '../stores/SharedStore'; | ||
29 | import type Disposer from '../utils/Disposer'; | ||
30 | import { getLogger } from '../utils/log'; | ||
31 | |||
32 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); | ||
33 | |||
34 | const log = getLogger('synchronizeConfig'); | ||
35 | |||
36 | export default async function synchronizeConfig( | ||
37 | sharedStore: SharedStore, | ||
38 | repository: ConfigRepository, | ||
39 | debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, | ||
40 | ): Promise<Disposer> { | ||
41 | let lastConfigOnDisk: Config | undefined; | ||
42 | |||
43 | async function writeConfig(): Promise<void> { | ||
44 | const { config } = sharedStore; | ||
45 | await repository.writeConfig(config); | ||
46 | lastConfigOnDisk = config; | ||
47 | } | ||
48 | |||
49 | async function readConfig(): Promise<boolean> { | ||
50 | const result = await repository.readConfig(); | ||
51 | if (result.found) { | ||
52 | try { | ||
53 | // This cast is unsound if the config file is invalid, | ||
54 | // but we'll throw an error in the end anyways. | ||
55 | sharedStore.loadConfig(result.data as Config); | ||
56 | } catch (error) { | ||
57 | log.error('Failed to apply config snapshot', result.data, error); | ||
58 | return true; | ||
59 | } | ||
60 | lastConfigOnDisk = sharedStore.config; | ||
61 | if (!deepEqual(result.data, lastConfigOnDisk, { strict: true })) { | ||
62 | await writeConfig(); | ||
63 | } | ||
64 | } | ||
65 | return result.found; | ||
66 | } | ||
67 | |||
68 | if (!(await readConfig())) { | ||
69 | log.info('Config file was not found'); | ||
70 | await writeConfig(); | ||
71 | log.info('Created config file'); | ||
72 | } | ||
73 | |||
74 | const disposeReaction = reaction( | ||
75 | () => sharedStore.config, | ||
76 | debounce((config) => { | ||
77 | // We can compare snapshots by reference, since it is only recreated on store changes. | ||
78 | if (lastConfigOnDisk !== config) { | ||
79 | writeConfig().catch((error) => { | ||
80 | log.error('Failed to write config on config change', error); | ||
81 | }); | ||
82 | } | ||
83 | }, debounceTime), | ||
84 | ); | ||
85 | |||
86 | const disposeWatcher = repository.watchConfig(async () => { | ||
87 | try { | ||
88 | await readConfig(); | ||
89 | } catch (error) { | ||
90 | log.error('Failed to read config', error); | ||
91 | } | ||
92 | }, debounceTime); | ||
93 | |||
94 | return () => { | ||
95 | disposeWatcher(); | ||
96 | disposeReaction(); | ||
97 | }; | ||
98 | } | ||