diff options
Diffstat (limited to 'packages/main/src/controllers/initConfig.ts')
-rw-r--r-- | packages/main/src/controllers/initConfig.ts | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/packages/main/src/controllers/initConfig.ts b/packages/main/src/controllers/initConfig.ts index c8cd335..55bf6df 100644 --- a/packages/main/src/controllers/initConfig.ts +++ b/packages/main/src/controllers/initConfig.ts | |||
@@ -20,11 +20,11 @@ | |||
20 | 20 | ||
21 | import deepEqual from 'deep-equal'; | 21 | import deepEqual from 'deep-equal'; |
22 | import { debounce } from 'lodash-es'; | 22 | import { debounce } from 'lodash-es'; |
23 | import { getSnapshot, onSnapshot } from 'mobx-state-tree'; | 23 | import { reaction } from 'mobx'; |
24 | import ms from 'ms'; | 24 | import ms from 'ms'; |
25 | 25 | ||
26 | import type ConfigPersistence from '../infrastructure/ConfigPersistence'; | 26 | import type ConfigPersistence from '../infrastructure/ConfigPersistence'; |
27 | import { Config, ConfigFileIn, ConfigSnapshotOut } from '../stores/Config'; | 27 | import { Config, SharedStore } from '../stores/SharedStore'; |
28 | import type Disposer from '../utils/Disposer'; | 28 | import type Disposer from '../utils/Disposer'; |
29 | import { getLogger } from '../utils/log'; | 29 | import { getLogger } from '../utils/log'; |
30 | 30 | ||
@@ -33,18 +33,18 @@ const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); | |||
33 | const log = getLogger('config'); | 33 | const log = getLogger('config'); |
34 | 34 | ||
35 | export default async function initConfig( | 35 | export default async function initConfig( |
36 | config: Config, | 36 | sharedStore: SharedStore, |
37 | persistenceService: ConfigPersistence, | 37 | persistenceService: ConfigPersistence, |
38 | debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, | 38 | debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, |
39 | ): Promise<Disposer> { | 39 | ): Promise<Disposer> { |
40 | log.trace('Initializing config controller'); | 40 | log.trace('Initializing config controller'); |
41 | 41 | ||
42 | let lastSnapshotOnDisk: ConfigSnapshotOut | undefined; | 42 | let lastConfigOnDisk: Config | undefined; |
43 | 43 | ||
44 | async function writeConfig(): Promise<void> { | 44 | async function writeConfig(): Promise<void> { |
45 | const snapshot = getSnapshot(config); | 45 | const { config } = sharedStore; |
46 | await persistenceService.writeConfig(snapshot); | 46 | await persistenceService.writeConfig(config); |
47 | lastSnapshotOnDisk = snapshot; | 47 | lastConfigOnDisk = config; |
48 | } | 48 | } |
49 | 49 | ||
50 | async function readConfig(): Promise<boolean> { | 50 | async function readConfig(): Promise<boolean> { |
@@ -53,13 +53,13 @@ export default async function initConfig( | |||
53 | try { | 53 | try { |
54 | // This cast is unsound if the config file is invalid, | 54 | // This cast is unsound if the config file is invalid, |
55 | // but we'll throw an error in the end anyways. | 55 | // but we'll throw an error in the end anyways. |
56 | config.loadFromConfigFile(result.data as ConfigFileIn); | 56 | sharedStore.loadConfig(result.data as Config); |
57 | } catch (error) { | 57 | } catch (error) { |
58 | log.error('Failed to apply config snapshot', result.data, error); | 58 | log.error('Failed to apply config snapshot', result.data, error); |
59 | return true; | 59 | return true; |
60 | } | 60 | } |
61 | lastSnapshotOnDisk = getSnapshot(config); | 61 | lastConfigOnDisk = sharedStore.config; |
62 | if (!deepEqual(result.data, lastSnapshotOnDisk, { strict: true })) { | 62 | if (!deepEqual(result.data, lastConfigOnDisk, { strict: true })) { |
63 | await writeConfig(); | 63 | await writeConfig(); |
64 | } | 64 | } |
65 | } | 65 | } |
@@ -72,11 +72,11 @@ export default async function initConfig( | |||
72 | log.info('Created config file'); | 72 | log.info('Created config file'); |
73 | } | 73 | } |
74 | 74 | ||
75 | const disposeOnSnapshot = onSnapshot( | 75 | const disposeReaction = reaction( |
76 | config, | 76 | () => sharedStore.config, |
77 | debounce((snapshot) => { | 77 | debounce((config) => { |
78 | // We can compare snapshots by reference, since it is only recreated on store changes. | 78 | // We can compare snapshots by reference, since it is only recreated on store changes. |
79 | if (lastSnapshotOnDisk !== snapshot) { | 79 | if (lastConfigOnDisk !== config) { |
80 | writeConfig().catch((error) => { | 80 | writeConfig().catch((error) => { |
81 | log.error('Failed to write config on config change', error); | 81 | log.error('Failed to write config on config change', error); |
82 | }); | 82 | }); |
@@ -95,6 +95,6 @@ export default async function initConfig( | |||
95 | return () => { | 95 | return () => { |
96 | log.trace('Disposing config controller'); | 96 | log.trace('Disposing config controller'); |
97 | disposeWatcher(); | 97 | disposeWatcher(); |
98 | disposeOnSnapshot(); | 98 | disposeReaction(); |
99 | }; | 99 | }; |
100 | } | 100 | } |