diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-09-14 19:58:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 19:58:52 +0200 |
commit | 95df3522a15631abc51a4295cae0ea401a8d4e1e (patch) | |
tree | e5eb0f368c947683f01458e912f21756fb0d99cb /src/stores/lib | |
parent | docs: add sad270 as a contributor for bug, userTesting [skip ci] (#1941) (diff) | |
download | ferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.tar.gz ferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.tar.zst ferdium-app-95df3522a15631abc51a4295cae0ea401a8d4e1e.zip |
feat: add eslint-plugin-unicorn (#1936)
Diffstat (limited to 'src/stores/lib')
-rw-r--r-- | src/stores/lib/CachedRequest.js | 96 | ||||
-rw-r--r-- | src/stores/lib/Request.js | 2 | ||||
-rw-r--r-- | src/stores/lib/Store.js | 6 |
3 files changed, 59 insertions, 45 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 | } |
diff --git a/src/stores/lib/Request.js b/src/stores/lib/Request.js index 32ffe4367..39f32729a 100644 --- a/src/stores/lib/Request.js +++ b/src/stores/lib/Request.js | |||
@@ -107,7 +107,7 @@ export default class Request { | |||
107 | } | 107 | } |
108 | 108 | ||
109 | _triggerHooks() { | 109 | _triggerHooks() { |
110 | Request._hooks.forEach((hook) => hook(this)); | 110 | for (const hook of Request._hooks) hook(this); |
111 | } | 111 | } |
112 | 112 | ||
113 | reset = () => { | 113 | reset = () => { |
diff --git a/src/stores/lib/Store.js b/src/stores/lib/Store.js index b03a7e725..b39070ce8 100644 --- a/src/stores/lib/Store.js +++ b/src/stores/lib/Store.js | |||
@@ -28,18 +28,18 @@ export default class Store { | |||
28 | } | 28 | } |
29 | 29 | ||
30 | registerReactions(reactions) { | 30 | registerReactions(reactions) { |
31 | reactions.forEach((reaction) => this._reactions.push(new Reaction(reaction))); | 31 | for (const reaction of reactions) this._reactions.push(new Reaction(reaction)); |
32 | } | 32 | } |
33 | 33 | ||
34 | setup() {} | 34 | setup() {} |
35 | 35 | ||
36 | initialize() { | 36 | initialize() { |
37 | this.setup(); | 37 | this.setup(); |
38 | this._reactions.forEach((reaction) => reaction.start()); | 38 | for (const reaction of this._reactions) reaction.start(); |
39 | } | 39 | } |
40 | 40 | ||
41 | teardown() { | 41 | teardown() { |
42 | this._reactions.forEach((reaction) => reaction.stop()); | 42 | for (const reaction of this._reactions) reaction.stop(); |
43 | } | 43 | } |
44 | 44 | ||
45 | resetStatus() { | 45 | resetStatus() { |