diff options
Diffstat (limited to 'src/electron/Settings.js')
-rw-r--r-- | src/electron/Settings.js | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/src/electron/Settings.js b/src/electron/Settings.js index b3138e948..6ac3b9177 100644 --- a/src/electron/Settings.js +++ b/src/electron/Settings.js | |||
@@ -1,15 +1,21 @@ | |||
1 | import { observable, toJS } from 'mobx'; | 1 | import { observable, toJS } from 'mobx'; |
2 | import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; | 2 | import { pathExistsSync, outputJsonSync, readJsonSync } from 'fs-extra'; |
3 | import path from 'path'; | ||
3 | 4 | ||
4 | import { SETTINGS_PATH, DEFAULT_APP_SETTINGS } from '../config'; | 5 | import { SETTINGS_PATH } from '../config'; |
5 | 6 | ||
6 | const debug = require('debug')('Settings'); | 7 | const debug = require('debug')('Franz:Settings'); |
7 | 8 | ||
8 | export default class Settings { | 9 | export default class Settings { |
9 | @observable store = DEFAULT_APP_SETTINGS; | 10 | type = ''; |
11 | @observable store = {}; | ||
10 | 12 | ||
11 | constructor() { | 13 | constructor(type, defaultState = {}) { |
12 | if (!pathExistsSync(SETTINGS_PATH)) { | 14 | this.type = type; |
15 | this.store = defaultState; | ||
16 | this.defaultState = defaultState; | ||
17 | |||
18 | if (!pathExistsSync(this.settingsFile)) { | ||
13 | this._writeFile(); | 19 | this._writeFile(); |
14 | } else { | 20 | } else { |
15 | this._hydrate(); | 21 | this._hydrate(); |
@@ -17,7 +23,7 @@ export default class Settings { | |||
17 | } | 23 | } |
18 | 24 | ||
19 | set(settings) { | 25 | set(settings) { |
20 | this.store = Object.assign(this.store, settings); | 26 | this.store = this._merge(settings); |
21 | 27 | ||
22 | this._writeFile(); | 28 | this._writeFile(); |
23 | } | 29 | } |
@@ -30,13 +36,21 @@ export default class Settings { | |||
30 | return this.store[key]; | 36 | return this.store[key]; |
31 | } | 37 | } |
32 | 38 | ||
39 | _merge(settings) { | ||
40 | return Object.assign(this.defaultState, this.store, settings); | ||
41 | } | ||
42 | |||
33 | _hydrate() { | 43 | _hydrate() { |
34 | this.store = readJsonSync(SETTINGS_PATH); | 44 | this.store = this._merge(readJsonSync(this.settingsFile)); |
35 | debug('Hydrate store', toJS(this.store)); | 45 | debug('Hydrate store', toJS(this.store)); |
36 | } | 46 | } |
37 | 47 | ||
38 | _writeFile() { | 48 | _writeFile() { |
39 | outputJsonSync(SETTINGS_PATH, this.store); | 49 | outputJsonSync(this.settingsFile, this.store); |
40 | debug('Write settings file', toJS(this.store)); | 50 | debug('Write settings file', toJS(this.store)); |
41 | } | 51 | } |
52 | |||
53 | get settingsFile() { | ||
54 | return path.join(SETTINGS_PATH, `${this.type === 'app' ? 'settings' : this.type}.json`); | ||
55 | } | ||
42 | } | 56 | } |