aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/SettingsStore.js
blob: 7cd7c91148df120071cee46b04ce7c402d1d4a29 (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
import { ipcRenderer } from 'electron';
import { action, computed, observable } from 'mobx';

import Store from './lib/Store';
import Request from './lib/Request';
import CachedRequest from './lib/CachedRequest';
import { gaEvent } from '../lib/analytics';
import { DEFAULT_APP_SETTINGS } from '../config';

export default class SettingsStore extends Store {
  @observable allSettingsRequest = new CachedRequest(this.api.local, 'getSettings');
  @observable updateSettingsRequest = new Request(this.api.local, 'updateSettings');
  @observable removeSettingsKeyRequest = new Request(this.api.local, 'removeKey');

  constructor(...args) {
    super(...args);

    // Register action handlers
    this.actions.settings.update.listen(this._update.bind(this));
    this.actions.settings.remove.listen(this._remove.bind(this));

    // this.registerReactions([
    //   this._shareSettingsWithMainProcess.bind(this),
    // ]);
  }

  setup() {
    this.allSettingsRequest.execute();
    this._shareSettingsWithMainProcess();
  }

  @computed get all() {
    return observable(Object.assign(DEFAULT_APP_SETTINGS, this.allSettingsRequest.result));
  }

  @action async _update({ settings }) {
    await this.updateSettingsRequest.execute(settings)._promise;
    this.allSettingsRequest.patch((result) => {
      if (!result) return;
      Object.assign(result, settings);
    });

    this._shareSettingsWithMainProcess();

    gaEvent('Settings', 'update');
  }

  @action async _remove({ key }) {
    await this.removeSettingsKeyRequest.execute(key);
    await this.allSettingsRequest.invalidate({ immediately: true });

    this._shareSettingsWithMainProcess();
  }

  // Reactions
  _shareSettingsWithMainProcess() {
    ipcRenderer.send('settings', this.all);
  }
}