diff options
Diffstat (limited to 'src/stores/ServicesStore.js')
-rw-r--r-- | src/stores/ServicesStore.js | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.js index 19e6f8299..80c7d7e81 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.js | |||
@@ -5,7 +5,7 @@ import { | |||
5 | computed, | 5 | computed, |
6 | observable, | 6 | observable, |
7 | } from 'mobx'; | 7 | } from 'mobx'; |
8 | import { remove } from 'lodash'; | 8 | import { debounce, remove } from 'lodash'; |
9 | import ms from 'ms'; | 9 | import ms from 'ms'; |
10 | import fs from 'fs-extra'; | 10 | import fs from 'fs-extra'; |
11 | import path from 'path'; | 11 | import path from 'path'; |
@@ -127,6 +127,60 @@ export default class ServicesStore extends Store { | |||
127 | ); | 127 | ); |
128 | } | 128 | } |
129 | 129 | ||
130 | initialize() { | ||
131 | super.initialize(); | ||
132 | |||
133 | // Check services to become hibernated | ||
134 | this.serviceMaintenanceTick(); | ||
135 | } | ||
136 | |||
137 | teardown() { | ||
138 | super.teardown(); | ||
139 | |||
140 | // Stop checking services for hibernation | ||
141 | this.serviceMaintenanceTick.cancel(); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Сheck for services to become hibernated. | ||
146 | */ | ||
147 | serviceMaintenanceTick = debounce(() => { | ||
148 | this._serviceMaintenance(); | ||
149 | this.serviceMaintenanceTick(); | ||
150 | debug('Service maintenance tick'); | ||
151 | }, ms('10s')); | ||
152 | |||
153 | /** | ||
154 | * Run various maintenance tasks on services | ||
155 | */ | ||
156 | _serviceMaintenance() { | ||
157 | this.all.forEach((service) => { | ||
158 | // Defines which services should be hibernated. | ||
159 | if (!service.isActive && (Date.now() - service.lastUsed > ms('5m'))) { | ||
160 | // If service is stale for 5 min, hibernate it. | ||
161 | this._hibernate({ serviceId: service.id }); | ||
162 | } | ||
163 | |||
164 | if (service.lastPoll && (service.lastPoll) - service.lastPollAnswer > ms('30s')) { | ||
165 | // If service did not reply for more than 30s try to reload. | ||
166 | if (!service.isActive) { | ||
167 | if (this.stores.app.isOnline && service.lostRecipeReloadAttempt < 3) { | ||
168 | service.webview.reload(); | ||
169 | service.lostRecipeReloadAttempt += 1; | ||
170 | |||
171 | service.lostRecipeConnection = false; | ||
172 | } | ||
173 | } else { | ||
174 | service.lostRecipeConnection = true; | ||
175 | } | ||
176 | } else { | ||
177 | service.lostRecipeConnection = false; | ||
178 | service.lostRecipeReloadAttempt = 0; | ||
179 | } | ||
180 | }); | ||
181 | } | ||
182 | |||
183 | // Computed props | ||
130 | @computed get all() { | 184 | @computed get all() { |
131 | if (this.stores.user.isLoggedIn) { | 185 | if (this.stores.user.isLoggedIn) { |
132 | const services = this.allServicesRequest.execute().result; | 186 | const services = this.allServicesRequest.execute().result; |
@@ -379,6 +433,7 @@ export default class ServicesStore extends Store { | |||
379 | this.all[index].isActive = false; | 433 | this.all[index].isActive = false; |
380 | }); | 434 | }); |
381 | service.isActive = true; | 435 | service.isActive = true; |
436 | service.lastUsed = Date.now(); | ||
382 | 437 | ||
383 | // Update list of last used services | 438 | // Update list of last used services |
384 | this.lastUsedServices = this.lastUsedServices.filter(id => id !== serviceId); | 439 | this.lastUsedServices = this.lastUsedServices.filter(id => id !== serviceId); |
@@ -475,10 +530,16 @@ export default class ServicesStore extends Store { | |||
475 | const service = this.one(serviceId); | 530 | const service = this.one(serviceId); |
476 | 531 | ||
477 | if (channel === 'hello') { | 532 | if (channel === 'hello') { |
533 | debug('Received hello event from', serviceId); | ||
534 | |||
478 | this._initRecipePolling(service.id); | 535 | this._initRecipePolling(service.id); |
479 | this._initializeServiceRecipeInWebview(serviceId); | 536 | this._initializeServiceRecipeInWebview(serviceId); |
480 | this._shareSettingsWithServiceProcess(); | 537 | this._shareSettingsWithServiceProcess(); |
538 | } else if (channel === 'alive') { | ||
539 | service.lastPollAnswer = Date.now(); | ||
481 | } else if (channel === 'messages') { | 540 | } else if (channel === 'messages') { |
541 | debug(`Received unread message info from '${serviceId}'`, args[0]); | ||
542 | |||
482 | this.actions.service.setUnreadMessageCount({ | 543 | this.actions.service.setUnreadMessageCount({ |
483 | serviceId, | 544 | serviceId, |
484 | count: { | 545 | count: { |
@@ -600,6 +661,7 @@ export default class ServicesStore extends Store { | |||
600 | if (!service.isEnabled) return; | 661 | if (!service.isEnabled) return; |
601 | 662 | ||
602 | service.resetMessageCount(); | 663 | service.resetMessageCount(); |
664 | service.lostRecipeConnection = false; | ||
603 | 665 | ||
604 | // service.webview.loadURL(service.url); | 666 | // service.webview.loadURL(service.url); |
605 | service.webview.reload(); | 667 | service.webview.reload(); |
@@ -777,7 +839,7 @@ export default class ServicesStore extends Store { | |||
777 | const isMuted = isAppMuted || service.isMuted; | 839 | const isMuted = isAppMuted || service.isMuted; |
778 | 840 | ||
779 | if (isAttached) { | 841 | if (isAttached) { |
780 | service.webview.setAudioMuted(isMuted); | 842 | service.webview.audioMuted = isMuted; |
781 | } | 843 | } |
782 | }); | 844 | }); |
783 | } | 845 | } |
@@ -863,6 +925,7 @@ export default class ServicesStore extends Store { | |||
863 | service.webview.send('poll'); | 925 | service.webview.send('poll'); |
864 | 926 | ||
865 | service.timer = setTimeout(loop, delay); | 927 | service.timer = setTimeout(loop, delay); |
928 | service.lastPoll = Date.now(); | ||
866 | }; | 929 | }; |
867 | 930 | ||
868 | loop(); | 931 | loop(); |