aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/controllers/ConfigController.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/controllers/ConfigController.ts')
-rw-r--r--packages/main/src/controllers/ConfigController.ts99
1 files changed, 0 insertions, 99 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
21import { debounce } from 'lodash';
22import ms from 'ms';
23import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree';
24
25import type { ConfigPersistenceService } from '../services/ConfigPersistenceService';
26import type { Config, ConfigSnapshotOut } from '../stores/Config';
27import { DisposeHelper } from '../utils';
28
29const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s');
30
31export 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}