diff options
author | 2022-11-17 05:45:39 +0530 | |
---|---|---|
committer | 2022-11-17 00:15:39 +0000 | |
commit | d9502c7516bc2d4ae467c6ea8a2e4816b0885f37 (patch) | |
tree | b339c587a5529ac26d52cfc12d9972a8a00255e6 /src/stores/lib/Request.js | |
parent | Transform JSX components to TSX (#755) (diff) | |
download | ferdium-app-d9502c7516bc2d4ae467c6ea8a2e4816b0885f37.tar.gz ferdium-app-d9502c7516bc2d4ae467c6ea8a2e4816b0885f37.tar.zst ferdium-app-d9502c7516bc2d4ae467c6ea8a2e4816b0885f37.zip |
Transfrom workspace components to ts (#775)
Diffstat (limited to 'src/stores/lib/Request.js')
-rw-r--r-- | src/stores/lib/Request.js | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/src/stores/lib/Request.js b/src/stores/lib/Request.js deleted file mode 100644 index 60c943a42..000000000 --- a/src/stores/lib/Request.js +++ /dev/null | |||
@@ -1,155 +0,0 @@ | |||
1 | import { observable, action, computed, makeObservable } from 'mobx'; | ||
2 | import { isEqual } from 'lodash/fp'; | ||
3 | |||
4 | export default class Request { | ||
5 | static _hooks = []; | ||
6 | |||
7 | static registerHook(hook) { | ||
8 | Request._hooks.push(hook); | ||
9 | } | ||
10 | |||
11 | @observable result = null; | ||
12 | |||
13 | @observable error = null; | ||
14 | |||
15 | @observable isExecuting = false; | ||
16 | |||
17 | @observable isError = false; | ||
18 | |||
19 | @observable wasExecuted = false; | ||
20 | |||
21 | @action _reset() { | ||
22 | this.error = null; | ||
23 | this.result = null; | ||
24 | this.isExecuting = false; | ||
25 | this.isError = false; | ||
26 | this.wasExecuted = false; | ||
27 | this._isWaitingForResponse = false; | ||
28 | this._promise = Promise; | ||
29 | |||
30 | return this; | ||
31 | } | ||
32 | |||
33 | _promise = Promise; | ||
34 | |||
35 | _api = {}; | ||
36 | |||
37 | _method = ''; | ||
38 | |||
39 | _isWaitingForResponse = false; | ||
40 | |||
41 | _currentApiCall = null; | ||
42 | |||
43 | constructor(api, method) { | ||
44 | makeObservable(this); | ||
45 | |||
46 | this._api = api; | ||
47 | this._method = method; | ||
48 | } | ||
49 | |||
50 | execute(...callArgs) { | ||
51 | // Do not continue if this request is already loading | ||
52 | if (this._isWaitingForResponse) return this; | ||
53 | |||
54 | if (!this._api[this._method]) { | ||
55 | throw new Error( | ||
56 | `Missing method <${this._method}> on api object:`, | ||
57 | this._api, | ||
58 | ); | ||
59 | } | ||
60 | |||
61 | // This timeout is necessary to avoid warnings from mobx | ||
62 | // regarding triggering actions as side-effect of getters | ||
63 | setTimeout( | ||
64 | action(() => { | ||
65 | this.isExecuting = true; | ||
66 | }), | ||
67 | 0, | ||
68 | ); | ||
69 | |||
70 | // Issue api call & save it as promise that is handled to update the results of the operation | ||
71 | this._promise = new Promise((resolve, reject) => { | ||
72 | this._api[this._method](...callArgs) | ||
73 | .then(result => { | ||
74 | setTimeout( | ||
75 | action(() => { | ||
76 | this.error = null; | ||
77 | this.result = result; | ||
78 | if (this._currentApiCall) this._currentApiCall.result = result; | ||
79 | this.isExecuting = false; | ||
80 | this.isError = false; | ||
81 | this.wasExecuted = true; | ||
82 | this._isWaitingForResponse = false; | ||
83 | this._triggerHooks(); | ||
84 | resolve(result); | ||
85 | }), | ||
86 | 1, | ||
87 | ); | ||
88 | return result; | ||
89 | }) | ||
90 | .catch( | ||
91 | action(error => { | ||
92 | setTimeout( | ||
93 | action(() => { | ||
94 | this.error = error; | ||
95 | this.isExecuting = false; | ||
96 | this.isError = true; | ||
97 | this.wasExecuted = true; | ||
98 | this._isWaitingForResponse = false; | ||
99 | this._triggerHooks(); | ||
100 | reject(error); | ||
101 | }), | ||
102 | 1, | ||
103 | ); | ||
104 | }), | ||
105 | ); | ||
106 | }); | ||
107 | |||
108 | this._isWaitingForResponse = true; | ||
109 | this._currentApiCall = { args: callArgs, result: null }; | ||
110 | return this; | ||
111 | } | ||
112 | |||
113 | reload() { | ||
114 | const args = this._currentApiCall ? this._currentApiCall.args : []; | ||
115 | this.error = null; | ||
116 | return this.execute(...args); | ||
117 | } | ||
118 | |||
119 | retry = () => this.reload(); | ||
120 | |||
121 | isExecutingWithArgs(...args) { | ||
122 | return ( | ||
123 | this.isExecuting && | ||
124 | this._currentApiCall && | ||
125 | isEqual(this._currentApiCall.args, args) | ||
126 | ); | ||
127 | } | ||
128 | |||
129 | @computed get isExecutingFirstTime() { | ||
130 | return !this.wasExecuted && this.isExecuting; | ||
131 | } | ||
132 | |||
133 | /* eslint-disable unicorn/no-thenable */ | ||
134 | then(...args) { | ||
135 | if (!this._promise) | ||
136 | throw new Error( | ||
137 | 'You have to call Request::execute before you can access it as promise', | ||
138 | ); | ||
139 | return this._promise.then(...args); | ||
140 | } | ||
141 | |||
142 | catch(...args) { | ||
143 | if (!this._promise) | ||
144 | throw new Error( | ||
145 | 'You have to call Request::execute before you can access it as promise', | ||
146 | ); | ||
147 | return this._promise.catch(...args); | ||
148 | } | ||
149 | |||
150 | _triggerHooks() { | ||
151 | for (const hook of Request._hooks) hook(this); | ||
152 | } | ||
153 | |||
154 | reset = () => this._reset(); | ||
155 | } | ||