aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/services/ConfigPersistenceService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/main/src/services/ConfigPersistenceService.ts')
-rw-r--r--packages/main/src/services/ConfigPersistenceService.ts23
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 */
20import { FSWatcher, watch } from 'fs'; 20import { watch } from 'fs';
21import { readFile, stat, writeFile } from 'fs/promises'; 21import { readFile, stat, writeFile } from 'fs/promises';
22import JSON5 from 'json5'; 22import JSON5 from 'json5';
23import { throttle } from 'lodash'; 23import { throttle } from 'lodash';
24import { join } from 'path'; 24import { join } from 'path';
25 25
26import type { ConfigSnapshotOut } from '../stores/Config'; 26import type { ConfigSnapshotOut } from '../stores/Config';
27import type { Disposer } from '../utils';
27 28
28export type ReadConfigResult = { found: true; data: unknown; } | { found: false; }; 29export type ReadConfigResult = { found: true; data: unknown; } | { found: false; };
29 30
30export class ConfigPersistenceService { 31export 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}