aboutsummaryrefslogtreecommitdiffstats
path: root/src/api/server/LocalApi.js
blob: d2ce0c9deae6945ff491e952e9c11ef6a9610c2b (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
import { remote } from 'electron';

const { session } = remote;

export default class LocalApi {
  // App
  async updateAppSettings(data) {
    const currentSettings = await this.getAppSettings();
    const settings = Object.assign(currentSettings, data);

    localStorage.setItem('app', JSON.stringify(settings));
    console.debug('LocalApi::updateAppSettings resolves', settings);

    return settings;
  }

  async getAppSettings() {
    const settingsString = localStorage.getItem('app');
    try {
      const settings = JSON.parse(settingsString) || {};
      console.debug('LocalApi::getAppSettings resolves', settings);

      return settings;
    } catch (err) {
      return {};
    }
  }

  async removeKey(key) {
    const settings = await this.getAppSettings();

    if (Object.hasOwnProperty.call(settings, key)) {
      delete settings[key];
      localStorage.setItem('app', JSON.stringify(settings));
    }
  }

  // Services
  async clearCache(serviceId) {
    console.debug(`Clearing cache for persist:service-${serviceId}`);
    const s = session.fromPartition(`persist:service-${serviceId}`);
    await new Promise(resolve => s.clearCache(resolve));
  }

  async clearAppCache() {
    const s = session.defaultSession;
    await new Promise(resolve => s.clearCache(resolve));
  }
}