diff options
Diffstat (limited to 'src/stores/lib/CachedRequest.js')
-rw-r--r-- | src/stores/lib/CachedRequest.js | 121 |
1 files changed, 0 insertions, 121 deletions
diff --git a/src/stores/lib/CachedRequest.js b/src/stores/lib/CachedRequest.js deleted file mode 100644 index a6dd47f7d..000000000 --- a/src/stores/lib/CachedRequest.js +++ /dev/null | |||
@@ -1,121 +0,0 @@ | |||
1 | import { action } from 'mobx'; | ||
2 | import { isEqual, remove } from 'lodash'; | ||
3 | import Request from './Request'; | ||
4 | |||
5 | export default class CachedRequest extends Request { | ||
6 | _apiCalls = []; | ||
7 | |||
8 | _isInvalidated = true; | ||
9 | |||
10 | execute(...callArgs) { | ||
11 | // Do not continue if this request is already loading | ||
12 | if (this._isWaitingForResponse) return this; | ||
13 | |||
14 | // Very simple caching strategy -> only continue if the call / args changed | ||
15 | // or the request was invalidated manually from outside | ||
16 | const existingApiCall = this._findApiCall(callArgs); | ||
17 | |||
18 | // Invalidate if new or different api call will be done | ||
19 | if (existingApiCall && existingApiCall !== this._currentApiCall) { | ||
20 | this._isInvalidated = true; | ||
21 | this._currentApiCall = existingApiCall; | ||
22 | } else if (!existingApiCall) { | ||
23 | this._isInvalidated = true; | ||
24 | this._currentApiCall = this._addApiCall(callArgs); | ||
25 | } | ||
26 | |||
27 | // Do not continue if this request is not invalidated (see above) | ||
28 | if (!this._isInvalidated) return this; | ||
29 | |||
30 | // This timeout is necessary to avoid warnings from mobx | ||
31 | // regarding triggering actions as side-effect of getters | ||
32 | setTimeout( | ||
33 | action(() => { | ||
34 | this.isExecuting = true; | ||
35 | // Apply the previous result from this call immediately (cached) | ||
36 | if (existingApiCall) { | ||
37 | this.result = existingApiCall.result; | ||
38 | } | ||
39 | }), | ||
40 | 0, | ||
41 | ); | ||
42 | |||
43 | // Issue api call & save it as promise that is handled to update the results of the operation | ||
44 | this._promise = new Promise(resolve => { | ||
45 | this._api[this._method](...callArgs) | ||
46 | .then(result => { | ||
47 | setTimeout( | ||
48 | action(() => { | ||
49 | this.result = result; | ||
50 | if (this._currentApiCall) this._currentApiCall.result = result; | ||
51 | this.isExecuting = false; | ||
52 | this.isError = false; | ||
53 | this.wasExecuted = true; | ||
54 | this._isInvalidated = false; | ||
55 | this._isWaitingForResponse = false; | ||
56 | this._triggerHooks(); | ||
57 | resolve(result); | ||
58 | }), | ||
59 | 1, | ||
60 | ); | ||
61 | return result; | ||
62 | }) | ||
63 | .catch( | ||
64 | action(error => { | ||
65 | setTimeout( | ||
66 | action(() => { | ||
67 | this.error = error; | ||
68 | this.isExecuting = false; | ||
69 | this.isError = true; | ||
70 | this.wasExecuted = true; | ||
71 | this._isWaitingForResponse = false; | ||
72 | this._triggerHooks(); | ||
73 | // reject(error); | ||
74 | }), | ||
75 | 1, | ||
76 | ); | ||
77 | }), | ||
78 | ); | ||
79 | }); | ||
80 | |||
81 | this._isWaitingForResponse = true; | ||
82 | return this; | ||
83 | } | ||
84 | |||
85 | // eslint-disable-next-line unicorn/no-object-as-default-parameter | ||
86 | invalidate(options = { immediately: false }) { | ||
87 | this._isInvalidated = true; | ||
88 | if (options.immediately && this._currentApiCall) { | ||
89 | return this.execute(...this._currentApiCall.args); | ||
90 | } | ||
91 | return this; | ||
92 | } | ||
93 | |||
94 | patch(modify) { | ||
95 | return new Promise(resolve => { | ||
96 | setTimeout( | ||
97 | action(() => { | ||
98 | const override = modify(this.result); | ||
99 | if (override !== undefined) this.result = override; | ||
100 | if (this._currentApiCall) this._currentApiCall.result = this.result; | ||
101 | resolve(this); | ||
102 | }), | ||
103 | 0, | ||
104 | ); | ||
105 | }); | ||
106 | } | ||
107 | |||
108 | removeCacheForCallWith(...args) { | ||
109 | remove(this._apiCalls, c => isEqual(c.args, args)); | ||
110 | } | ||
111 | |||
112 | _addApiCall(args) { | ||
113 | const newCall = { args, result: null }; | ||
114 | this._apiCalls.push(newCall); | ||
115 | return newCall; | ||
116 | } | ||
117 | |||
118 | _findApiCall(args) { | ||
119 | return this._apiCalls.find(c => isEqual(c.args, args)); | ||
120 | } | ||
121 | } | ||