diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-28 13:51:16 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-28 13:51:16 +0100 |
commit | 5712e88785d600a63d59cb583f045375c8c16255 (patch) | |
tree | 0abe682c30d1d9c03f4dce2f6d551615026ee368 /packages/main/src/controllers | |
parent | refactor: Get rid of dependency injector (diff) | |
download | sophie-5712e88785d600a63d59cb583f045375c8c16255.tar.gz sophie-5712e88785d600a63d59cb583f045375c8c16255.tar.zst sophie-5712e88785d600a63d59cb583f045375c8c16255.zip |
refactor: Functional design for controllers
Diffstat (limited to 'packages/main/src/controllers')
-rw-r--r-- | packages/main/src/controllers/ConfigController.ts | 99 | ||||
-rw-r--r-- | packages/main/src/controllers/config.ts | 93 | ||||
-rw-r--r-- | packages/main/src/controllers/nativeTheme.ts (renamed from packages/main/src/controllers/NativeThemeController.ts) | 28 |
3 files changed, 107 insertions, 113 deletions
diff --git a/packages/main/src/controllers/ConfigController.ts b/packages/main/src/controllers/ConfigController.ts deleted file mode 100644 index 318506f..0000000 --- a/packages/main/src/controllers/ConfigController.ts +++ /dev/null | |||
@@ -1,99 +0,0 @@ | |||
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 { debounce } from 'lodash'; | ||
22 | import ms from 'ms'; | ||
23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; | ||
24 | |||
25 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; | ||
26 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; | ||
27 | import { DisposeHelper } from '../utils'; | ||
28 | |||
29 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); | ||
30 | |||
31 | export class ConfigController extends DisposeHelper { | ||
32 | private config: Config | null = null; | ||
33 | |||
34 | private lastSnapshotOnDisk: ConfigSnapshotOut | null = null; | ||
35 | |||
36 | private writingConfig: boolean = false; | ||
37 | |||
38 | constructor( | ||
39 | private readonly persistenceService: ConfigPersistenceService, | ||
40 | private readonly debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, | ||
41 | ) { | ||
42 | super(); | ||
43 | } | ||
44 | |||
45 | async connect(config: Config): Promise<void> { | ||
46 | this.config = config; | ||
47 | |||
48 | const foundConfig: boolean = await this.readConfig(); | ||
49 | if (!foundConfig) { | ||
50 | console.log('Creating new config file'); | ||
51 | try { | ||
52 | await this.writeConfig(); | ||
53 | } catch (err) { | ||
54 | console.error('Failed to initialize config'); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | this.registerDisposable(onSnapshot(this.config, debounce((snapshot) => { | ||
59 | // We can compare snapshots by reference, since it is only recreated on store changes. | ||
60 | if (this.lastSnapshotOnDisk !== snapshot) { | ||
61 | this.writeConfig().catch((err) => { | ||
62 | console.log('Failed to write config on config change', err); | ||
63 | }) | ||
64 | } | ||
65 | }, this.debounceTime))); | ||
66 | |||
67 | this.registerDisposable(this.persistenceService.watchConfig(async () => { | ||
68 | if (!this.writingConfig) { | ||
69 | await this.readConfig(); | ||
70 | } | ||
71 | }, this.debounceTime)); | ||
72 | } | ||
73 | |||
74 | private async readConfig(): Promise<boolean> { | ||
75 | const result = await this.persistenceService.readConfig(); | ||
76 | if (result.found) { | ||
77 | try { | ||
78 | applySnapshot(this.config!, result.data); | ||
79 | this.lastSnapshotOnDisk = getSnapshot(this.config!); | ||
80 | console.log('Loaded config'); | ||
81 | } catch (err) { | ||
82 | console.error('Failed to read config', result.data, err); | ||
83 | } | ||
84 | } | ||
85 | return result.found; | ||
86 | } | ||
87 | |||
88 | private async writeConfig(): Promise<void> { | ||
89 | const snapshot = getSnapshot(this.config!); | ||
90 | this.writingConfig = true; | ||
91 | try { | ||
92 | await this.persistenceService.writeConfig(snapshot); | ||
93 | this.lastSnapshotOnDisk = snapshot; | ||
94 | console.log('Wrote config'); | ||
95 | } finally { | ||
96 | this.writingConfig = false; | ||
97 | } | ||
98 | } | ||
99 | } | ||
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts new file mode 100644 index 0000000..c7b027d --- /dev/null +++ b/packages/main/src/controllers/config.ts | |||
@@ -0,0 +1,93 @@ | |||
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 { debounce } from 'lodash'; | ||
22 | import ms from 'ms'; | ||
23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; | ||
24 | |||
25 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; | ||
26 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; | ||
27 | import { Disposer } from '../utils'; | ||
28 | |||
29 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); | ||
30 | |||
31 | export async function initConfig( | ||
32 | config: Config, | ||
33 | persistenceService: ConfigPersistenceService, | ||
34 | debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, | ||
35 | ): Promise<Disposer> { | ||
36 | let lastSnapshotOnDisk: ConfigSnapshotOut | null = null; | ||
37 | let writingConfig: boolean = false; | ||
38 | |||
39 | async function readConfig(): Promise<boolean> { | ||
40 | const result = await persistenceService.readConfig(); | ||
41 | if (result.found) { | ||
42 | try { | ||
43 | applySnapshot(config, result.data); | ||
44 | lastSnapshotOnDisk = getSnapshot(config); | ||
45 | console.log('Loaded config'); | ||
46 | } catch (err) { | ||
47 | console.error('Failed to read config', result.data, err); | ||
48 | } | ||
49 | } | ||
50 | return result.found; | ||
51 | } | ||
52 | |||
53 | async function writeConfig(): Promise<void> { | ||
54 | const snapshot = getSnapshot(config); | ||
55 | writingConfig = true; | ||
56 | try { | ||
57 | await persistenceService.writeConfig(snapshot); | ||
58 | lastSnapshotOnDisk = snapshot; | ||
59 | console.log('Wrote config'); | ||
60 | } finally { | ||
61 | writingConfig = false; | ||
62 | } | ||
63 | } | ||
64 | |||
65 | if (!await readConfig()) { | ||
66 | console.log('Creating new config file'); | ||
67 | try { | ||
68 | await writeConfig(); | ||
69 | } catch (err) { | ||
70 | console.error('Failed to initialize config'); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => { | ||
75 | // We can compare snapshots by reference, since it is only recreated on store changes. | ||
76 | if (lastSnapshotOnDisk !== snapshot) { | ||
77 | writeConfig().catch((err) => { | ||
78 | console.log('Failed to write config on config change', err); | ||
79 | }) | ||
80 | } | ||
81 | }, debounceTime)); | ||
82 | |||
83 | const disposeWatcher = persistenceService.watchConfig(async () => { | ||
84 | if (!writingConfig) { | ||
85 | await readConfig(); | ||
86 | } | ||
87 | }, debounceTime); | ||
88 | |||
89 | return () => { | ||
90 | disposeWatcher(); | ||
91 | disposeOnSnapshot(); | ||
92 | }; | ||
93 | } | ||
diff --git a/packages/main/src/controllers/NativeThemeController.ts b/packages/main/src/controllers/nativeTheme.ts index 931660c..e4390a8 100644 --- a/packages/main/src/controllers/NativeThemeController.ts +++ b/packages/main/src/controllers/nativeTheme.ts | |||
@@ -22,21 +22,21 @@ import { nativeTheme } from 'electron'; | |||
22 | import { autorun } from 'mobx'; | 22 | import { autorun } from 'mobx'; |
23 | 23 | ||
24 | import type { MainStore } from '../stores/MainStore'; | 24 | import type { MainStore } from '../stores/MainStore'; |
25 | import { DisposeHelper } from '../utils'; | 25 | import { Disposer } from '../utils'; |
26 | 26 | ||
27 | export class NativeThemeController extends DisposeHelper { | 27 | export function initNativeTheme(store: MainStore): Disposer { |
28 | connect(store: MainStore): void { | 28 | const disposeThemeSourceReaction = autorun(() => { |
29 | this.registerDisposable(autorun(() => { | 29 | nativeTheme.themeSource = store.config.themeSource; |
30 | nativeTheme.themeSource = store.config.themeSource; | 30 | }); |
31 | })); | ||
32 | 31 | ||
32 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | ||
33 | const shouldUseDarkColorsListener = () => { | ||
33 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | 34 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); |
34 | const shouldUseDarkColorsListener = () => { | 35 | }; |
35 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | 36 | nativeTheme.on('updated', shouldUseDarkColorsListener); |
36 | }; | 37 | |
37 | nativeTheme.on('updated', shouldUseDarkColorsListener); | 38 | return () => { |
38 | this.registerDisposable(() => { | 39 | nativeTheme.off('updated', shouldUseDarkColorsListener); |
39 | nativeTheme.off('updated', shouldUseDarkColorsListener); | 40 | disposeThemeSourceReaction(); |
40 | }); | 41 | }; |
41 | } | ||
42 | } | 42 | } |