diff options
author | Stefan Malzner <stefan@adlk.io> | 2018-01-17 10:34:22 +0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-17 10:34:22 +0400 |
commit | 81b49fba430959ec4e0946f905dc182d2733831c (patch) | |
tree | a1979dafda41ac804986ef57a68912a868cb5ac7 /src/stores/AppStore.js | |
parent | Remove idle timer dependency (diff) | |
parent | Merge branch 'develop' of github.com:meetfranz/franz into develop (diff) | |
download | ferdium-app-81b49fba430959ec4e0946f905dc182d2733831c.tar.gz ferdium-app-81b49fba430959ec4e0946f905dc182d2733831c.tar.zst ferdium-app-81b49fba430959ec4e0946f905dc182d2733831c.zip |
Merge branch 'develop' into feature/remove-miner
Diffstat (limited to 'src/stores/AppStore.js')
-rw-r--r-- | src/stores/AppStore.js | 57 |
1 files changed, 52 insertions, 5 deletions
diff --git a/src/stores/AppStore.js b/src/stores/AppStore.js index 3eddff5ed..772b33c67 100644 --- a/src/stores/AppStore.js +++ b/src/stores/AppStore.js | |||
@@ -1,9 +1,10 @@ | |||
1 | import { remote, ipcRenderer, shell } from 'electron'; | 1 | import { remote, ipcRenderer, shell } from 'electron'; |
2 | import { action, observable } from 'mobx'; | 2 | import { action, computed, observable } from 'mobx'; |
3 | import moment from 'moment'; | 3 | import moment from 'moment'; |
4 | import key from 'keymaster'; | 4 | import key from 'keymaster'; |
5 | import { getDoNotDisturb } from '@meetfranz/electron-notification-state'; | 5 | import { getDoNotDisturb } from '@meetfranz/electron-notification-state'; |
6 | import AutoLaunch from 'auto-launch'; | 6 | import AutoLaunch from 'auto-launch'; |
7 | import prettyBytes from 'pretty-bytes'; | ||
7 | 8 | ||
8 | import Store from './lib/Store'; | 9 | import Store from './lib/Store'; |
9 | import Request from './lib/Request'; | 10 | import Request from './lib/Request'; |
@@ -12,7 +13,10 @@ import { isMac } from '../environment'; | |||
12 | import locales from '../i18n/translations'; | 13 | import locales from '../i18n/translations'; |
13 | import { gaEvent } from '../lib/analytics'; | 14 | import { gaEvent } from '../lib/analytics'; |
14 | 15 | ||
15 | const { app, powerMonitor } = remote; | 16 | import { getServiceIdsFromPartitions, removeServicePartitionDirectory } from '../helpers/service-helpers.js'; |
17 | |||
18 | const { app } = remote; | ||
19 | |||
16 | const defaultLocale = DEFAULT_APP_SETTINGS.locale; | 20 | const defaultLocale = DEFAULT_APP_SETTINGS.locale; |
17 | const autoLauncher = new AutoLaunch({ | 21 | const autoLauncher = new AutoLaunch({ |
18 | name: 'Franz', | 22 | name: 'Franz', |
@@ -28,6 +32,8 @@ export default class AppStore extends Store { | |||
28 | }; | 32 | }; |
29 | 33 | ||
30 | @observable healthCheckRequest = new Request(this.api.app, 'health'); | 34 | @observable healthCheckRequest = new Request(this.api.app, 'health'); |
35 | @observable getAppCacheSizeRequest = new Request(this.api.local, 'getAppCacheSize'); | ||
36 | @observable clearAppCacheRequest = new Request(this.api.local, 'clearAppCache'); | ||
31 | 37 | ||
32 | @observable autoLaunchOnStart = true; | 38 | @observable autoLaunchOnStart = true; |
33 | 39 | ||
@@ -40,6 +46,8 @@ export default class AppStore extends Store { | |||
40 | 46 | ||
41 | @observable isSystemMuteOverridden = false; | 47 | @observable isSystemMuteOverridden = false; |
42 | 48 | ||
49 | @observable isClearingAllCache = false; | ||
50 | |||
43 | constructor(...args) { | 51 | constructor(...args) { |
44 | super(...args); | 52 | super(...args); |
45 | 53 | ||
@@ -54,6 +62,7 @@ export default class AppStore extends Store { | |||
54 | this.actions.app.healthCheck.listen(this._healthCheck.bind(this)); | 62 | this.actions.app.healthCheck.listen(this._healthCheck.bind(this)); |
55 | this.actions.app.muteApp.listen(this._muteApp.bind(this)); | 63 | this.actions.app.muteApp.listen(this._muteApp.bind(this)); |
56 | this.actions.app.toggleMuteApp.listen(this._toggleMuteApp.bind(this)); | 64 | this.actions.app.toggleMuteApp.listen(this._toggleMuteApp.bind(this)); |
65 | this.actions.app.clearAllCache.listen(this._clearAllCache.bind(this)); | ||
57 | 66 | ||
58 | this.registerReactions([ | 67 | this.registerReactions([ |
59 | this._offlineCheck.bind(this), | 68 | this._offlineCheck.bind(this), |
@@ -116,9 +125,16 @@ export default class AppStore extends Store { | |||
116 | }); | 125 | }); |
117 | 126 | ||
118 | // Reload all services after a healthy nap | 127 | // Reload all services after a healthy nap |
119 | powerMonitor.on('resume', () => { | 128 | // Alternative solution for powerMonitor as the resume event is not fired |
120 | setTimeout(window.location.reload, 5000); | 129 | // More information: https://github.com/electron/electron/issues/1615 |
121 | }); | 130 | let lastTime = (new Date()).getTime(); |
131 | setInterval(() => { | ||
132 | const currentTime = (new Date()).getTime(); | ||
133 | if (currentTime > (lastTime + TIMEOUT + 2000)) { | ||
134 | this._reactivateServices(); | ||
135 | } | ||
136 | lastTime = currentTime; | ||
137 | }, TIMEOUT); | ||
122 | 138 | ||
123 | // Set active the next service | 139 | // Set active the next service |
124 | key( | 140 | key( |
@@ -143,6 +159,10 @@ export default class AppStore extends Store { | |||
143 | this._healthCheck(); | 159 | this._healthCheck(); |
144 | } | 160 | } |
145 | 161 | ||
162 | @computed get cacheSize() { | ||
163 | return prettyBytes(this.getAppCacheSizeRequest.execute().result || 0); | ||
164 | } | ||
165 | |||
146 | // Actions | 166 | // Actions |
147 | @action _notify({ title, options, notificationId, serviceId = null }) { | 167 | @action _notify({ title, options, notificationId, serviceId = null }) { |
148 | if (this.stores.settings.all.isAppMuted) return; | 168 | if (this.stores.settings.all.isAppMuted) return; |
@@ -233,6 +253,23 @@ export default class AppStore extends Store { | |||
233 | this._muteApp({ isMuted: !this.stores.settings.all.isAppMuted }); | 253 | this._muteApp({ isMuted: !this.stores.settings.all.isAppMuted }); |
234 | } | 254 | } |
235 | 255 | ||
256 | @action async _clearAllCache() { | ||
257 | this.isClearingAllCache = true; | ||
258 | const clearAppCache = this.clearAppCacheRequest.execute(); | ||
259 | const allServiceIds = await getServiceIdsFromPartitions(); | ||
260 | const allOrphanedServiceIds = allServiceIds.filter(id => !this.stores.services.all.find(s => id.replace('service-', '') === s.id)); | ||
261 | |||
262 | await Promise.all(allOrphanedServiceIds.map(id => removeServicePartitionDirectory(id))); | ||
263 | |||
264 | await Promise.all(this.stores.services.all.map(s => this.actions.service.clearCache({ serviceId: s.id }))); | ||
265 | |||
266 | await clearAppCache._promise; | ||
267 | |||
268 | this.getAppCacheSizeRequest.execute(); | ||
269 | |||
270 | this.isClearingAllCache = false; | ||
271 | } | ||
272 | |||
236 | // Reactions | 273 | // Reactions |
237 | _offlineCheck() { | 274 | _offlineCheck() { |
238 | if (!this.isOnline) { | 275 | if (!this.isOnline) { |
@@ -321,6 +358,16 @@ export default class AppStore extends Store { | |||
321 | return autoLauncher.isEnabled() || false; | 358 | return autoLauncher.isEnabled() || false; |
322 | } | 359 | } |
323 | 360 | ||
361 | _reactivateServices(retryCount = 0) { | ||
362 | if (!this.isOnline) { | ||
363 | console.debug('reactivateServices: computer is offline, trying again in 5s, retries:', retryCount); | ||
364 | setTimeout(() => this._reactivateServices(retryCount + 1), 5000); | ||
365 | } else { | ||
366 | console.debug('reactivateServices: reload all services'); | ||
367 | this.actions.service.reloadAll(); | ||
368 | } | ||
369 | } | ||
370 | |||
324 | _systemDND() { | 371 | _systemDND() { |
325 | const dnd = getDoNotDisturb(); | 372 | const dnd = getDoNotDisturb(); |
326 | if (dnd === this.stores.settings.all.isAppMuted || !this.isSystemMuteOverriden) { | 373 | if (dnd === this.stores.settings.all.isAppMuted || !this.isSystemMuteOverriden) { |