diff options
author | 2021-12-26 19:17:23 +0100 | |
---|---|---|
committer | 2021-12-26 19:17:23 +0100 | |
commit | e56cdad02c00adf3b779d9de62d460e78be204a6 (patch) | |
tree | 638d8ac94380b3d675f13c6eabc1e5ee51126342 /packages/main/src/services | |
parent | refactor: Config persistence architecture (diff) | |
download | sophie-e56cdad02c00adf3b779d9de62d460e78be204a6.tar.gz sophie-e56cdad02c00adf3b779d9de62d460e78be204a6.tar.zst sophie-e56cdad02c00adf3b779d9de62d460e78be204a6.zip |
refactor: Clarify main process architecture
* stores: reactive data structures to hold application state
* controllers: subscribe to store changes and call store actions in
response to external events from services
* services: integrate with the nodejs and electron environment (should
be mocked for unit testing)
Diffstat (limited to 'packages/main/src/services')
-rw-r--r-- | packages/main/src/services/ConfigPersistenceService.ts (renamed from packages/main/src/services/impl/ConfigPersistenceImpl.ts) | 11 | ||||
-rw-r--r-- | packages/main/src/services/MainEnv.ts | 38 | ||||
-rw-r--r-- | packages/main/src/services/NativeThemeService.ts (renamed from packages/main/src/services/ConfigPersistence.ts) | 28 |
3 files changed, 21 insertions, 56 deletions
diff --git a/packages/main/src/services/impl/ConfigPersistenceImpl.ts b/packages/main/src/services/ConfigPersistenceService.ts index 097ab74..85b0088 100644 --- a/packages/main/src/services/impl/ConfigPersistenceImpl.ts +++ b/packages/main/src/services/ConfigPersistenceService.ts | |||
@@ -25,10 +25,11 @@ import { throttle } from 'lodash'; | |||
25 | import { IDisposer } from 'mobx-state-tree'; | 25 | import { IDisposer } from 'mobx-state-tree'; |
26 | import { join } from 'path'; | 26 | import { join } from 'path'; |
27 | 27 | ||
28 | import { CONFIG_DEBOUNCE_TIME, ConfigPersistence, ReadConfigResult } from '../ConfigPersistence'; | 28 | import type { ConfigSnapshotOut } from '../stores/Config'; |
29 | import type { ConfigSnapshotOut } from '../../stores/Config'; | ||
30 | 29 | ||
31 | export class ConfigPersistenceImpl implements ConfigPersistence { | 30 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; |
31 | |||
32 | export class ConfigPersistenceService { | ||
32 | readonly #userDataDir: string; | 33 | readonly #userDataDir: string; |
33 | 34 | ||
34 | readonly #configFileName: string; | 35 | readonly #configFileName: string; |
@@ -69,7 +70,7 @@ export class ConfigPersistenceImpl implements ConfigPersistence { | |||
69 | return mtime; | 70 | return mtime; |
70 | } | 71 | } |
71 | 72 | ||
72 | watchConfig(callback: (mtime: Date) => Promise<void>): IDisposer { | 73 | watchConfig(callback: (mtime: Date) => Promise<void>, throttleMs: number): IDisposer { |
73 | const configChanged = throttle(async () => { | 74 | const configChanged = throttle(async () => { |
74 | let mtime: Date; | 75 | let mtime: Date; |
75 | try { | 76 | try { |
@@ -82,7 +83,7 @@ export class ConfigPersistenceImpl implements ConfigPersistence { | |||
82 | throw err; | 83 | throw err; |
83 | } | 84 | } |
84 | return callback(mtime); | 85 | return callback(mtime); |
85 | }, CONFIG_DEBOUNCE_TIME); | 86 | }, throttleMs); |
86 | 87 | ||
87 | const watcher = watch(this.#userDataDir, { | 88 | const watcher = watch(this.#userDataDir, { |
88 | persistent: false, | 89 | persistent: false, |
diff --git a/packages/main/src/services/MainEnv.ts b/packages/main/src/services/MainEnv.ts deleted file mode 100644 index 23ee9a1..0000000 --- a/packages/main/src/services/MainEnv.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 | |||
21 | import { IAnyStateTreeNode, getEnv as getAnyEnv } from 'mobx-state-tree'; | ||
22 | |||
23 | import type { ConfigPersistence } from './ConfigPersistence'; | ||
24 | |||
25 | export interface MainEnv { | ||
26 | configPersistence: ConfigPersistence; | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Gets a well-typed environment from `model`. | ||
31 | * | ||
32 | * Only useable inside state trees created by `createAndConnectRootStore`. | ||
33 | * | ||
34 | * @param model The state tree node. | ||
35 | */ | ||
36 | export function getEnv(model: IAnyStateTreeNode): MainEnv { | ||
37 | return getAnyEnv<MainEnv>(model); | ||
38 | } | ||
diff --git a/packages/main/src/services/ConfigPersistence.ts b/packages/main/src/services/NativeThemeService.ts index f9a82de..7a26c3c 100644 --- a/packages/main/src/services/ConfigPersistence.ts +++ b/packages/main/src/services/NativeThemeService.ts | |||
@@ -18,19 +18,21 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { IDisposer } from 'mobx-state-tree'; | 21 | import { nativeTheme } from 'electron'; |
22 | import ms from 'ms'; | 22 | import type { IDisposer } from 'mobx-state-tree'; |
23 | import type { ThemeSource } from '@sophie/shared'; | ||
23 | 24 | ||
24 | import type { ConfigSnapshotOut } from '../stores/Config'; | 25 | export class NativeThemeService { |
26 | setThemeSource(themeSource: ThemeSource): void { | ||
27 | nativeTheme.themeSource = themeSource; | ||
28 | } | ||
25 | 29 | ||
26 | export const CONFIG_DEBOUNCE_TIME: number = ms('1s'); | 30 | onShouldUseDarkColorsUpdated(callback: (shouldUseDarkColors: boolean) => void): IDisposer { |
27 | 31 | const wrappedCallback = () => { | |
28 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; | 32 | callback(nativeTheme.shouldUseDarkColors); |
29 | 33 | }; | |
30 | export interface ConfigPersistence { | 34 | wrappedCallback(); |
31 | readConfig(): Promise<ReadConfigResult>; | 35 | nativeTheme.on('updated', wrappedCallback); |
32 | 36 | return () => nativeTheme.off('updated', wrappedCallback); | |
33 | writeConfig(configSnapshot: ConfigSnapshotOut): Promise<Date>; | 37 | } |
34 | |||
35 | watchConfig(callback: (mtime: Date) => Promise<void>): IDisposer; | ||
36 | } | 38 | } |