aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/SettingsStore.js
diff options
context:
space:
mode:
authorLibravatar Amine <amine@mouafik.fr>2020-02-28 02:39:40 +0100
committerLibravatar GitHub <noreply@github.com>2020-02-28 02:39:40 +0100
commit4c084fd3b336dff2ca1d2c326b1e8e98af2b7e29 (patch)
treef91e6e4f4aa344e4788430691e72fd58fa1f2db6 /src/stores/SettingsStore.js
parentMerge pull request #403 from getferdi/feat/#270 (diff)
parentFix lock at startup when previously closed while locked (diff)
downloadferdium-app-4c084fd3b336dff2ca1d2c326b1e8e98af2b7e29.tar.gz
ferdium-app-4c084fd3b336dff2ca1d2c326b1e8e98af2b7e29.tar.zst
ferdium-app-4c084fd3b336dff2ca1d2c326b1e8e98af2b7e29.zip
Merge pull request #393 from getferdi/refactor/lock
Fix race condition in Ferdi Lock at startup
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r--src/stores/SettingsStore.js68
1 files changed, 29 insertions, 39 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js
index 43f00b723..26e83b725 100644
--- a/src/stores/SettingsStore.js
+++ b/src/stores/SettingsStore.js
@@ -1,21 +1,18 @@
1import { ipcRenderer, remote } from 'electron'; 1import { ipcRenderer, remote } from 'electron';
2import { 2import { action, computed, observable, reaction } from 'mobx';
3 action, computed, observable, reaction,
4} from 'mobx';
5import localStorage from 'mobx-localstorage'; 3import localStorage from 'mobx-localstorage';
6
7import Store from './lib/Store';
8import Request from './lib/Request';
9import { getLocale } from '../helpers/i18n-helpers';
10import { API } from '../environment';
11
12import { DEFAULT_APP_SETTINGS, FILE_SYSTEM_SETTINGS_TYPES, LOCAL_SERVER } from '../config'; 4import { DEFAULT_APP_SETTINGS, FILE_SYSTEM_SETTINGS_TYPES, LOCAL_SERVER } from '../config';
5import { API } from '../environment';
6import { getLocale } from '../helpers/i18n-helpers';
13import { SPELLCHECKER_LOCALES } from '../i18n/languages'; 7import { SPELLCHECKER_LOCALES } from '../i18n/languages';
8import Request from './lib/Request';
9import Store from './lib/Store';
14 10
15const debug = require('debug')('Ferdi:SettingsStore'); 11const debug = require('debug')('Ferdi:SettingsStore');
16 12
17export default class SettingsStore extends Store { 13export default class SettingsStore extends Store {
18 @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings'); 14 @observable updateAppSettingsRequest = new Request(this.api.local, 'updateAppSettings');
15 startup = true;
19 16
20 fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES; 17 fileSystemSettingsTypes = FILE_SYSTEM_SETTINGS_TYPES;
21 18
@@ -30,16 +27,6 @@ export default class SettingsStore extends Store {
30 // Register action handlers 27 // Register action handlers
31 this.actions.settings.update.listen(this._update.bind(this)); 28 this.actions.settings.update.listen(this._update.bind(this));
32 this.actions.settings.remove.listen(this._remove.bind(this)); 29 this.actions.settings.remove.listen(this._remove.bind(this));
33
34 ipcRenderer.on('appSettings', (event, resp) => {
35 debug('Get appSettings resolves', resp.type, resp.data);
36
37 Object.assign(this._fileSystemSettingsCache[resp.type], resp.data);
38 });
39
40 this.fileSystemSettingsTypes.forEach((type) => {
41 ipcRenderer.send('getAppSettings', type);
42 });
43 } 30 }
44 31
45 async setup() { 32 async setup() {
@@ -101,27 +88,30 @@ export default class SettingsStore extends Store {
101 } 88 }
102 }); 89 });
103 90
104 // Make sure to lock app on launch if locking feature is enabled 91 ipcRenderer.on('appSettings', (event, resp) => {
105 setTimeout(() => { 92 // Lock on startup if enabled in settings
106 const isLoggedIn = Boolean(localStorage.getItem('authToken')); 93 if (this.startup && resp.type === 'app' && resp.data.lockingFeatureEnabled) {
107 if (isLoggedIn && this.all.app.lockingFeatureEnabled) { 94 this.startup = false;
108 // Disable lock first - otherwise the lock might not get activated corrently 95 process.nextTick(() => {
109 this.actions.settings.update({ 96 // If the app was previously closed unlocked
110 type: 'app', 97 // we can update the `locked` setting and rely on the reaction to lock at startup
111 data: { 98 if (!this.all.app.locked) {
112 locked: false, 99 this.all.app.locked = true;
113 }, 100 } else {
114 }); 101 // Otherwise the app previously closed in a locked state
115 setTimeout(() => { 102 // We can't rely on updating the locked setting for the reaction to be triggered
116 this.actions.settings.update({ 103 // So we lock manually
117 type: 'app', 104 window.ferdi.stores.router.push('/auth/locked');
118 data: { 105 }
119 locked: true, 106 })
120 },
121 });
122 }, 0);
123 } 107 }
124 }, 1000); 108 debug('Get appSettings resolves', resp.type, resp.data);
109 Object.assign(this._fileSystemSettingsCache[resp.type], resp.data);
110 });
111
112 this.fileSystemSettingsTypes.forEach((type) => {
113 ipcRenderer.send('getAppSettings', type);
114 });
125 } 115 }
126 116
127 @computed get app() { 117 @computed get app() {