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.ts32
1 files changed, 12 insertions, 20 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}