diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-09-14 11:03:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 11:03:28 +0200 |
commit | 087113d8a1214ba4c7df03bfe66747d8d944280c (patch) | |
tree | 4d853a03057138dfa845cd6a7d91ccf63565a1a6 /src/electron/Settings.js | |
parent | chore: codebase improvements (#1930) (diff) | |
download | ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.gz ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.zst ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.zip |
chore: convert JS to TS (#1934)
Diffstat (limited to 'src/electron/Settings.js')
-rw-r--r-- | src/electron/Settings.js | 61 |
1 files changed, 0 insertions, 61 deletions
diff --git a/src/electron/Settings.js b/src/electron/Settings.js deleted file mode 100644 index 3e11bb175..000000000 --- a/src/electron/Settings.js +++ /dev/null | |||
@@ -1,61 +0,0 @@ | |||
1 | import { observable, toJS } from 'mobx'; | ||
2 | import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; | ||
3 | import { userDataPath } from '../environment'; | ||
4 | |||
5 | const debug = require('debug')('Ferdi:Settings'); | ||
6 | |||
7 | export default class Settings { | ||
8 | type = ''; | ||
9 | |||
10 | @observable store = {}; | ||
11 | |||
12 | constructor(type, defaultState = {}) { | ||
13 | this.type = type; | ||
14 | this.store = defaultState; | ||
15 | this.defaultState = defaultState; | ||
16 | |||
17 | if (!pathExistsSync(this.settingsFile)) { | ||
18 | this._writeFile(); | ||
19 | } else { | ||
20 | this._hydrate(); | ||
21 | } | ||
22 | } | ||
23 | |||
24 | set(settings) { | ||
25 | this.store = this._merge(settings); | ||
26 | |||
27 | this._writeFile(); | ||
28 | } | ||
29 | |||
30 | get all() { | ||
31 | return this.store; | ||
32 | } | ||
33 | |||
34 | get allSerialized() { | ||
35 | return toJS(this.store); | ||
36 | } | ||
37 | |||
38 | get(key) { | ||
39 | return this.store[key]; | ||
40 | } | ||
41 | |||
42 | _merge(settings) { | ||
43 | return Object.assign(this.defaultState, this.store, settings); | ||
44 | } | ||
45 | |||
46 | _hydrate() { | ||
47 | this.store = this._merge(readJsonSync(this.settingsFile)); | ||
48 | debug('Hydrate store', this.type, this.allSerialized); | ||
49 | } | ||
50 | |||
51 | _writeFile() { | ||
52 | outputJsonSync(this.settingsFile, this.store, { | ||
53 | spaces: 2, | ||
54 | }); | ||
55 | debug('Write settings file', this.type, this.allSerialized); | ||
56 | } | ||
57 | |||
58 | get settingsFile() { | ||
59 | return userDataPath('config', `${this.type === 'app' ? 'settings' : this.type}.json`); | ||
60 | } | ||
61 | } | ||