aboutsummaryrefslogtreecommitdiffstats
path: root/packages/main/src/controllers/ConfigController.ts
blob: 6690548a34ce73dc3ebe04cb132b66a2239c1cbc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Copyright (C)  2021-2022 Kristóf Marussy <kristof@marussy.com>
 *
 * This file is part of Sophie.
 *
 * Sophie is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { debounce } from 'lodash';
import {
  applySnapshot,
  getSnapshot,
  IDisposer,
  onSnapshot,
} from 'mobx-state-tree';
import ms from 'ms';

import { ConfigPersistenceService } from '../services/ConfigPersistenceService';
import { Config, ConfigSnapshotOut } from '../stores/Config';

const DEFAULT_DEBOUNCE_TIME = ms('1s');

class ConfigController {
  readonly #config: Config;

  readonly #persistenceService: ConfigPersistenceService;

  readonly #onSnapshotDisposer: IDisposer;

  readonly #watcherDisposer: IDisposer;

  #lastSnapshotOnDisk: ConfigSnapshotOut | null = null;

  #writingConfig: boolean = false;

  #configMTime: Date | null = null;

  constructor(
    config: Config,
    persistenceService: ConfigPersistenceService,
    debounceTime: number,
  ) {
    this.#config = config;
    this.#persistenceService = persistenceService;
    this.#onSnapshotDisposer = onSnapshot(this.#config, debounce((snapshot) => {
      // We can compare snapshots by reference, since it is only recreated on store changes.
      if (this.#lastSnapshotOnDisk !== snapshot) {
        this.#writeConfig().catch((err) => {
          console.log('Failed to write config on config change', err);
        })
      }
    }, debounceTime));
    this.#watcherDisposer = this.#persistenceService.watchConfig(async (mtime) => {
      if (!this.#writingConfig && (this.#configMTime === null || mtime > this.#configMTime)) {
        await this.#readConfig();
      }
    }, debounceTime);
  }

  async #readConfig(): Promise<boolean> {
    const result = await this.#persistenceService.readConfig();
    if (result.found) {
      try {
        applySnapshot(this.#config, result.data);
        this.#lastSnapshotOnDisk = getSnapshot(this.#config);
        console.log('Loaded config');
      } catch (err) {
        console.error('Failed to read config', result.data, err);
      }
    }
    return result.found;
  }

  async #writeConfig(): Promise<void> {
    const snapshot = getSnapshot(this.#config);
    this.#writingConfig = true;
    try {
      this.#configMTime = await this.#persistenceService.writeConfig(snapshot);
      this.#lastSnapshotOnDisk = snapshot;
      console.log('Wrote config');
    } finally {
      this.#writingConfig = false;
    }
  }

  async initConfig(): Promise<void> {
    const foundConfig: boolean = await this.#readConfig();
    if (!foundConfig) {
      console.log('Creating new config file');
      try {
        await this.#writeConfig();
      } catch (err) {
        console.error('Failed to initialize config');
      }
    }
  }

  dispose(): void {
    this.#onSnapshotDisposer();
    this.#watcherDisposer();
  }
}

export async function initConfig(
  config: Config,
  persistenceService: ConfigPersistenceService,
  debounceTime: number = DEFAULT_DEBOUNCE_TIME,
): Promise<IDisposer> {
  const controller = new ConfigController(config, persistenceService, debounceTime);
  await controller.initConfig();
  return () => controller.dispose();
}