diff options
author | Ricardo Cino <ricardo@cino.io> | 2022-06-22 22:10:39 +0200 |
---|---|---|
committer | Vijay Aravamudhan <vraravam@users.noreply.github.com> | 2022-06-23 06:55:42 +0530 |
commit | 5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34 (patch) | |
tree | 81f643c38eecdfa49476e0b9dac3aed9107ecf99 /src/stores/RequestStore.ts | |
parent | Upgrade 'electron' to '19.0.6' (diff) | |
download | ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.gz ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.tar.zst ferdium-app-5dcc1aff28290fe3b7e2ed5cbe99d3034c0b6f34.zip |
chore: recipes/request stores js => ts
Diffstat (limited to 'src/stores/RequestStore.ts')
-rw-r--r-- | src/stores/RequestStore.ts | 78 |
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 @@ | |||
1 | import { ipcRenderer } from 'electron'; | ||
2 | import { action, computed, observable } from 'mobx'; | ||
3 | import ms from 'ms'; | ||
4 | |||
5 | import { Actions } from 'src/actions/lib/actions'; | ||
6 | import { ApiInterface } from 'src/api'; | ||
7 | import { Stores } from 'src/stores.types'; | ||
8 | import CachedRequest from './lib/CachedRequest'; | ||
9 | import { LOCAL_PORT } from '../config'; | ||
10 | |||
11 | import TypedStore from './lib/TypedStore'; | ||
12 | |||
13 | const debug = require('../preload-safe-debug')('Ferdium:RequestsStore'); | ||
14 | |||
15 | export 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 | } | ||