aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/controllers
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 13:06:11 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-28 13:06:11 +0100
commit6fd7d4855f26aa7f217094f815fc7c2ec14bed4f (patch)
tree208f7340b7688b035769b09248c0fd3564eaf40b /packages/main/src/controllers
parentrefactor: Inversion of control with typed-inject (diff)
downloadsophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.tar.gz
sophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.tar.zst
sophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.zip
refactor: Get rid of dependency injector
Diffstat (limited to 'packages/main/src/controllers')
-rw-r--r--packages/main/src/controllers/ConfigController.ts32
-rw-r--r--packages/main/src/controllers/MainController.ts38
-rw-r--r--packages/main/src/controllers/NativeThemeController.ts27
3 files changed, 22 insertions, 75 deletions
diff --git a/packages/main/src/controllers/ConfigController.ts b/packages/main/src/controllers/ConfigController.ts
index a28746c..318506f 100644
--- a/packages/main/src/controllers/ConfigController.ts
+++ b/packages/main/src/controllers/ConfigController.ts
@@ -19,34 +19,30 @@
19 */ 19 */
20 20
21import { debounce } from 'lodash'; 21import { debounce } from 'lodash';
22import { 22import ms from 'ms';
23 applySnapshot, 23import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree';
24 getSnapshot,
25 IDisposer,
26 onSnapshot,
27} from 'mobx-state-tree';
28 24
29import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; 25import type { ConfigPersistenceService } from '../services/ConfigPersistenceService';
30import type { Config, ConfigSnapshotOut } from '../stores/Config'; 26import type { Config, ConfigSnapshotOut } from '../stores/Config';
27import { DisposeHelper } from '../utils';
31 28
32export class ConfigController { 29const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s');
33 static inject = ['configPersistenceService', 'configDebounceTime'] as const;
34 30
31export class ConfigController extends DisposeHelper {
35 private config: Config | null = null; 32 private config: Config | null = null;
36 33
37 private onSnapshotDisposer: IDisposer | null = null;
38
39 private lastSnapshotOnDisk: ConfigSnapshotOut | null = null; 34 private lastSnapshotOnDisk: ConfigSnapshotOut | null = null;
40 35
41 private writingConfig: boolean = false; 36 private writingConfig: boolean = false;
42 37
43 constructor( 38 constructor(
44 private readonly persistenceService: ConfigPersistenceService, 39 private readonly persistenceService: ConfigPersistenceService,
45 private readonly debounceTime: number, 40 private readonly debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME,
46 ) { 41 ) {
42 super();
47 } 43 }
48 44
49 async initConfig(config: Config): Promise<void> { 45 async connect(config: Config): Promise<void> {
50 this.config = config; 46 this.config = config;
51 47
52 const foundConfig: boolean = await this.readConfig(); 48 const foundConfig: boolean = await this.readConfig();
@@ -59,20 +55,20 @@ export class ConfigController {
59 } 55 }
60 } 56 }
61 57
62 this.onSnapshotDisposer = onSnapshot(this.config, debounce((snapshot) => { 58 this.registerDisposable(onSnapshot(this.config, debounce((snapshot) => {
63 // We can compare snapshots by reference, since it is only recreated on store changes. 59 // We can compare snapshots by reference, since it is only recreated on store changes.
64 if (this.lastSnapshotOnDisk !== snapshot) { 60 if (this.lastSnapshotOnDisk !== snapshot) {
65 this.writeConfig().catch((err) => { 61 this.writeConfig().catch((err) => {
66 console.log('Failed to write config on config change', err); 62 console.log('Failed to write config on config change', err);
67 }) 63 })
68 } 64 }
69 }, this.debounceTime)); 65 }, this.debounceTime)));
70 66
71 this.persistenceService.watchConfig(async () => { 67 this.registerDisposable(this.persistenceService.watchConfig(async () => {
72 if (!this.writingConfig) { 68 if (!this.writingConfig) {
73 await this.readConfig(); 69 await this.readConfig();
74 } 70 }
75 }, this.debounceTime); 71 }, this.debounceTime));
76 } 72 }
77 73
78 private async readConfig(): Promise<boolean> { 74 private async readConfig(): Promise<boolean> {
@@ -100,8 +96,4 @@ export class ConfigController {
100 this.writingConfig = false; 96 this.writingConfig = false;
101 } 97 }
102 } 98 }
103
104 dispose(): void {
105 this.onSnapshotDisposer?.();
106 }
107} 99}
diff --git a/packages/main/src/controllers/MainController.ts b/packages/main/src/controllers/MainController.ts
deleted file mode 100644
index 6b97330..0000000
--- a/packages/main/src/controllers/MainController.ts
+++ /dev/null
@@ -1,38 +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
21import type { ConfigController } from './ConfigController';
22import type { NativeThemeController } from './NativeThemeController';
23import type { MainStore } from '../stores/MainStore';
24
25export class MainController {
26 static inject = ['configController', 'nativeThemeController'] as const;
27
28 constructor(
29 private readonly configController: ConfigController,
30 private readonly nativeThemeController: NativeThemeController,
31 ) {
32 }
33
34 async connect(store: MainStore): Promise<void> {
35 await this.configController.initConfig(store.config);
36 this.nativeThemeController.connect(store);
37 }
38}
diff --git a/packages/main/src/controllers/NativeThemeController.ts b/packages/main/src/controllers/NativeThemeController.ts
index a50d41e..931660c 100644
--- a/packages/main/src/controllers/NativeThemeController.ts
+++ b/packages/main/src/controllers/NativeThemeController.ts
@@ -20,30 +20,23 @@
20 20
21import { nativeTheme } from 'electron'; 21import { nativeTheme } from 'electron';
22import { autorun } from 'mobx'; 22import { autorun } from 'mobx';
23import { IDisposer } from 'mobx-state-tree';
24 23
25import type { MainStore } from '../stores/MainStore'; 24import type { MainStore } from '../stores/MainStore';
25import { DisposeHelper } from '../utils';
26 26
27export class NativeThemeController { 27export class NativeThemeController extends DisposeHelper {
28 private autorunDisposer: IDisposer | null = null;
29
30 private shouldUseDarkColorsListener: (() => void) | null = null;
31
32 connect(store: MainStore): void { 28 connect(store: MainStore): void {
33 this.autorunDisposer = autorun(() => { 29 this.registerDisposable(autorun(() => {
34 nativeTheme.themeSource = store.config.themeSource; 30 nativeTheme.themeSource = store.config.themeSource;
35 }); 31 }));
32
36 store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); 33 store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors);
37 this.shouldUseDarkColorsListener = () => { 34 const shouldUseDarkColorsListener = () => {
38 store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); 35 store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors);
39 }; 36 };
40 nativeTheme.on('updated', this.shouldUseDarkColorsListener); 37 nativeTheme.on('updated', shouldUseDarkColorsListener);
41 } 38 this.registerDisposable(() => {
42 39 nativeTheme.off('updated', shouldUseDarkColorsListener);
43 dispose(): void { 40 });
44 if (this.shouldUseDarkColorsListener !== null) {
45 nativeTheme.off('updated', this.shouldUseDarkColorsListener);
46 }
47 this.autorunDisposer?.();
48 } 41 }
49} 42}