@data-client/core 0.13.1 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +198 -214
- package/dist/index.umd.min.js +1 -1
- package/legacy/actions.js +1 -1
- package/legacy/controller/Controller.js +21 -30
- package/legacy/controller/actions/createExpireAll.js +8 -0
- package/legacy/controller/actions/createFetch.js +27 -0
- package/legacy/controller/actions/createInvalidate.js +10 -0
- package/legacy/controller/actions/createInvalidateAll.js +8 -0
- package/legacy/controller/actions/createMeta.js +9 -0
- package/legacy/controller/actions/createOptimistic.js +17 -0
- package/legacy/controller/actions/createReset.js +8 -0
- package/legacy/controller/actions/createSet.js +17 -0
- package/legacy/controller/actions/createSetResponse.js +26 -0
- package/legacy/controller/actions/createSubscription.js +22 -0
- package/legacy/controller/actions/index.js +11 -0
- package/legacy/index.js +3 -4
- package/legacy/internal.js +1 -2
- package/legacy/manager/LogoutManager.js +2 -2
- package/legacy/manager/NetworkManager.js +39 -35
- package/legacy/manager/PollingSubscription.js +3 -3
- package/legacy/manager/SubscriptionManager.js +3 -3
- package/legacy/state/reducer/createReducer.js +2 -2
- package/legacy/state/reducer/fetchReducer.js +6 -10
- package/legacy/state/reducer/invalidateReducer.js +2 -2
- package/legacy/state/reducer/setReducer.js +11 -3
- package/legacy/state/reducer/setResponseReducer.js +17 -17
- package/legacy/types.js +1 -1
- package/lib/actions.d.ts +35 -41
- package/lib/actions.d.ts.map +1 -1
- package/lib/actions.js +1 -1
- package/lib/controller/Controller.d.ts +18 -17
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +21 -30
- package/lib/controller/actions/createExpireAll.d.ts +3 -0
- package/lib/controller/actions/createExpireAll.d.ts.map +1 -0
- package/lib/controller/actions/createExpireAll.js +8 -0
- package/lib/controller/{createFetch.d.ts → actions/createFetch.d.ts} +3 -3
- package/lib/controller/actions/createFetch.d.ts.map +1 -0
- package/lib/controller/actions/createFetch.js +27 -0
- package/lib/controller/actions/createInvalidate.d.ts +6 -0
- package/lib/controller/actions/createInvalidate.d.ts.map +1 -0
- package/lib/controller/actions/createInvalidate.js +10 -0
- package/lib/controller/actions/createInvalidateAll.d.ts +3 -0
- package/lib/controller/actions/createInvalidateAll.d.ts.map +1 -0
- package/lib/controller/actions/createInvalidateAll.js +8 -0
- package/lib/controller/actions/createMeta.d.ts +3 -0
- package/lib/controller/actions/createMeta.d.ts.map +1 -0
- package/lib/controller/actions/createMeta.js +9 -0
- package/lib/controller/actions/createOptimistic.d.ts +7 -0
- package/lib/controller/actions/createOptimistic.d.ts.map +1 -0
- package/lib/controller/actions/createOptimistic.js +17 -0
- package/lib/controller/actions/createReset.d.ts +3 -0
- package/lib/controller/actions/createReset.d.ts.map +1 -0
- package/lib/controller/actions/createReset.js +8 -0
- package/lib/controller/actions/createSet.d.ts +8 -0
- package/lib/controller/actions/createSet.d.ts.map +1 -0
- package/lib/controller/actions/createSet.js +17 -0
- package/lib/controller/{createSetResponse.d.ts → actions/createSetResponse.d.ts} +4 -4
- package/lib/controller/actions/createSetResponse.d.ts.map +1 -0
- package/lib/controller/actions/createSetResponse.js +26 -0
- package/lib/controller/{createSubscription.d.ts → actions/createSubscription.d.ts} +1 -1
- package/lib/controller/actions/createSubscription.d.ts.map +1 -0
- package/lib/controller/actions/createSubscription.js +22 -0
- package/lib/controller/actions/index.d.ts +11 -0
- package/lib/controller/actions/index.d.ts.map +1 -0
- package/lib/controller/actions/index.js +11 -0
- package/lib/index.d.ts +1 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -4
- package/lib/internal.d.ts +0 -1
- package/lib/internal.d.ts.map +1 -1
- package/lib/internal.js +1 -2
- package/lib/manager/LogoutManager.js +2 -2
- package/lib/manager/NetworkManager.d.ts +7 -2
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +39 -35
- package/lib/manager/PollingSubscription.js +3 -3
- package/lib/manager/SubscriptionManager.js +3 -3
- package/lib/state/reducer/createReducer.js +2 -2
- package/lib/state/reducer/expireReducer.d.ts +6 -8
- package/lib/state/reducer/expireReducer.d.ts.map +1 -1
- package/lib/state/reducer/fetchReducer.d.ts.map +1 -1
- package/lib/state/reducer/fetchReducer.js +8 -12
- package/lib/state/reducer/invalidateReducer.d.ts +5 -7
- package/lib/state/reducer/invalidateReducer.d.ts.map +1 -1
- package/lib/state/reducer/invalidateReducer.js +2 -2
- package/lib/state/reducer/setReducer.d.ts +2 -1
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +11 -3
- package/lib/state/reducer/setResponseReducer.d.ts.map +1 -1
- package/lib/state/reducer/setResponseReducer.js +17 -17
- package/lib/types.d.ts +3 -3
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +1 -1
- package/package.json +2 -2
- package/src/actions.ts +37 -43
- package/src/controller/Controller.ts +29 -26
- package/src/controller/__tests__/Controller.ts +2 -2
- package/src/controller/actions/createExpireAll.ts +11 -0
- package/src/controller/{createFetch.ts → actions/createFetch.ts} +9 -13
- package/src/controller/actions/createInvalidate.ts +14 -0
- package/src/controller/actions/createInvalidateAll.ts +11 -0
- package/src/controller/actions/createMeta.ts +13 -0
- package/src/controller/actions/createOptimistic.ts +32 -0
- package/src/controller/actions/createReset.ts +9 -0
- package/src/controller/actions/createSet.ts +31 -0
- package/src/controller/{createSetResponse.ts → actions/createSetResponse.ts} +17 -23
- package/src/controller/{createSubscription.ts → actions/createSubscription.ts} +6 -10
- package/src/controller/actions/index.ts +13 -0
- package/src/index.ts +1 -3
- package/src/internal.ts +0 -1
- package/src/manager/LogoutManager.ts +1 -1
- package/src/manager/NetworkManager.ts +41 -32
- package/src/manager/PollingSubscription.ts +2 -2
- package/src/manager/SubscriptionManager.ts +2 -2
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +4 -10
- package/src/manager/__tests__/logoutManager.ts +1 -1
- package/src/manager/__tests__/networkManager.ts +37 -25
- package/src/manager/__tests__/pollingSubscription.ts +3 -3
- package/src/manager/__tests__/subscriptionManager.ts +22 -27
- package/src/state/__tests__/reducer.ts +110 -44
- package/src/state/reducer/createReducer.ts +1 -1
- package/src/state/reducer/fetchReducer.ts +12 -18
- package/src/state/reducer/invalidateReducer.ts +1 -1
- package/src/state/reducer/setReducer.ts +17 -6
- package/src/state/reducer/setResponseReducer.ts +18 -20
- package/src/types.ts +4 -4
- package/ts3.4/actions.d.ts +36 -42
- package/ts3.4/controller/Controller.d.ts +23 -19
- package/ts3.4/controller/actions/createExpireAll.d.ts +3 -0
- package/ts3.4/controller/{createFetch.d.ts → actions/createFetch.d.ts} +3 -3
- package/ts3.4/controller/{createInvalidate.d.ts → actions/createInvalidate.d.ts} +2 -2
- package/ts3.4/controller/actions/createInvalidateAll.d.ts +3 -0
- package/ts3.4/controller/actions/createMeta.d.ts +3 -0
- package/ts3.4/controller/actions/createOptimistic.d.ts +9 -0
- package/ts3.4/controller/actions/createReset.d.ts +3 -0
- package/ts3.4/controller/actions/createSet.d.ts +10 -0
- package/ts3.4/controller/{createSetResponse.d.ts → actions/createSetResponse.d.ts} +4 -4
- package/ts3.4/controller/{createSubscription.d.ts → actions/createSubscription.d.ts} +1 -1
- package/ts3.4/controller/actions/index.d.ts +11 -0
- package/ts3.4/index.d.ts +2 -3
- package/ts3.4/internal.d.ts +0 -1
- package/ts3.4/manager/NetworkManager.d.ts +7 -2
- package/ts3.4/state/reducer/expireReducer.d.ts +6 -8
- package/ts3.4/state/reducer/invalidateReducer.d.ts +5 -7
- package/ts3.4/state/reducer/setReducer.d.ts +2 -1
- package/ts3.4/types.d.ts +3 -3
- package/legacy/controller/createExpireAll.js +0 -8
- package/legacy/controller/createFetch.js +0 -31
- package/legacy/controller/createInvalidate.js +0 -12
- package/legacy/controller/createInvalidateAll.js +0 -8
- package/legacy/controller/createOptimistic.js +0 -27
- package/legacy/controller/createReset.js +0 -8
- package/legacy/controller/createSet.js +0 -28
- package/legacy/controller/createSetResponse.js +0 -32
- package/legacy/controller/createSubscription.js +0 -26
- package/legacy/state/RIC.js +0 -3
- package/lib/controller/createExpireAll.d.ts +0 -3
- package/lib/controller/createExpireAll.d.ts.map +0 -1
- package/lib/controller/createExpireAll.js +0 -8
- package/lib/controller/createFetch.d.ts.map +0 -1
- package/lib/controller/createFetch.js +0 -31
- package/lib/controller/createInvalidate.d.ts +0 -6
- package/lib/controller/createInvalidate.d.ts.map +0 -1
- package/lib/controller/createInvalidate.js +0 -12
- package/lib/controller/createInvalidateAll.d.ts +0 -3
- package/lib/controller/createInvalidateAll.d.ts.map +0 -1
- package/lib/controller/createInvalidateAll.js +0 -8
- package/lib/controller/createOptimistic.d.ts +0 -10
- package/lib/controller/createOptimistic.d.ts.map +0 -1
- package/lib/controller/createOptimistic.js +0 -27
- package/lib/controller/createReset.d.ts +0 -3
- package/lib/controller/createReset.d.ts.map +0 -1
- package/lib/controller/createReset.js +0 -8
- package/lib/controller/createSet.d.ts +0 -8
- package/lib/controller/createSet.d.ts.map +0 -1
- package/lib/controller/createSet.js +0 -28
- package/lib/controller/createSetResponse.d.ts.map +0 -1
- package/lib/controller/createSetResponse.js +0 -32
- package/lib/controller/createSubscription.d.ts.map +0 -1
- package/lib/controller/createSubscription.js +0 -26
- package/lib/state/RIC.d.ts +0 -3
- package/lib/state/RIC.d.ts.map +0 -1
- package/lib/state/RIC.js +0 -3
- package/src/controller/createExpireAll.ts +0 -11
- package/src/controller/createInvalidate.ts +0 -16
- package/src/controller/createInvalidateAll.ts +0 -11
- package/src/controller/createOptimistic.ts +0 -41
- package/src/controller/createReset.ts +0 -9
- package/src/controller/createSet.ts +0 -39
- package/src/state/RIC.ts +0 -5
- package/src/state/__tests__/RIC.web.ts +0 -16
- package/ts3.4/controller/createExpireAll.d.ts +0 -3
- package/ts3.4/controller/createInvalidateAll.d.ts +0 -3
- package/ts3.4/controller/createOptimistic.d.ts +0 -12
- package/ts3.4/controller/createReset.d.ts +0 -3
- package/ts3.4/controller/createSet.d.ts +0 -10
- package/ts3.4/state/RIC.d.ts +0 -3
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { SET_RESPONSE_TYPE, FETCH_TYPE, RESET_TYPE } from '../actionTypes.js';
|
|
2
|
+
import { createSetResponse } from '../controller/actions/index.js';
|
|
2
3
|
import Controller from '../controller/Controller.js';
|
|
3
|
-
import createSetResponse from '../controller/createSetResponse.js';
|
|
4
|
-
import RIC from '../state/RIC.js';
|
|
5
4
|
import type {
|
|
6
5
|
FetchAction,
|
|
7
6
|
Manager,
|
|
@@ -63,15 +62,14 @@ export default class NetworkManager implements Manager {
|
|
|
63
62
|
case SET_RESPONSE_TYPE:
|
|
64
63
|
// only set after new state is computed
|
|
65
64
|
return next(action).then(() => {
|
|
66
|
-
if (action.
|
|
65
|
+
if (action.key in this.fetched) {
|
|
67
66
|
// Note: meta *must* be set by reducer so this should be safe
|
|
68
|
-
const error =
|
|
69
|
-
controller.getState().meta[action.meta.key]?.error;
|
|
67
|
+
const error = controller.getState().meta[action.key]?.error;
|
|
70
68
|
// processing errors result in state meta having error, so we should reject the promise
|
|
71
69
|
if (error) {
|
|
72
70
|
this.handleSet(
|
|
73
71
|
createSetResponse(action.endpoint, {
|
|
74
|
-
args: action.
|
|
72
|
+
args: action.args,
|
|
75
73
|
response: error,
|
|
76
74
|
fetchedAt: action.meta.fetchedAt,
|
|
77
75
|
error: true,
|
|
@@ -104,7 +102,7 @@ export default class NetworkManager implements Manager {
|
|
|
104
102
|
/** Used by DevtoolsManager to determine whether to log an action */
|
|
105
103
|
skipLogging(action: ActionTypes) {
|
|
106
104
|
/* istanbul ignore next */
|
|
107
|
-
return action.type === FETCH_TYPE && action.
|
|
105
|
+
return action.type === FETCH_TYPE && action.key in this.fetched;
|
|
108
106
|
}
|
|
109
107
|
|
|
110
108
|
/** On mount */
|
|
@@ -150,15 +148,15 @@ export default class NetworkManager implements Manager {
|
|
|
150
148
|
* Will then start a promise for a key and potentially start the network
|
|
151
149
|
* fetch.
|
|
152
150
|
*
|
|
153
|
-
* Uses throttle
|
|
151
|
+
* Uses throttle endpoints without sideEffects. This is valuable
|
|
154
152
|
* for ensures mutation requests always go through.
|
|
155
153
|
*/
|
|
156
154
|
protected handleFetch(action: FetchAction) {
|
|
157
|
-
const
|
|
158
|
-
const
|
|
155
|
+
const { resolve, reject, fetchedAt } = action.meta;
|
|
156
|
+
const throttle = !action.endpoint.sideEffect;
|
|
159
157
|
|
|
160
158
|
const deferedFetch = () => {
|
|
161
|
-
let promise =
|
|
159
|
+
let promise = action.endpoint(...action.args);
|
|
162
160
|
const resolvePromise = (
|
|
163
161
|
promise: Promise<string | number | object | null>,
|
|
164
162
|
) =>
|
|
@@ -178,7 +176,7 @@ export default class NetworkManager implements Manager {
|
|
|
178
176
|
promise = resolvePromise(promise);
|
|
179
177
|
}
|
|
180
178
|
promise = promise
|
|
181
|
-
.then(
|
|
179
|
+
.then(response => {
|
|
182
180
|
let lastReset = this.getLastReset();
|
|
183
181
|
|
|
184
182
|
/* istanbul ignore else */
|
|
@@ -190,23 +188,23 @@ export default class NetworkManager implements Manager {
|
|
|
190
188
|
}
|
|
191
189
|
|
|
192
190
|
// don't update state with promises started before last clear
|
|
193
|
-
if (
|
|
191
|
+
if (fetchedAt >= lastReset) {
|
|
194
192
|
this.controller.resolve(action.endpoint, {
|
|
195
|
-
args: action.
|
|
196
|
-
response
|
|
197
|
-
fetchedAt
|
|
193
|
+
args: action.args,
|
|
194
|
+
response,
|
|
195
|
+
fetchedAt,
|
|
198
196
|
});
|
|
199
197
|
}
|
|
200
|
-
return
|
|
198
|
+
return response;
|
|
201
199
|
})
|
|
202
200
|
.catch(error => {
|
|
203
201
|
const lastReset = this.getLastReset();
|
|
204
202
|
// don't update state with promises started before last clear
|
|
205
|
-
if (
|
|
203
|
+
if (fetchedAt >= lastReset) {
|
|
206
204
|
this.controller.resolve(action.endpoint, {
|
|
207
|
-
args: action.
|
|
205
|
+
args: action.args,
|
|
208
206
|
response: error,
|
|
209
|
-
fetchedAt
|
|
207
|
+
fetchedAt,
|
|
210
208
|
error: true,
|
|
211
209
|
});
|
|
212
210
|
}
|
|
@@ -216,7 +214,7 @@ export default class NetworkManager implements Manager {
|
|
|
216
214
|
};
|
|
217
215
|
|
|
218
216
|
if (throttle) {
|
|
219
|
-
return this.throttle(key, deferedFetch,
|
|
217
|
+
return this.throttle(action.key, deferedFetch, fetchedAt)
|
|
220
218
|
.then(data => resolve(data))
|
|
221
219
|
.catch(error => reject(error));
|
|
222
220
|
} else {
|
|
@@ -230,16 +228,16 @@ export default class NetworkManager implements Manager {
|
|
|
230
228
|
*/
|
|
231
229
|
protected handleSet(action: SetResponseAction) {
|
|
232
230
|
// this can still turn out to be untrue since this is async
|
|
233
|
-
if (action.
|
|
231
|
+
if (action.key in this.fetched) {
|
|
234
232
|
let promiseHandler: (value?: any) => void;
|
|
235
233
|
if (action.error) {
|
|
236
|
-
promiseHandler = this.rejectors[action.
|
|
234
|
+
promiseHandler = this.rejectors[action.key];
|
|
237
235
|
} else {
|
|
238
|
-
promiseHandler = this.resolvers[action.
|
|
236
|
+
promiseHandler = this.resolvers[action.key];
|
|
239
237
|
}
|
|
240
|
-
promiseHandler(action.
|
|
238
|
+
promiseHandler(action.response);
|
|
241
239
|
// since we're resolved we no longer need to keep track of this promise
|
|
242
|
-
this.clear(action.
|
|
240
|
+
this.clear(action.key);
|
|
243
241
|
}
|
|
244
242
|
}
|
|
245
243
|
|
|
@@ -267,7 +265,7 @@ export default class NetworkManager implements Manager {
|
|
|
267
265
|
protected throttle(
|
|
268
266
|
key: string,
|
|
269
267
|
fetch: () => Promise<any>,
|
|
270
|
-
|
|
268
|
+
fetchedAt: number,
|
|
271
269
|
) {
|
|
272
270
|
const lastReset = this.getLastReset();
|
|
273
271
|
// we're already fetching so reuse the promise
|
|
@@ -280,13 +278,13 @@ export default class NetworkManager implements Manager {
|
|
|
280
278
|
this.resolvers[key] = resolve;
|
|
281
279
|
this.rejectors[key] = reject;
|
|
282
280
|
});
|
|
283
|
-
this.fetchedAt[key] =
|
|
281
|
+
this.fetchedAt[key] = fetchedAt;
|
|
284
282
|
|
|
285
|
-
|
|
286
|
-
// we should just stop all errors here.
|
|
287
|
-
// TODO: decouple this from useFetcher() (that's what's dispatching the error the resolves in here)
|
|
288
|
-
RIC(
|
|
283
|
+
this.idleCallback(
|
|
289
284
|
() => {
|
|
285
|
+
// since our real promise is resolved via the wrapReducer(),
|
|
286
|
+
// we should just stop all errors here.
|
|
287
|
+
// TODO: decouple this from useFetcher() (that's what's dispatching the error the resolves in here)
|
|
290
288
|
fetch().catch(() => null);
|
|
291
289
|
},
|
|
292
290
|
{ timeout: 500 },
|
|
@@ -294,4 +292,15 @@ export default class NetworkManager implements Manager {
|
|
|
294
292
|
|
|
295
293
|
return this.fetched[key];
|
|
296
294
|
}
|
|
295
|
+
|
|
296
|
+
/** Calls the callback when client is not 'busy' with high priority interaction tasks
|
|
297
|
+
*
|
|
298
|
+
* Override for platform-specific implementations
|
|
299
|
+
*/
|
|
300
|
+
protected idleCallback(
|
|
301
|
+
callback: (...args: any[]) => void,
|
|
302
|
+
options?: IdleRequestOptions,
|
|
303
|
+
) {
|
|
304
|
+
callback();
|
|
305
|
+
}
|
|
297
306
|
}
|
|
@@ -34,8 +34,8 @@ export default class PollingSubscription implements Subscription {
|
|
|
34
34
|
throw new Error('frequency needed for polling subscription');
|
|
35
35
|
this.endpoint = action.endpoint;
|
|
36
36
|
this.frequency = action.endpoint.pollFrequency;
|
|
37
|
-
this.args = action.
|
|
38
|
-
this.key = action.
|
|
37
|
+
this.args = action.args;
|
|
38
|
+
this.key = action.key;
|
|
39
39
|
this.frequencyHistogram.set(this.frequency, 1);
|
|
40
40
|
this.controller = controller;
|
|
41
41
|
this.connectionListener =
|
|
@@ -79,7 +79,7 @@ export default class SubscriptionManager<S extends SubscriptionConstructable>
|
|
|
79
79
|
*
|
|
80
80
|
*/
|
|
81
81
|
protected handleSubscribe(action: SubscribeAction) {
|
|
82
|
-
const key = action.
|
|
82
|
+
const key = action.key;
|
|
83
83
|
|
|
84
84
|
if (key in this.subscriptions) {
|
|
85
85
|
const frequency = action.endpoint.pollFrequency;
|
|
@@ -96,7 +96,7 @@ export default class SubscriptionManager<S extends SubscriptionConstructable>
|
|
|
96
96
|
*
|
|
97
97
|
*/
|
|
98
98
|
protected handleUnsubscribe(action: UnsubscribeAction) {
|
|
99
|
-
const key = action.
|
|
99
|
+
const key = action.key;
|
|
100
100
|
|
|
101
101
|
/* istanbul ignore else */
|
|
102
102
|
if (key in this.subscriptions) {
|
|
@@ -9,17 +9,14 @@ exports[`PollingSubscription fresh data cleanup() should not run even if interva
|
|
|
9
9
|
exports[`PollingSubscription fresh data should call after period 1`] = `
|
|
10
10
|
[
|
|
11
11
|
{
|
|
12
|
+
"args": [],
|
|
12
13
|
"endpoint": [Function],
|
|
14
|
+
"key": "test.com",
|
|
13
15
|
"meta": {
|
|
14
|
-
"args": [],
|
|
15
|
-
"key": "test.com",
|
|
16
|
-
"nm": false,
|
|
17
16
|
"promise": Promise {},
|
|
18
17
|
"reject": [Function],
|
|
19
18
|
"resolve": [Function],
|
|
20
|
-
"throttle": true,
|
|
21
19
|
},
|
|
22
|
-
"payload": [Function],
|
|
23
20
|
"type": "rdc/fetch",
|
|
24
21
|
},
|
|
25
22
|
]
|
|
@@ -28,17 +25,14 @@ exports[`PollingSubscription fresh data should call after period 1`] = `
|
|
|
28
25
|
exports[`PollingSubscription fresh data should call after period 2`] = `
|
|
29
26
|
[
|
|
30
27
|
{
|
|
28
|
+
"args": [],
|
|
31
29
|
"endpoint": [Function],
|
|
30
|
+
"key": "test.com",
|
|
32
31
|
"meta": {
|
|
33
|
-
"args": [],
|
|
34
|
-
"key": "test.com",
|
|
35
|
-
"nm": false,
|
|
36
32
|
"promise": Promise {},
|
|
37
33
|
"reject": [Function],
|
|
38
34
|
"resolve": [Function],
|
|
39
|
-
"throttle": true,
|
|
40
35
|
},
|
|
41
|
-
"payload": [Function],
|
|
42
36
|
"type": "rdc/fetch",
|
|
43
37
|
},
|
|
44
38
|
]
|
|
@@ -2,7 +2,7 @@ import { CoolerArticleResource } from '__tests__/new';
|
|
|
2
2
|
|
|
3
3
|
import { Controller, initialState } from '../..';
|
|
4
4
|
import { FETCH_TYPE, RESET_TYPE } from '../../actionTypes';
|
|
5
|
-
import createSetResponse from '../../controller/
|
|
5
|
+
import { createSetResponse } from '../../controller/actions';
|
|
6
6
|
import LogoutManager from '../LogoutManager.js';
|
|
7
7
|
|
|
8
8
|
function onError(e: any) {
|
|
@@ -2,11 +2,11 @@ import { Endpoint } from '@data-client/endpoint';
|
|
|
2
2
|
import { Article, ArticleResource } from '__tests__/new';
|
|
3
3
|
|
|
4
4
|
import { SET_RESPONSE_TYPE } from '../../actionTypes';
|
|
5
|
+
import { createFetch } from '../../controller/actions';
|
|
5
6
|
import Controller from '../../controller/Controller';
|
|
6
|
-
import createFetch from '../../controller/createFetch';
|
|
7
7
|
import NetworkManager from '../../manager/NetworkManager';
|
|
8
8
|
import { initialState } from '../../state/reducer/createReducer';
|
|
9
|
-
import { Middleware } from '../../types';
|
|
9
|
+
import { Middleware, SetResponseAction } from '../../types';
|
|
10
10
|
|
|
11
11
|
describe('NetworkManager', () => {
|
|
12
12
|
const manager = new NetworkManager();
|
|
@@ -154,18 +154,21 @@ describe('NetworkManager', () => {
|
|
|
154
154
|
|
|
155
155
|
middleware(API)(next)(fetchResolveAction);
|
|
156
156
|
|
|
157
|
-
const
|
|
157
|
+
const response = await fetchResolveAction.endpoint(
|
|
158
|
+
...fetchResolveAction.args,
|
|
159
|
+
);
|
|
158
160
|
|
|
159
161
|
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
160
162
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
161
163
|
|
|
162
|
-
const action = {
|
|
164
|
+
const action: SetResponseAction = {
|
|
163
165
|
type: SET_RESPONSE_TYPE,
|
|
164
166
|
endpoint: fetchResolveAction.endpoint,
|
|
165
|
-
|
|
167
|
+
response,
|
|
168
|
+
args: fetchResolveAction.args,
|
|
169
|
+
key: fetchResolveAction.key,
|
|
170
|
+
error: expect.anything(),
|
|
166
171
|
meta: {
|
|
167
|
-
args: fetchResolveAction.meta.args,
|
|
168
|
-
key: fetchResolveAction.meta.key,
|
|
169
172
|
date: expect.any(Number),
|
|
170
173
|
expiresAt: expect.any(Number),
|
|
171
174
|
fetchedAt: expect.any(Number),
|
|
@@ -187,18 +190,21 @@ describe('NetworkManager', () => {
|
|
|
187
190
|
|
|
188
191
|
middleware(API)(next)(fetchSetWithUpdatersAction);
|
|
189
192
|
|
|
190
|
-
const
|
|
193
|
+
const response = await fetchSetWithUpdatersAction.endpoint(
|
|
194
|
+
...fetchSetWithUpdatersAction.args,
|
|
195
|
+
);
|
|
191
196
|
|
|
192
197
|
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
193
198
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
194
199
|
|
|
195
|
-
const action = {
|
|
200
|
+
const action: SetResponseAction = {
|
|
196
201
|
type: SET_RESPONSE_TYPE,
|
|
197
202
|
endpoint: fetchSetWithUpdatersAction.endpoint,
|
|
198
|
-
|
|
203
|
+
response,
|
|
204
|
+
args: fetchSetWithUpdatersAction.args,
|
|
205
|
+
key: fetchSetWithUpdatersAction.key,
|
|
206
|
+
error: expect.anything(),
|
|
199
207
|
meta: {
|
|
200
|
-
args: fetchSetWithUpdatersAction.meta.args,
|
|
201
|
-
key: fetchSetWithUpdatersAction.meta.key,
|
|
202
208
|
date: expect.any(Number),
|
|
203
209
|
expiresAt: expect.any(Number),
|
|
204
210
|
fetchedAt: expect.any(Number),
|
|
@@ -220,18 +226,21 @@ describe('NetworkManager', () => {
|
|
|
220
226
|
|
|
221
227
|
middleware(API)(next)(fetchRpcWithUpdatersAction);
|
|
222
228
|
|
|
223
|
-
const
|
|
229
|
+
const response = await fetchRpcWithUpdatersAction.endpoint(
|
|
230
|
+
...fetchRpcWithUpdatersAction.args,
|
|
231
|
+
);
|
|
224
232
|
|
|
225
233
|
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
226
234
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
227
235
|
|
|
228
|
-
const action = {
|
|
236
|
+
const action: SetResponseAction = {
|
|
229
237
|
type: SET_RESPONSE_TYPE,
|
|
230
238
|
endpoint: fetchRpcWithUpdatersAction.endpoint,
|
|
231
|
-
|
|
239
|
+
response,
|
|
240
|
+
args: fetchRpcWithUpdatersAction.args,
|
|
241
|
+
key: fetchRpcWithUpdatersAction.key,
|
|
242
|
+
error: expect.anything(),
|
|
232
243
|
meta: {
|
|
233
|
-
args: fetchRpcWithUpdatersAction.meta.args,
|
|
234
|
-
key: fetchRpcWithUpdatersAction.meta.key,
|
|
235
244
|
date: expect.any(Number),
|
|
236
245
|
expiresAt: expect.any(Number),
|
|
237
246
|
fetchedAt: expect.any(Number),
|
|
@@ -253,7 +262,9 @@ describe('NetworkManager', () => {
|
|
|
253
262
|
|
|
254
263
|
middleware(API)(next)(fetchRpcWithUpdatersAndOptimisticAction);
|
|
255
264
|
|
|
256
|
-
const
|
|
265
|
+
const response = await fetchRpcWithUpdatersAndOptimisticAction.endpoint(
|
|
266
|
+
...fetchRpcWithUpdatersAndOptimisticAction.args,
|
|
267
|
+
);
|
|
257
268
|
|
|
258
269
|
expect(next).toHaveBeenCalled();
|
|
259
270
|
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
@@ -261,10 +272,11 @@ describe('NetworkManager', () => {
|
|
|
261
272
|
expect(dispatch).toHaveBeenCalledWith({
|
|
262
273
|
type: SET_RESPONSE_TYPE,
|
|
263
274
|
endpoint: fetchRpcWithUpdatersAndOptimisticAction.endpoint,
|
|
264
|
-
|
|
275
|
+
response,
|
|
276
|
+
args: fetchRpcWithUpdatersAndOptimisticAction.args,
|
|
277
|
+
key: fetchRpcWithUpdatersAndOptimisticAction.key,
|
|
278
|
+
error: expect.anything(),
|
|
265
279
|
meta: {
|
|
266
|
-
args: fetchRpcWithUpdatersAndOptimisticAction.meta.args,
|
|
267
|
-
key: fetchRpcWithUpdatersAndOptimisticAction.meta.key,
|
|
268
280
|
date: expect.any(Number),
|
|
269
281
|
expiresAt: expect.any(Number),
|
|
270
282
|
fetchedAt: expect.any(Number),
|
|
@@ -286,7 +298,7 @@ describe('NetworkManager', () => {
|
|
|
286
298
|
endpoint: detailEndpoint.extend({ dataExpiryLength: 314 }),
|
|
287
299
|
});
|
|
288
300
|
|
|
289
|
-
await fetchResolveAction.
|
|
301
|
+
await fetchResolveAction.endpoint(...fetchResolveAction.args);
|
|
290
302
|
|
|
291
303
|
expect(dispatch).toHaveBeenCalled();
|
|
292
304
|
const { meta } = dispatch.mock.calls[0][0];
|
|
@@ -307,7 +319,7 @@ describe('NetworkManager', () => {
|
|
|
307
319
|
endpoint: detailEndpoint.extend({ dataExpiryLength: undefined }),
|
|
308
320
|
});
|
|
309
321
|
|
|
310
|
-
await fetchResolveAction.
|
|
322
|
+
await fetchResolveAction.endpoint(...fetchResolveAction.args);
|
|
311
323
|
|
|
312
324
|
expect(dispatch).toHaveBeenCalled();
|
|
313
325
|
const { meta } = dispatch.mock.calls[0][0];
|
|
@@ -330,9 +342,9 @@ describe('NetworkManager', () => {
|
|
|
330
342
|
expect(next).not.toHaveBeenCalled();
|
|
331
343
|
expect(dispatch).toHaveBeenCalledWith({
|
|
332
344
|
type: SET_RESPONSE_TYPE,
|
|
333
|
-
|
|
345
|
+
response: error,
|
|
346
|
+
key: fetchRejectAction.key,
|
|
334
347
|
meta: {
|
|
335
|
-
key: fetchRejectAction.meta.key,
|
|
336
348
|
date: expect.any(Number),
|
|
337
349
|
expiresAt: expect.any(Number),
|
|
338
350
|
},
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Endpoint } from '@data-client/endpoint';
|
|
2
2
|
import { Article, PollingArticleResource } from '__tests__/new';
|
|
3
3
|
|
|
4
|
+
import { createSubscription } from '../../controller/actions/createSubscription';
|
|
4
5
|
import Controller from '../../controller/Controller';
|
|
5
|
-
import { createSubscription } from '../../controller/createSubscription';
|
|
6
6
|
import { initialState } from '../../state/reducer/createReducer';
|
|
7
7
|
import ConnectionListener from '../ConnectionListener';
|
|
8
8
|
import DefaultConnectionListener from '../DefaultConnectionListener';
|
|
@@ -178,13 +178,13 @@ describe('PollingSubscription', () => {
|
|
|
178
178
|
jest.advanceTimersByTime(5000);
|
|
179
179
|
expect(dispatch.mock.calls.length).toBe(1);
|
|
180
180
|
dispatch.mock.calls[0].forEach((element: any) => {
|
|
181
|
-
delete element?.meta?.
|
|
181
|
+
delete element?.meta?.fetchedAt;
|
|
182
182
|
});
|
|
183
183
|
expect(dispatch.mock.calls[0]).toMatchSnapshot();
|
|
184
184
|
jest.advanceTimersByTime(5000);
|
|
185
185
|
expect(dispatch.mock.calls.length).toBe(2);
|
|
186
186
|
dispatch.mock.calls[1].forEach((element: any) => {
|
|
187
|
-
delete element?.meta?.
|
|
187
|
+
delete element?.meta?.fetchedAt;
|
|
188
188
|
});
|
|
189
189
|
|
|
190
190
|
expect(dispatch.mock.calls[1]).toMatchSnapshot();
|
|
@@ -48,32 +48,28 @@ describe('SubscriptionManager', () => {
|
|
|
48
48
|
|
|
49
49
|
describe('middleware', () => {
|
|
50
50
|
function createSubscribeAction(
|
|
51
|
-
|
|
51
|
+
response: Record<string, any>,
|
|
52
52
|
reject = false,
|
|
53
53
|
): SubscribeAction {
|
|
54
54
|
const fetch =
|
|
55
55
|
reject ?
|
|
56
56
|
() => Promise.reject(new Error('Failed'))
|
|
57
|
-
: () => Promise.resolve(
|
|
57
|
+
: () => Promise.resolve(response);
|
|
58
58
|
return {
|
|
59
59
|
type: SUBSCRIBE_TYPE,
|
|
60
|
-
endpoint: PollingArticleResource.get,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
args: [{ id: payload.id }],
|
|
64
|
-
},
|
|
60
|
+
endpoint: PollingArticleResource.get.extend({ fetch }),
|
|
61
|
+
args: [{ id: response.id }],
|
|
62
|
+
key: PollingArticleResource.get.key({ id: response.id }),
|
|
65
63
|
};
|
|
66
64
|
}
|
|
67
65
|
function createUnsubscribeAction(
|
|
68
|
-
|
|
66
|
+
response: Record<string, any>,
|
|
69
67
|
): UnsubscribeAction {
|
|
70
68
|
return {
|
|
71
69
|
type: UNSUBSCRIBE_TYPE,
|
|
72
70
|
endpoint: PollingArticleResource.get,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
args: [{ id: payload.id }],
|
|
76
|
-
},
|
|
71
|
+
key: PollingArticleResource.get.key({ id: response.id }),
|
|
72
|
+
args: [{ id: response.id }],
|
|
77
73
|
};
|
|
78
74
|
}
|
|
79
75
|
|
|
@@ -93,14 +89,14 @@ describe('SubscriptionManager', () => {
|
|
|
93
89
|
middleware(API)(next)(action);
|
|
94
90
|
|
|
95
91
|
expect(next).not.toHaveBeenCalled();
|
|
96
|
-
expect((manager as any).subscriptions[action.
|
|
92
|
+
expect((manager as any).subscriptions[action.key]).toBeDefined();
|
|
97
93
|
});
|
|
98
94
|
it('subscribe should add a subscription (no frequency)', () => {
|
|
99
95
|
const action = createSubscribeAction({ id: 597 });
|
|
100
96
|
middleware(API)(next)(action);
|
|
101
97
|
|
|
102
98
|
expect(next).not.toHaveBeenCalled();
|
|
103
|
-
expect((manager as any).subscriptions[action.
|
|
99
|
+
expect((manager as any).subscriptions[action.key]).toBeDefined();
|
|
104
100
|
});
|
|
105
101
|
|
|
106
102
|
it('subscribe with same should call subscription.add', () => {
|
|
@@ -108,20 +104,20 @@ describe('SubscriptionManager', () => {
|
|
|
108
104
|
middleware(API)(next)(action);
|
|
109
105
|
|
|
110
106
|
expect(
|
|
111
|
-
(manager as any).subscriptions[action.
|
|
107
|
+
(manager as any).subscriptions[action.key].add.mock.calls.length,
|
|
112
108
|
).toBe(1);
|
|
113
109
|
middleware(API)(next)(action);
|
|
114
110
|
expect(
|
|
115
|
-
(manager as any).subscriptions[action.
|
|
111
|
+
(manager as any).subscriptions[action.key].add.mock.calls.length,
|
|
116
112
|
).toBe(2);
|
|
117
113
|
});
|
|
118
114
|
it('subscribe with another should create another', () => {
|
|
119
115
|
const action = createSubscribeAction({ id: 7, title: 'four cakes' });
|
|
120
116
|
middleware(API)(next)(action);
|
|
121
117
|
|
|
122
|
-
expect((manager as any).subscriptions[action.
|
|
118
|
+
expect((manager as any).subscriptions[action.key]).toBeDefined();
|
|
123
119
|
expect(
|
|
124
|
-
(manager as any).subscriptions[action.
|
|
120
|
+
(manager as any).subscriptions[action.key].add.mock.calls.length,
|
|
125
121
|
).toBe(0);
|
|
126
122
|
});
|
|
127
123
|
it('subscribe with another should not call previous', () => {
|
|
@@ -134,13 +130,13 @@ describe('SubscriptionManager', () => {
|
|
|
134
130
|
|
|
135
131
|
it('unsubscribe should delete when remove returns true', () => {
|
|
136
132
|
const action = createUnsubscribeAction({ id: 7, title: 'four cakes' });
|
|
137
|
-
(manager as any).subscriptions[action.
|
|
133
|
+
(manager as any).subscriptions[action.key].remove.mockImplementation(
|
|
138
134
|
() => true,
|
|
139
135
|
);
|
|
140
136
|
|
|
141
137
|
middleware(API)(next)(action);
|
|
142
138
|
|
|
143
|
-
expect((manager as any).subscriptions[action.
|
|
139
|
+
expect((manager as any).subscriptions[action.key]).not.toBeDefined();
|
|
144
140
|
});
|
|
145
141
|
|
|
146
142
|
it('unsubscribe should delete when remove returns true (no frequency)', () => {
|
|
@@ -149,27 +145,26 @@ describe('SubscriptionManager', () => {
|
|
|
149
145
|
);
|
|
150
146
|
|
|
151
147
|
const action = createUnsubscribeAction({ id: 50, title: 'four cakes' });
|
|
152
|
-
(manager as any).subscriptions[action.
|
|
148
|
+
(manager as any).subscriptions[action.key].remove.mockImplementation(
|
|
153
149
|
() => true,
|
|
154
150
|
);
|
|
155
151
|
|
|
156
152
|
middleware(API)(next)(action);
|
|
157
153
|
|
|
158
|
-
expect((manager as any).subscriptions[action.
|
|
154
|
+
expect((manager as any).subscriptions[action.key]).not.toBeDefined();
|
|
159
155
|
});
|
|
160
156
|
|
|
161
157
|
it('unsubscribe should not delete when remove returns false', () => {
|
|
162
158
|
const action = createUnsubscribeAction({ id: 5, title: 'four cakes' });
|
|
163
|
-
(manager as any).subscriptions[action.
|
|
159
|
+
(manager as any).subscriptions[action.key].remove.mockImplementation(
|
|
164
160
|
() => false,
|
|
165
161
|
);
|
|
166
162
|
|
|
167
163
|
middleware(API)(next)(action);
|
|
168
164
|
|
|
169
|
-
expect((manager as any).subscriptions[action.
|
|
165
|
+
expect((manager as any).subscriptions[action.key]).toBeDefined();
|
|
170
166
|
expect(
|
|
171
|
-
(manager as any).subscriptions[action.
|
|
172
|
-
.length,
|
|
167
|
+
(manager as any).subscriptions[action.key].remove.mock.calls.length,
|
|
173
168
|
).toBe(1);
|
|
174
169
|
});
|
|
175
170
|
|
|
@@ -181,7 +176,7 @@ describe('SubscriptionManager', () => {
|
|
|
181
176
|
|
|
182
177
|
middleware(API)(next)(action);
|
|
183
178
|
|
|
184
|
-
expect((manager as any).subscriptions[action.
|
|
179
|
+
expect((manager as any).subscriptions[action.key]).not.toBeDefined();
|
|
185
180
|
|
|
186
181
|
expect(spy.mock.calls[0]).toMatchInlineSnapshot(`
|
|
187
182
|
[
|