diff options
Diffstat (limited to 'packages/main/src/services')
-rw-r--r-- | packages/main/src/services/ConfigPersistenceService.ts | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/packages/main/src/services/ConfigPersistenceService.ts b/packages/main/src/services/ConfigPersistenceService.ts index 61123d9..34d0e3e 100644 --- a/packages/main/src/services/ConfigPersistenceService.ts +++ b/packages/main/src/services/ConfigPersistenceService.ts | |||
@@ -17,28 +17,25 @@ | |||
17 | * | 17 | * |
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | import { FSWatcher, watch } from 'fs'; | 20 | import { watch } from 'fs'; |
21 | import { readFile, stat, writeFile } from 'fs/promises'; | 21 | import { readFile, stat, writeFile } from 'fs/promises'; |
22 | import JSON5 from 'json5'; | 22 | import JSON5 from 'json5'; |
23 | import { throttle } from 'lodash'; | 23 | import { throttle } from 'lodash'; |
24 | import { join } from 'path'; | 24 | import { join } from 'path'; |
25 | 25 | ||
26 | import type { ConfigSnapshotOut } from '../stores/Config'; | 26 | import type { ConfigSnapshotOut } from '../stores/Config'; |
27 | import type { Disposer } from '../utils'; | ||
27 | 28 | ||
28 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; | 29 | export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; |
29 | 30 | ||
30 | export class ConfigPersistenceService { | 31 | export class ConfigPersistenceService { |
31 | static inject = ['userDataDir', 'configFileName'] as const; | ||
32 | |||
33 | private readonly configFilePath: string; | 32 | private readonly configFilePath: string; |
34 | 33 | ||
35 | private timeLastWritten: Date | null = null; | 34 | private timeLastWritten: Date | null = null; |
36 | 35 | ||
37 | private watcher: FSWatcher | null = null; | ||
38 | |||
39 | constructor( | 36 | constructor( |
40 | private readonly userDataDir: string, | 37 | private readonly userDataDir: string, |
41 | private readonly configFileName: string, | 38 | private readonly configFileName: string = 'config.json5', |
42 | ) { | 39 | ) { |
43 | this.configFileName = configFileName; | 40 | this.configFileName = configFileName; |
44 | this.configFilePath = join(this.userDataDir, this.configFileName); | 41 | this.configFilePath = join(this.userDataDir, this.configFileName); |
@@ -69,11 +66,7 @@ export class ConfigPersistenceService { | |||
69 | this.timeLastWritten = stats.mtime; | 66 | this.timeLastWritten = stats.mtime; |
70 | } | 67 | } |
71 | 68 | ||
72 | watchConfig(callback: () => Promise<void>, throttleMs: number): void { | 69 | watchConfig(callback: () => Promise<void>, throttleMs: number): Disposer { |
73 | if (this.watcher !== null) { | ||
74 | throw new Error('watchConfig was already called'); | ||
75 | } | ||
76 | |||
77 | const configChanged = throttle(async () => { | 70 | const configChanged = throttle(async () => { |
78 | let mtime: Date; | 71 | let mtime: Date; |
79 | try { | 72 | try { |
@@ -90,11 +83,11 @@ export class ConfigPersistenceService { | |||
90 | } | 83 | } |
91 | }, throttleMs); | 84 | }, throttleMs); |
92 | 85 | ||
93 | this.watcher = watch(this.userDataDir, { | 86 | const watcher = watch(this.userDataDir, { |
94 | persistent: false, | 87 | persistent: false, |
95 | }); | 88 | }); |
96 | 89 | ||
97 | this.watcher.on('change', (eventType, filename) => { | 90 | watcher.on('change', (eventType, filename) => { |
98 | if (eventType === 'change' | 91 | if (eventType === 'change' |
99 | && (filename === this.configFileName || filename === null)) { | 92 | && (filename === this.configFileName || filename === null)) { |
100 | configChanged()?.catch((err) => { | 93 | configChanged()?.catch((err) => { |
@@ -102,9 +95,7 @@ export class ConfigPersistenceService { | |||
102 | }); | 95 | }); |
103 | } | 96 | } |
104 | }); | 97 | }); |
105 | } | ||
106 | 98 | ||
107 | dispose(): void { | 99 | return () => watcher.close(); |
108 | this.watcher?.close(); | ||
109 | } | 100 | } |
110 | } | 101 | } |