diff options
Diffstat (limited to 'src/stores/lib/CachedRequest.js')
-rw-r--r-- | src/stores/lib/CachedRequest.js | 96 |
1 files changed, 55 insertions, 41 deletions
diff --git a/src/stores/lib/CachedRequest.js b/src/stores/lib/CachedRequest.js index 94f615144..a6dd47f7d 100644 --- a/src/stores/lib/CachedRequest.js +++ b/src/stores/lib/CachedRequest.js | |||
@@ -1,4 +1,3 @@ | |||
1 | // @flow | ||
2 | import { action } from 'mobx'; | 1 | import { action } from 'mobx'; |
3 | import { isEqual, remove } from 'lodash'; | 2 | import { isEqual, remove } from 'lodash'; |
4 | import Request from './Request'; | 3 | import Request from './Request'; |
@@ -30,48 +29,60 @@ export default class CachedRequest extends Request { | |||
30 | 29 | ||
31 | // This timeout is necessary to avoid warnings from mobx | 30 | // This timeout is necessary to avoid warnings from mobx |
32 | // regarding triggering actions as side-effect of getters | 31 | // regarding triggering actions as side-effect of getters |
33 | setTimeout(action(() => { | 32 | setTimeout( |
34 | this.isExecuting = true; | 33 | action(() => { |
35 | // Apply the previous result from this call immediately (cached) | 34 | this.isExecuting = true; |
36 | if (existingApiCall) { | 35 | // Apply the previous result from this call immediately (cached) |
37 | this.result = existingApiCall.result; | 36 | if (existingApiCall) { |
38 | } | 37 | this.result = existingApiCall.result; |
39 | }), 0); | 38 | } |
39 | }), | ||
40 | 0, | ||
41 | ); | ||
40 | 42 | ||
41 | // Issue api call & save it as promise that is handled to update the results of the operation | 43 | // Issue api call & save it as promise that is handled to update the results of the operation |
42 | this._promise = new Promise((resolve) => { | 44 | this._promise = new Promise(resolve => { |
43 | this._api[this._method](...callArgs) | 45 | this._api[this._method](...callArgs) |
44 | .then((result) => { | 46 | .then(result => { |
45 | setTimeout(action(() => { | 47 | setTimeout( |
46 | this.result = result; | 48 | action(() => { |
47 | if (this._currentApiCall) this._currentApiCall.result = result; | 49 | this.result = result; |
48 | this.isExecuting = false; | 50 | if (this._currentApiCall) this._currentApiCall.result = result; |
49 | this.isError = false; | 51 | this.isExecuting = false; |
50 | this.wasExecuted = true; | 52 | this.isError = false; |
51 | this._isInvalidated = false; | 53 | this.wasExecuted = true; |
52 | this._isWaitingForResponse = false; | 54 | this._isInvalidated = false; |
53 | this._triggerHooks(); | 55 | this._isWaitingForResponse = false; |
54 | resolve(result); | 56 | this._triggerHooks(); |
55 | }), 1); | 57 | resolve(result); |
58 | }), | ||
59 | 1, | ||
60 | ); | ||
56 | return result; | 61 | return result; |
57 | }) | 62 | }) |
58 | .catch(action((error) => { | 63 | .catch( |
59 | setTimeout(action(() => { | 64 | action(error => { |
60 | this.error = error; | 65 | setTimeout( |
61 | this.isExecuting = false; | 66 | action(() => { |
62 | this.isError = true; | 67 | this.error = error; |
63 | this.wasExecuted = true; | 68 | this.isExecuting = false; |
64 | this._isWaitingForResponse = false; | 69 | this.isError = true; |
65 | this._triggerHooks(); | 70 | this.wasExecuted = true; |
66 | // reject(error); | 71 | this._isWaitingForResponse = false; |
67 | }), 1); | 72 | this._triggerHooks(); |
68 | })); | 73 | // reject(error); |
74 | }), | ||
75 | 1, | ||
76 | ); | ||
77 | }), | ||
78 | ); | ||
69 | }); | 79 | }); |
70 | 80 | ||
71 | this._isWaitingForResponse = true; | 81 | this._isWaitingForResponse = true; |
72 | return this; | 82 | return this; |
73 | } | 83 | } |
74 | 84 | ||
85 | // eslint-disable-next-line unicorn/no-object-as-default-parameter | ||
75 | invalidate(options = { immediately: false }) { | 86 | invalidate(options = { immediately: false }) { |
76 | this._isInvalidated = true; | 87 | this._isInvalidated = true; |
77 | if (options.immediately && this._currentApiCall) { | 88 | if (options.immediately && this._currentApiCall) { |
@@ -81,18 +92,21 @@ export default class CachedRequest extends Request { | |||
81 | } | 92 | } |
82 | 93 | ||
83 | patch(modify) { | 94 | patch(modify) { |
84 | return new Promise((resolve) => { | 95 | return new Promise(resolve => { |
85 | setTimeout(action(() => { | 96 | setTimeout( |
86 | const override = modify(this.result); | 97 | action(() => { |
87 | if (override !== undefined) this.result = override; | 98 | const override = modify(this.result); |
88 | if (this._currentApiCall) this._currentApiCall.result = this.result; | 99 | if (override !== undefined) this.result = override; |
89 | resolve(this); | 100 | if (this._currentApiCall) this._currentApiCall.result = this.result; |
90 | }), 0); | 101 | resolve(this); |
102 | }), | ||
103 | 0, | ||
104 | ); | ||
91 | }); | 105 | }); |
92 | } | 106 | } |
93 | 107 | ||
94 | removeCacheForCallWith(...args) { | 108 | removeCacheForCallWith(...args) { |
95 | remove(this._apiCalls, (c) => isEqual(c.args, args)); | 109 | remove(this._apiCalls, c => isEqual(c.args, args)); |
96 | } | 110 | } |
97 | 111 | ||
98 | _addApiCall(args) { | 112 | _addApiCall(args) { |
@@ -102,6 +116,6 @@ export default class CachedRequest extends Request { | |||
102 | } | 116 | } |
103 | 117 | ||
104 | _findApiCall(args) { | 118 | _findApiCall(args) { |
105 | return this._apiCalls.find((c) => isEqual(c.args, args)); | 119 | return this._apiCalls.find(c => isEqual(c.args, args)); |
106 | } | 120 | } |
107 | } | 121 | } |