diff options
author | 2021-12-28 13:06:11 +0100 | |
---|---|---|
committer | 2021-12-28 13:06:11 +0100 | |
commit | 6fd7d4855f26aa7f217094f815fc7c2ec14bed4f (patch) | |
tree | 208f7340b7688b035769b09248c0fd3564eaf40b /packages/main/src/controllers/ConfigController.ts | |
parent | refactor: Inversion of control with typed-inject (diff) | |
download | sophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.tar.gz sophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.tar.zst sophie-6fd7d4855f26aa7f217094f815fc7c2ec14bed4f.zip |
refactor: Get rid of dependency injector
Diffstat (limited to 'packages/main/src/controllers/ConfigController.ts')
-rw-r--r-- | packages/main/src/controllers/ConfigController.ts | 32 |
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 | ||
21 | import { debounce } from 'lodash'; | 21 | import { debounce } from 'lodash'; |
22 | import { | 22 | import ms from 'ms'; |
23 | applySnapshot, | 23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; |
24 | getSnapshot, | ||
25 | IDisposer, | ||
26 | onSnapshot, | ||
27 | } from 'mobx-state-tree'; | ||
28 | 24 | ||
29 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; | 25 | import type { ConfigPersistenceService } from '../services/ConfigPersistenceService'; |
30 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; | 26 | import type { Config, ConfigSnapshotOut } from '../stores/Config'; |
27 | import { DisposeHelper } from '../utils'; | ||
31 | 28 | ||
32 | export class ConfigController { | 29 | const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); |
33 | static inject = ['configPersistenceService', 'configDebounceTime'] as const; | ||
34 | 30 | ||
31 | export 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 | } |