diff options
Diffstat (limited to 'packages/main/src/reactions/synchronizeConfig.ts')
-rw-r--r-- | packages/main/src/reactions/synchronizeConfig.ts | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/packages/main/src/reactions/synchronizeConfig.ts b/packages/main/src/reactions/synchronizeConfig.ts index f3c8b4a..6044ee7 100644 --- a/packages/main/src/reactions/synchronizeConfig.ts +++ b/packages/main/src/reactions/synchronizeConfig.ts | |||
@@ -51,11 +51,16 @@ export default async function synchronizeConfig( | |||
51 | const result = await repository.readConfig(); | 51 | const result = await repository.readConfig(); |
52 | if (result.found) { | 52 | if (result.found) { |
53 | const { contents } = result; | 53 | const { contents } = result; |
54 | if (contents === lastConfigOnDisk) { | ||
55 | // No need to re-apply config if we have already applied it. | ||
56 | return true; | ||
57 | } | ||
54 | try { | 58 | try { |
55 | // This cast is unsound if the config file is invalid, | 59 | // This cast is unsound if the config file is invalid, |
56 | // but we'll throw an error in the end anyways. | 60 | // but we'll throw an error in the end anyways. |
57 | const data = JSON.parse(contents) as Config; | 61 | const data = JSON.parse(contents) as Config; |
58 | sharedStore.loadConfig(data); | 62 | sharedStore.loadConfig(data); |
63 | log.info('Loaded config'); | ||
59 | } catch (error) { | 64 | } catch (error) { |
60 | log.error('Failed to apply config snapshot', contents, error); | 65 | log.error('Failed to apply config snapshot', contents, error); |
61 | return true; | 66 | return true; |
@@ -75,16 +80,18 @@ export default async function synchronizeConfig( | |||
75 | log.info('Created config file'); | 80 | log.info('Created config file'); |
76 | } | 81 | } |
77 | 82 | ||
83 | const debouncedSerializeConfig = debounce(() => { | ||
84 | const serializedConfig = serializeConfig(sharedStore.config); | ||
85 | if (serializedConfig !== lastConfigOnDisk) { | ||
86 | writeConfig(serializedConfig).catch((error) => { | ||
87 | log.error('Failed to write config on config change', error); | ||
88 | }); | ||
89 | } | ||
90 | }, debounceTime); | ||
91 | |||
78 | const disposeReaction = reaction( | 92 | const disposeReaction = reaction( |
79 | () => sharedStore.config, | 93 | () => sharedStore.config, |
80 | debounce(() => { | 94 | debouncedSerializeConfig, |
81 | const serializedConfig = serializeConfig(sharedStore.config); | ||
82 | if (serializedConfig !== lastConfigOnDisk) { | ||
83 | writeConfig(serializedConfig).catch((error) => { | ||
84 | log.error('Failed to write config on config change', error); | ||
85 | }); | ||
86 | } | ||
87 | }, debounceTime), | ||
88 | ); | 95 | ); |
89 | 96 | ||
90 | const disposeWatcher = repository.watchConfig(async () => { | 97 | const disposeWatcher = repository.watchConfig(async () => { |
@@ -93,10 +100,11 @@ export default async function synchronizeConfig( | |||
93 | } catch (error) { | 100 | } catch (error) { |
94 | log.error('Failed to read config', error); | 101 | log.error('Failed to read config', error); |
95 | } | 102 | } |
96 | }, debounceTime); | 103 | }); |
97 | 104 | ||
98 | return () => { | 105 | return () => { |
99 | disposeWatcher(); | 106 | disposeWatcher(); |
100 | disposeReaction(); | 107 | disposeReaction(); |
108 | debouncedSerializeConfig.flush(); | ||
101 | }; | 109 | }; |
102 | } | 110 | } |