aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/RequestStore.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/RequestStore.ts')
-rw-r--r--src/stores/RequestStore.ts78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/stores/RequestStore.ts b/src/stores/RequestStore.ts
new file mode 100644
index 000000000..03ad2c7db
--- /dev/null
+++ b/src/stores/RequestStore.ts
@@ -0,0 +1,78 @@
1import { ipcRenderer } from 'electron';
2import { action, computed, observable } from 'mobx';
3import ms from 'ms';
4
5import { Actions } from 'src/actions/lib/actions';
6import { ApiInterface } from 'src/api';
7import { Stores } from 'src/stores.types';
8import CachedRequest from './lib/CachedRequest';
9import { LOCAL_PORT } from '../config';
10
11import TypedStore from './lib/TypedStore';
12
13const debug = require('../preload-safe-debug')('Ferdium:RequestsStore');
14
15export default class RequestStore extends TypedStore {
16 @observable userInfoRequest: CachedRequest;
17
18 @observable servicesRequest: CachedRequest;
19
20 @observable showRequiredRequestsError = false;
21
22 @observable localServerPort = LOCAL_PORT;
23
24 retries: number = 0;
25
26 retryDelay: number = ms('2s');
27
28 constructor(stores: Stores, api: ApiInterface, actions: Actions) {
29 super(stores, api, actions);
30
31 this.actions.requests.retryRequiredRequests.listen(
32 this._retryRequiredRequests.bind(this),
33 );
34
35 this.registerReactions([this._autoRetry.bind(this)]);
36 }
37
38 async setup(): Promise<void> {
39 this.userInfoRequest = this.stores.user.getUserInfoRequest;
40 this.servicesRequest = this.stores.services.allServicesRequest;
41
42 ipcRenderer.on('localServerPort', (_, data) => {
43 if (data.port) {
44 this.localServerPort = data.port;
45 }
46 });
47 }
48
49 @computed get areRequiredRequestsSuccessful(): boolean {
50 return !this.userInfoRequest.isError && !this.servicesRequest.isError;
51 }
52
53 @computed get areRequiredRequestsLoading(): boolean {
54 return this.userInfoRequest.isExecuting || this.servicesRequest.isExecuting;
55 }
56
57 @action _retryRequiredRequests(): void {
58 this.userInfoRequest.reload();
59 this.servicesRequest.reload();
60 }
61
62 // Reactions
63 _autoRetry(): void {
64 const delay = (this.retries <= 10 ? this.retries : 10) * this.retryDelay;
65 if (!this.areRequiredRequestsSuccessful && this.stores.user.isLoggedIn) {
66 setTimeout(() => {
67 this.retries += 1;
68 this._retryRequiredRequests();
69 if (this.retries === 4) {
70 this.showRequiredRequestsError = true;
71 }
72
73 this._autoRetry();
74 debug(`Retry required requests delayed in ${delay / 1000}s`);
75 }, delay);
76 }
77 }
78}