@data-client/core 0.4.3 → 0.8.1
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/data_client_logo_and_text.svg +50 -0
- package/dist/index.js +57 -90
- package/dist/index.umd.min.js +1 -1
- package/dist/next.js +0 -454
- package/legacy/actionTypes.js +11 -13
- package/legacy/actions.js +2 -0
- package/legacy/controller/Controller.js +24 -41
- package/legacy/index.js +2 -2
- package/legacy/internal.js +2 -2
- package/legacy/manager/DevtoolsManager.js +3 -3
- package/legacy/manager/NetworkManager.js +11 -11
- package/legacy/manager/SubscriptionManager.js +6 -6
- package/legacy/manager/applyManager.js +2 -9
- package/legacy/middlewareTypes.js +1 -1
- package/legacy/next/index.js +2 -3
- package/legacy/state/reducer/fetchReducer.js +5 -5
- package/legacy/state/reducer/setReducer.js +2 -2
- package/legacy/types.js +3 -4
- package/lib/actionTypes.d.ts +10 -12
- package/lib/actionTypes.d.ts.map +1 -1
- package/lib/actionTypes.js +11 -13
- package/lib/{newActions.d.ts → actions.d.ts} +1 -3
- package/lib/actions.d.ts.map +1 -0
- package/lib/actions.js +2 -0
- package/lib/controller/Controller.d.ts +20 -33
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +24 -41
- package/lib/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -2
- package/lib/internal.d.ts +1 -1
- package/lib/internal.d.ts.map +1 -1
- package/lib/internal.js +2 -2
- package/lib/manager/DevtoolsManager.js +3 -3
- package/lib/manager/NetworkManager.d.ts +6 -6
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +11 -11
- package/lib/manager/SubscriptionManager.d.ts +5 -5
- package/lib/manager/SubscriptionManager.js +6 -6
- package/lib/manager/applyManager.d.ts.map +1 -1
- package/lib/manager/applyManager.js +2 -9
- package/lib/middlewareTypes.d.ts +2 -5
- package/lib/middlewareTypes.d.ts.map +1 -1
- package/lib/middlewareTypes.js +1 -1
- package/lib/next/index.d.ts +0 -2
- package/lib/next/index.d.ts.map +1 -1
- package/lib/next/index.js +2 -3
- package/lib/state/reducer/expireReducer.d.ts +1 -1
- package/lib/state/reducer/fetchReducer.js +5 -5
- package/lib/state/reducer/invalidateReducer.d.ts +1 -1
- package/lib/state/reducer/setReducer.d.ts +2 -2
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +2 -2
- package/lib/types.d.ts +3 -4
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +3 -4
- package/package.json +3 -3
- package/src/actionTypes.ts +10 -12
- package/src/{newActions.ts → actions.ts} +1 -6
- package/src/controller/Controller.ts +23 -54
- package/src/index.ts +1 -1
- package/src/internal.ts +1 -1
- package/src/manager/DevtoolsManager.ts +2 -2
- package/src/manager/NetworkManager.ts +10 -10
- package/src/manager/SubscriptionManager.ts +5 -5
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +2 -2
- package/src/manager/__tests__/logoutManager.ts +2 -2
- package/src/manager/__tests__/manager.ts +1 -1
- package/src/manager/__tests__/networkManager.ts +13 -16
- package/src/manager/applyManager.ts +1 -6
- package/src/middlewareTypes.ts +4 -9
- package/src/next/index.ts +0 -2
- package/src/state/__tests__/__snapshots__/reducer.ts.snap +1 -1
- package/src/state/__tests__/reducer.ts +27 -9
- package/src/state/reducer/fetchReducer.ts +5 -5
- package/src/state/reducer/setReducer.ts +5 -5
- package/src/types.ts +3 -4
- package/ts3.4/actionTypes.d.ts +10 -12
- package/ts3.4/{newActions.d.ts → actions.d.ts} +1 -3
- package/ts3.4/controller/Controller.d.ts +20 -39
- package/ts3.4/index.d.ts +1 -1
- package/ts3.4/internal.d.ts +1 -1
- package/ts3.4/manager/NetworkManager.d.ts +6 -6
- package/ts3.4/manager/SubscriptionManager.d.ts +5 -5
- package/ts3.4/middlewareTypes.d.ts +2 -5
- package/ts3.4/next/index.d.ts +0 -2
- package/ts3.4/state/reducer/expireReducer.d.ts +1 -1
- package/ts3.4/state/reducer/invalidateReducer.d.ts +1 -1
- package/ts3.4/state/reducer/setReducer.d.ts +2 -2
- package/ts3.4/types.d.ts +3 -5
- package/legacy/fsa.js +0 -2
- package/legacy/newActions.js +0 -2
- package/lib/fsa.d.ts +0 -41
- package/lib/fsa.d.ts.map +0 -1
- package/lib/fsa.js +0 -2
- package/lib/newActions.d.ts.map +0 -1
- package/lib/newActions.js +0 -2
- package/src/fsa.ts +0 -99
- package/ts3.4/fsa.d.ts +0 -41
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@data-client/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "High performance reactive framework for async data.",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"LICENSE",
|
|
61
61
|
"README.md",
|
|
62
62
|
"./typescript.svg",
|
|
63
|
-
"./
|
|
63
|
+
"./data_client_logo_and_text.svg"
|
|
64
64
|
],
|
|
65
65
|
"scripts": {
|
|
66
66
|
"build:lib": "NODE_ENV=production BROWSERSLIST_ENV='2020' yarn g:babel --out-dir lib",
|
|
@@ -120,7 +120,7 @@
|
|
|
120
120
|
},
|
|
121
121
|
"dependencies": {
|
|
122
122
|
"@babel/runtime": "^7.17.0",
|
|
123
|
-
"@data-client/normalizr": "^0.
|
|
123
|
+
"@data-client/normalizr": "^0.8.0",
|
|
124
124
|
"flux-standard-action": "^2.1.1"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
package/src/actionTypes.ts
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
export const FETCH_TYPE = '
|
|
2
|
-
|
|
3
|
-
export const
|
|
4
|
-
export const
|
|
5
|
-
export const
|
|
6
|
-
export const
|
|
7
|
-
export const
|
|
8
|
-
export const
|
|
9
|
-
export const
|
|
10
|
-
export const
|
|
11
|
-
export const EXPIREALL_TYPE = 'rest-hooks/expireall' as const;
|
|
12
|
-
export const GC_TYPE = 'rest-hooks/gc' as const;
|
|
1
|
+
export const FETCH_TYPE = 'rdc/fetch' as const;
|
|
2
|
+
export const SET_TYPE = 'rdc/set' as const;
|
|
3
|
+
export const OPTIMISTIC_TYPE = 'rdc/optimistic' as const;
|
|
4
|
+
export const RESET_TYPE = 'rdc/reset' as const;
|
|
5
|
+
export const SUBSCRIBE_TYPE = 'rdc/subscribe' as const;
|
|
6
|
+
export const UNSUBSCRIBE_TYPE = 'rdc/unsubscribe' as const;
|
|
7
|
+
export const INVALIDATE_TYPE = 'rdc/invalidate' as const;
|
|
8
|
+
export const INVALIDATEALL_TYPE = 'rdc/invalidateall' as const;
|
|
9
|
+
export const EXPIREALL_TYPE = 'rdc/expireall' as const;
|
|
10
|
+
export const GC_TYPE = 'rdc/gc' as const;
|
|
@@ -25,7 +25,7 @@ type EndpointDefault = EndpointInterface & {
|
|
|
25
25
|
update?: EndpointUpdateFunction<EndpointInterface>;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
/*
|
|
28
|
+
/* SET */
|
|
29
29
|
export interface SetMeta {
|
|
30
30
|
args: readonly any[];
|
|
31
31
|
key: string;
|
|
@@ -55,11 +55,6 @@ export type SetAction<E extends EndpointAndUpdate<E> = EndpointDefault> =
|
|
|
55
55
|
| SetActionSuccess<E>
|
|
56
56
|
| SetActionError<E>;
|
|
57
57
|
|
|
58
|
-
// TODO(breaking): Remove - legacy name compatibility
|
|
59
|
-
/** @deprecated use SetAction instead */
|
|
60
|
-
export type ReceiveAction<E extends EndpointAndUpdate<E> = EndpointDefault> =
|
|
61
|
-
SetAction<E>;
|
|
62
|
-
|
|
63
58
|
/* FETCH */
|
|
64
59
|
export interface FetchMeta {
|
|
65
60
|
args: readonly any[];
|
|
@@ -57,16 +57,16 @@ const unsetState = (): State<unknown> => {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
* Imperative control of
|
|
61
|
-
* @see https://
|
|
60
|
+
* Imperative control of Reactive Data Client store
|
|
61
|
+
* @see https://dataclient.io/docs/api/Controller
|
|
62
62
|
*/
|
|
63
63
|
export default class Controller<
|
|
64
64
|
D extends GenericDispatch = DataClientDispatch,
|
|
65
65
|
> {
|
|
66
66
|
/**
|
|
67
|
-
* Dispatches an action to
|
|
67
|
+
* Dispatches an action to Reactive Data Client reducer.
|
|
68
68
|
*
|
|
69
|
-
* @see https://
|
|
69
|
+
* @see https://dataclient.io/docs/api/Controller#dispatch
|
|
70
70
|
*/
|
|
71
71
|
declare readonly dispatch: D;
|
|
72
72
|
/**
|
|
@@ -74,7 +74,7 @@ export default class Controller<
|
|
|
74
74
|
*
|
|
75
75
|
* This can be useful for imperative use-cases like event handlers.
|
|
76
76
|
* This should *not* be used to render; instead useSuspense() or useCache()
|
|
77
|
-
* @see https://
|
|
77
|
+
* @see https://dataclient.io/docs/api/Controller#getState
|
|
78
78
|
*/
|
|
79
79
|
declare readonly getState: () => State<unknown>;
|
|
80
80
|
declare readonly globalCache: DenormalizeCache;
|
|
@@ -95,8 +95,8 @@ export default class Controller<
|
|
|
95
95
|
/*************** Action Dispatchers ***************/
|
|
96
96
|
|
|
97
97
|
/**
|
|
98
|
-
* Fetches the endpoint with given args, updating the
|
|
99
|
-
* @see https://
|
|
98
|
+
* Fetches the endpoint with given args, updating the Reactive Data Client cache with the response or error upon completion.
|
|
99
|
+
* @see https://dataclient.io/docs/api/Controller#fetch
|
|
100
100
|
*/
|
|
101
101
|
fetch = <
|
|
102
102
|
E extends EndpointInterface & { update?: EndpointUpdateFunction<E> },
|
|
@@ -143,7 +143,7 @@ export default class Controller<
|
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
145
|
* Forces refetching and suspense on useSuspense with the same Endpoint and parameters.
|
|
146
|
-
* @see https://
|
|
146
|
+
* @see https://dataclient.io/docs/api/Controller#invalidate
|
|
147
147
|
*/
|
|
148
148
|
invalidate = <E extends EndpointInterface>(
|
|
149
149
|
endpoint: E,
|
|
@@ -159,7 +159,7 @@ export default class Controller<
|
|
|
159
159
|
|
|
160
160
|
/**
|
|
161
161
|
* Forces refetching and suspense on useSuspense on all matching endpoint result keys.
|
|
162
|
-
* @see https://
|
|
162
|
+
* @see https://dataclient.io/docs/api/Controller#invalidateAll
|
|
163
163
|
* @returns Promise that resolves when invalidation is commited.
|
|
164
164
|
*/
|
|
165
165
|
invalidateAll = (options: { testKey: (key: string) => boolean }) =>
|
|
@@ -174,14 +174,14 @@ export default class Controller<
|
|
|
174
174
|
this.dispatch(createExpireAll((key: string) => options.testKey(key)));
|
|
175
175
|
|
|
176
176
|
/**
|
|
177
|
-
* Resets the entire
|
|
178
|
-
* @see https://
|
|
177
|
+
* Resets the entire Reactive Data Client cache. All inflight requests will not resolve.
|
|
178
|
+
* @see https://dataclient.io/docs/api/Controller#resetEntireStore
|
|
179
179
|
*/
|
|
180
180
|
resetEntireStore = (): Promise<void> => this.dispatch(createReset());
|
|
181
181
|
|
|
182
182
|
/**
|
|
183
183
|
* Stores response in cache for given Endpoint and args.
|
|
184
|
-
* @see https://
|
|
184
|
+
* @see https://dataclient.io/docs/api/Controller#set
|
|
185
185
|
*/
|
|
186
186
|
setResponse = <
|
|
187
187
|
E extends EndpointInterface & {
|
|
@@ -199,24 +199,9 @@ export default class Controller<
|
|
|
199
199
|
return this.dispatch(action);
|
|
200
200
|
};
|
|
201
201
|
|
|
202
|
-
/**
|
|
203
|
-
* @deprecated use https://resthooks.io/docs/api/Controller#setResponse instead
|
|
204
|
-
*/
|
|
205
|
-
/* istanbul ignore next */ receive = <
|
|
206
|
-
E extends EndpointInterface & {
|
|
207
|
-
update?: EndpointUpdateFunction<E>;
|
|
208
|
-
},
|
|
209
|
-
>(
|
|
210
|
-
endpoint: E,
|
|
211
|
-
...rest: readonly [...Parameters<E>, any]
|
|
212
|
-
): Promise<void> => {
|
|
213
|
-
/* istanbul ignore next */
|
|
214
|
-
return this.setResponse(endpoint, ...rest);
|
|
215
|
-
};
|
|
216
|
-
|
|
217
202
|
/**
|
|
218
203
|
* Stores the result of Endpoint and args as the error provided.
|
|
219
|
-
* @see https://
|
|
204
|
+
* @see https://dataclient.io/docs/api/Controller#setError
|
|
220
205
|
*/
|
|
221
206
|
setError = <
|
|
222
207
|
E extends EndpointInterface & {
|
|
@@ -235,25 +220,9 @@ export default class Controller<
|
|
|
235
220
|
return this.dispatch(action);
|
|
236
221
|
};
|
|
237
222
|
|
|
238
|
-
/**
|
|
239
|
-
* Another name for setError
|
|
240
|
-
* @deprecated use https://resthooks.io/docs/api/Controller#setError instead
|
|
241
|
-
*/
|
|
242
|
-
/* istanbul ignore next */ receiveError = <
|
|
243
|
-
E extends EndpointInterface & {
|
|
244
|
-
update?: EndpointUpdateFunction<E>;
|
|
245
|
-
},
|
|
246
|
-
>(
|
|
247
|
-
endpoint: E,
|
|
248
|
-
...rest: readonly [...Parameters<E>, Error]
|
|
249
|
-
): Promise<void> => {
|
|
250
|
-
/* istanbul ignore next */
|
|
251
|
-
return this.setError(endpoint, ...rest);
|
|
252
|
-
};
|
|
253
|
-
|
|
254
223
|
/**
|
|
255
224
|
* Resolves an inflight fetch. `fetchedAt` should `fetch`'s `createdAt`
|
|
256
|
-
* @see https://
|
|
225
|
+
* @see https://dataclient.io/docs/api/Controller#resolve
|
|
257
226
|
*/
|
|
258
227
|
resolve = <
|
|
259
228
|
E extends EndpointInterface & {
|
|
@@ -280,7 +249,7 @@ export default class Controller<
|
|
|
280
249
|
|
|
281
250
|
/**
|
|
282
251
|
* Marks a new subscription to a given Endpoint.
|
|
283
|
-
* @see https://
|
|
252
|
+
* @see https://dataclient.io/docs/api/Controller#subscribe
|
|
284
253
|
*/
|
|
285
254
|
subscribe = <
|
|
286
255
|
E extends EndpointInterface<
|
|
@@ -302,7 +271,7 @@ export default class Controller<
|
|
|
302
271
|
|
|
303
272
|
/**
|
|
304
273
|
* Marks completion of subscription to a given Endpoint.
|
|
305
|
-
* @see https://
|
|
274
|
+
* @see https://dataclient.io/docs/api/Controller#unsubscribe
|
|
306
275
|
*/
|
|
307
276
|
unsubscribe = <
|
|
308
277
|
E extends EndpointInterface<
|
|
@@ -332,8 +301,8 @@ export default class Controller<
|
|
|
332
301
|
*/
|
|
333
302
|
|
|
334
303
|
/**
|
|
335
|
-
* Gets a snapshot (https://
|
|
336
|
-
* @see https://
|
|
304
|
+
* Gets a snapshot (https://dataclient.io/docs/api/Snapshot)
|
|
305
|
+
* @see https://dataclient.io/docs/api/Controller#snapshot
|
|
337
306
|
*/
|
|
338
307
|
snapshot = (state: State<unknown>, fetchedAt?: number): SnapshotInterface => {
|
|
339
308
|
return new Snapshot(this, state, fetchedAt);
|
|
@@ -341,7 +310,7 @@ export default class Controller<
|
|
|
341
310
|
|
|
342
311
|
/**
|
|
343
312
|
* Gets the error, if any, for a given endpoint. Returns undefined for no errors.
|
|
344
|
-
* @see https://
|
|
313
|
+
* @see https://dataclient.io/docs/api/Controller#getError
|
|
345
314
|
*/
|
|
346
315
|
getError = <
|
|
347
316
|
E extends Pick<EndpointInterface, 'key'>,
|
|
@@ -366,7 +335,7 @@ export default class Controller<
|
|
|
366
335
|
|
|
367
336
|
/**
|
|
368
337
|
* Gets the (globally referentially stable) response for a given endpoint/args pair from state given.
|
|
369
|
-
* @see https://
|
|
338
|
+
* @see https://dataclient.io/docs/api/Controller#getResponse
|
|
370
339
|
*/
|
|
371
340
|
getResponse = <
|
|
372
341
|
E extends Pick<EndpointInterface, 'key' | 'schema' | 'invalidIfStale'>,
|
|
@@ -450,7 +419,7 @@ export default class Controller<
|
|
|
450
419
|
expiresAt = entityExpiresAt(paths, state.entityMeta);
|
|
451
420
|
}
|
|
452
421
|
|
|
453
|
-
// https://
|
|
422
|
+
// https://dataclient.io/docs/concepts/expiry-policy#expiry-status
|
|
454
423
|
// we don't track the difference between stale or fresh because that is tied to triggering
|
|
455
424
|
// conditions
|
|
456
425
|
const expiryStatus =
|
|
@@ -520,7 +489,7 @@ class Snapshot<T = unknown> implements SnapshotInterface {
|
|
|
520
489
|
}
|
|
521
490
|
|
|
522
491
|
/*************** Data Access ***************/
|
|
523
|
-
/** @see https://
|
|
492
|
+
/** @see https://dataclient.io/docs/api/Snapshot#getResponse */
|
|
524
493
|
getResponse = <
|
|
525
494
|
E extends Pick<EndpointInterface, 'key' | 'schema' | 'invalidIfStale'>,
|
|
526
495
|
Args extends readonly [...Parameters<E['key']>],
|
|
@@ -535,7 +504,7 @@ class Snapshot<T = unknown> implements SnapshotInterface {
|
|
|
535
504
|
return this.controller.getResponse(endpoint, ...args, this.state);
|
|
536
505
|
};
|
|
537
506
|
|
|
538
|
-
/** @see https://
|
|
507
|
+
/** @see https://dataclient.io/docs/api/Snapshot#getError */
|
|
539
508
|
getError = <
|
|
540
509
|
E extends Pick<EndpointInterface, 'key'>,
|
|
541
510
|
Args extends readonly [...Parameters<E['key']>],
|
package/src/index.ts
CHANGED
|
@@ -38,7 +38,7 @@ export type {
|
|
|
38
38
|
GenericDispatch,
|
|
39
39
|
} from './controller/Controller.js';
|
|
40
40
|
export { default as createFetch } from './controller/createFetch.js';
|
|
41
|
-
export { default as
|
|
41
|
+
export { default as createSet } from './controller/createSet.js';
|
|
42
42
|
|
|
43
43
|
export * from './controller/types.js';
|
|
44
44
|
export * as actionTypes from './actionTypes.js';
|
package/src/internal.ts
CHANGED
|
@@ -7,7 +7,7 @@ export type { DevToolsConfig };
|
|
|
7
7
|
|
|
8
8
|
const HASINTL = typeof Intl !== 'undefined';
|
|
9
9
|
const DEFAULT_CONFIG = {
|
|
10
|
-
name: `
|
|
10
|
+
name: `RDC: ${globalThis.document?.title}`,
|
|
11
11
|
autoPause: true,
|
|
12
12
|
serialize: {
|
|
13
13
|
options: undefined,
|
|
@@ -70,7 +70,7 @@ export default class DevToolsManager implements Manager {
|
|
|
70
70
|
action,
|
|
71
71
|
state.optimistic.reduce(reducer, state),
|
|
72
72
|
undefined,
|
|
73
|
-
'
|
|
73
|
+
'RDC',
|
|
74
74
|
);
|
|
75
75
|
});
|
|
76
76
|
return ret;
|
|
@@ -61,7 +61,7 @@ export default class NetworkManager implements Manager {
|
|
|
61
61
|
}
|
|
62
62
|
return Promise.resolve();
|
|
63
63
|
case SET_TYPE:
|
|
64
|
-
// only
|
|
64
|
+
// only set after new state is computed
|
|
65
65
|
return next(action).then(() => {
|
|
66
66
|
if (action.meta.key in this.fetched) {
|
|
67
67
|
// Note: meta *must* be set by reducer so this should be safe
|
|
@@ -69,7 +69,7 @@ export default class NetworkManager implements Manager {
|
|
|
69
69
|
controller.getState().meta[action.meta.key]?.error;
|
|
70
70
|
// processing errors result in state meta having error, so we should reject the promise
|
|
71
71
|
if (error) {
|
|
72
|
-
this.
|
|
72
|
+
this.handleSet(
|
|
73
73
|
createSet(action.endpoint, {
|
|
74
74
|
args: action.meta.args as any,
|
|
75
75
|
response: error,
|
|
@@ -78,7 +78,7 @@ export default class NetworkManager implements Manager {
|
|
|
78
78
|
}),
|
|
79
79
|
);
|
|
80
80
|
} else {
|
|
81
|
-
this.
|
|
81
|
+
this.handleSet(action);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
});
|
|
@@ -145,7 +145,7 @@ export default class NetworkManager implements Manager {
|
|
|
145
145
|
return this.controller.getState().lastReset;
|
|
146
146
|
}
|
|
147
147
|
|
|
148
|
-
/** Called when middleware intercepts '
|
|
148
|
+
/** Called when middleware intercepts 'rdc/fetch' action.
|
|
149
149
|
*
|
|
150
150
|
* Will then start a promise for a key and potentially start the network
|
|
151
151
|
* fetch.
|
|
@@ -171,7 +171,7 @@ export default class NetworkManager implements Manager {
|
|
|
171
171
|
reject(error);
|
|
172
172
|
throw error;
|
|
173
173
|
});
|
|
174
|
-
// schedule non-throttled resolutions in a microtask before
|
|
174
|
+
// schedule non-throttled resolutions in a microtask before set
|
|
175
175
|
// this enables users awaiting their fetch to trigger any react updates needed to deal
|
|
176
176
|
// with upcoming changes because of the fetch (for instance avoiding suspense if something is deleted)
|
|
177
177
|
if (!throttle) {
|
|
@@ -224,11 +224,11 @@ export default class NetworkManager implements Manager {
|
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
/** Called when middleware intercepts a
|
|
227
|
+
/** Called when middleware intercepts a set action.
|
|
228
228
|
*
|
|
229
|
-
* Will resolve the promise associated with
|
|
229
|
+
* Will resolve the promise associated with set key.
|
|
230
230
|
*/
|
|
231
|
-
protected
|
|
231
|
+
protected handleSet(action: SetAction) {
|
|
232
232
|
// this can still turn out to be untrue since this is async
|
|
233
233
|
if (action.meta.key in this.fetched) {
|
|
234
234
|
let promiseHandler: (value?: any) => void;
|
|
@@ -245,9 +245,9 @@ export default class NetworkManager implements Manager {
|
|
|
245
245
|
|
|
246
246
|
/** Attaches NetworkManager to store
|
|
247
247
|
*
|
|
248
|
-
* Intercepts '
|
|
248
|
+
* Intercepts 'rdc/fetch' actions to start requests.
|
|
249
249
|
*
|
|
250
|
-
* Resolve/rejects a request when matching '
|
|
250
|
+
* Resolve/rejects a request when matching 'rdc/set' event
|
|
251
251
|
* is seen.
|
|
252
252
|
*/
|
|
253
253
|
getMiddleware() {
|
|
@@ -25,7 +25,7 @@ export interface SubscriptionConstructable {
|
|
|
25
25
|
): Subscription;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
/** Handles subscription actions -> fetch or
|
|
28
|
+
/** Handles subscription actions -> fetch or set actions
|
|
29
29
|
*
|
|
30
30
|
* Constructor takes a SubscriptionConstructable class to control how
|
|
31
31
|
* subscriptions are handled. (e.g., polling, websockets)
|
|
@@ -75,7 +75,7 @@ export default class SubscriptionManager<S extends SubscriptionConstructable>
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
/** Called when middleware intercepts '
|
|
78
|
+
/** Called when middleware intercepts 'rdc/subscribe' action.
|
|
79
79
|
*
|
|
80
80
|
*/
|
|
81
81
|
protected handleSubscribe(action: SubscribeAction) {
|
|
@@ -92,7 +92,7 @@ export default class SubscriptionManager<S extends SubscriptionConstructable>
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
/** Called when middleware intercepts '
|
|
95
|
+
/** Called when middleware intercepts 'rdc/unsubscribe' action.
|
|
96
96
|
*
|
|
97
97
|
*/
|
|
98
98
|
protected handleUnsubscribe(action: UnsubscribeAction) {
|
|
@@ -112,10 +112,10 @@ export default class SubscriptionManager<S extends SubscriptionConstructable>
|
|
|
112
112
|
|
|
113
113
|
/** Attaches Manager to store
|
|
114
114
|
*
|
|
115
|
-
* Intercepts '
|
|
115
|
+
* Intercepts 'rdc/subscribe'/'rest-hordc/ribe' to register resources that
|
|
116
116
|
* need to be kept up to date.
|
|
117
117
|
*
|
|
118
|
-
* Will possibly dispatch '
|
|
118
|
+
* Will possibly dispatch 'rdc/fetch' or 'rest-hordc/' to keep resources fresh
|
|
119
119
|
*
|
|
120
120
|
*/
|
|
121
121
|
getMiddleware() {
|
|
@@ -20,7 +20,7 @@ exports[`PollingSubscription fresh data should call after period 1`] = `
|
|
|
20
20
|
"throttle": true,
|
|
21
21
|
},
|
|
22
22
|
"payload": [Function],
|
|
23
|
-
"type": "
|
|
23
|
+
"type": "rdc/fetch",
|
|
24
24
|
},
|
|
25
25
|
]
|
|
26
26
|
`;
|
|
@@ -39,7 +39,7 @@ exports[`PollingSubscription fresh data should call after period 2`] = `
|
|
|
39
39
|
"throttle": true,
|
|
40
40
|
},
|
|
41
41
|
"payload": [Function],
|
|
42
|
-
"type": "
|
|
42
|
+
"type": "rdc/fetch",
|
|
43
43
|
},
|
|
44
44
|
]
|
|
45
45
|
`;
|
|
@@ -43,7 +43,7 @@ describe('LogoutManager', () => {
|
|
|
43
43
|
controller: { value: controller },
|
|
44
44
|
},
|
|
45
45
|
);
|
|
46
|
-
it('should ignore non-error
|
|
46
|
+
it('should ignore non-error set', async () => {
|
|
47
47
|
const action = createSet(CoolerArticleResource.get, {
|
|
48
48
|
args: [{ id: 5 }],
|
|
49
49
|
response: { id: 5, title: 'hi' },
|
|
@@ -52,7 +52,7 @@ describe('LogoutManager', () => {
|
|
|
52
52
|
|
|
53
53
|
expect(dispatch.mock.calls.length).toBe(0);
|
|
54
54
|
});
|
|
55
|
-
it('should ignore non-401
|
|
55
|
+
it('should ignore non-401 set', async () => {
|
|
56
56
|
const error: any = new Error('network failed');
|
|
57
57
|
error.status = 404;
|
|
58
58
|
const action = createSet(CoolerArticleResource.get, {
|
|
@@ -4,7 +4,7 @@ import { ActionTypes } from '../../types';
|
|
|
4
4
|
import NetworkManager from '../NetworkManager';
|
|
5
5
|
|
|
6
6
|
const middleware: Middleware = new NetworkManager().getMiddleware();
|
|
7
|
-
it('middlewares should compose with non-
|
|
7
|
+
it('middlewares should compose with non-data-client middlewares', () => {
|
|
8
8
|
type AnotherAction = {
|
|
9
9
|
type: 'BOB';
|
|
10
10
|
payload: any;
|
|
@@ -71,12 +71,9 @@ describe('NetworkManager', () => {
|
|
|
71
71
|
return updates;
|
|
72
72
|
},
|
|
73
73
|
});
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
args: [{ id: 5 }],
|
|
78
|
-
},
|
|
79
|
-
);
|
|
74
|
+
const fetchSetWithUpdatersAction = createFetch(detailWithUpdaterEndpoint, {
|
|
75
|
+
args: [{ id: 5 }],
|
|
76
|
+
});
|
|
80
77
|
|
|
81
78
|
const updateShape = new Endpoint(
|
|
82
79
|
(params: any, body: any) => Promise.resolve(body),
|
|
@@ -158,7 +155,7 @@ describe('NetworkManager', () => {
|
|
|
158
155
|
|
|
159
156
|
const data = await fetchResolveAction.payload();
|
|
160
157
|
|
|
161
|
-
// mutations resolve before dispatch, so we must wait for next tick to see
|
|
158
|
+
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
162
159
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
163
160
|
|
|
164
161
|
const action = {
|
|
@@ -176,7 +173,7 @@ describe('NetworkManager', () => {
|
|
|
176
173
|
expect(dispatch).toHaveBeenCalledWith(action);
|
|
177
174
|
expect(next).not.toHaveBeenCalledWith(action);
|
|
178
175
|
});
|
|
179
|
-
it('should handle fetch
|
|
176
|
+
it('should handle fetch set action and dispatch on success with updaters', async () => {
|
|
180
177
|
const next = jest.fn();
|
|
181
178
|
const dispatch = jest.fn();
|
|
182
179
|
const controller = new Controller({ dispatch, getState });
|
|
@@ -187,20 +184,20 @@ describe('NetworkManager', () => {
|
|
|
187
184
|
},
|
|
188
185
|
);
|
|
189
186
|
|
|
190
|
-
middleware(API)(next)(
|
|
187
|
+
middleware(API)(next)(fetchSetWithUpdatersAction);
|
|
191
188
|
|
|
192
|
-
const data = await
|
|
189
|
+
const data = await fetchSetWithUpdatersAction.payload();
|
|
193
190
|
|
|
194
|
-
// mutations resolve before dispatch, so we must wait for next tick to see
|
|
191
|
+
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
195
192
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
196
193
|
|
|
197
194
|
const action = {
|
|
198
195
|
type: SET_TYPE,
|
|
199
|
-
endpoint:
|
|
196
|
+
endpoint: fetchSetWithUpdatersAction.endpoint,
|
|
200
197
|
payload: data,
|
|
201
198
|
meta: {
|
|
202
|
-
args:
|
|
203
|
-
key:
|
|
199
|
+
args: fetchSetWithUpdatersAction.meta.args,
|
|
200
|
+
key: fetchSetWithUpdatersAction.meta.key,
|
|
204
201
|
date: expect.any(Number),
|
|
205
202
|
expiresAt: expect.any(Number),
|
|
206
203
|
fetchedAt: expect.any(Number),
|
|
@@ -224,7 +221,7 @@ describe('NetworkManager', () => {
|
|
|
224
221
|
|
|
225
222
|
const data = await fetchRpcWithUpdatersAction.payload();
|
|
226
223
|
|
|
227
|
-
// mutations resolve before dispatch, so we must wait for next tick to see
|
|
224
|
+
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
228
225
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
229
226
|
|
|
230
227
|
const action = {
|
|
@@ -258,7 +255,7 @@ describe('NetworkManager', () => {
|
|
|
258
255
|
const data = await fetchRpcWithUpdatersAndOptimisticAction.payload();
|
|
259
256
|
|
|
260
257
|
expect(next).toHaveBeenCalled();
|
|
261
|
-
// mutations resolve before dispatch, so we must wait for next tick to see
|
|
258
|
+
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
262
259
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
263
260
|
expect(dispatch).toHaveBeenCalledWith({
|
|
264
261
|
type: SET_TYPE,
|
|
@@ -8,16 +8,11 @@ export default function applyManager(
|
|
|
8
8
|
): Middleware[] {
|
|
9
9
|
return managers.map(manager => {
|
|
10
10
|
const middleware = manager.getMiddleware();
|
|
11
|
-
// TODO(breaking): remove this once controller prop is no longer supported
|
|
12
11
|
return ({ dispatch, getState }) => {
|
|
13
12
|
(controller as any).dispatch = dispatch;
|
|
14
13
|
(controller as any).getState = getState;
|
|
15
|
-
// this is needed for backwards compatibility as we added 'controller' prop previously
|
|
16
|
-
const API = Object.create(controller, {
|
|
17
|
-
controller: { value: controller },
|
|
18
|
-
});
|
|
19
14
|
// controller is a superset of the middleware API
|
|
20
|
-
return middleware(
|
|
15
|
+
return middleware(controller as Controller<any>);
|
|
21
16
|
};
|
|
22
17
|
});
|
|
23
18
|
}
|
package/src/middlewareTypes.ts
CHANGED
|
@@ -3,15 +3,10 @@ import { ActionTypes, State } from './types.js';
|
|
|
3
3
|
|
|
4
4
|
type RHDispatch<Actions = any> = (value: Actions) => Promise<void>;
|
|
5
5
|
|
|
6
|
-
export interface MiddlewareAPI<R extends
|
|
7
|
-
extends Controller<RHDispatch<ActionTypes>> {
|
|
8
|
-
/** @deprecated use members directly instead */
|
|
9
|
-
controller: Controller<RHDispatch<ActionTypes>>;
|
|
10
|
-
}
|
|
6
|
+
export interface MiddlewareAPI<R extends DataClientReducer = DataClientReducer>
|
|
7
|
+
extends Controller<RHDispatch<ActionTypes>> {}
|
|
11
8
|
export interface MiddlewareController<Actions = ActionTypes>
|
|
12
|
-
extends Controller<RHDispatch<Actions>> {
|
|
13
|
-
controller: Controller<RHDispatch<Actions>>;
|
|
14
|
-
}
|
|
9
|
+
extends Controller<RHDispatch<Actions>> {}
|
|
15
10
|
|
|
16
11
|
export type Middleware<Actions = any> = <
|
|
17
12
|
C extends MiddlewareController<Actions>,
|
|
@@ -19,7 +14,7 @@ export type Middleware<Actions = any> = <
|
|
|
19
14
|
controller: C,
|
|
20
15
|
) => (next: C['dispatch']) => C['dispatch'];
|
|
21
16
|
|
|
22
|
-
export type
|
|
17
|
+
export type DataClientReducer = (
|
|
23
18
|
prevState: State<unknown>,
|
|
24
19
|
action: ActionTypes,
|
|
25
20
|
) => State<unknown>;
|
package/src/next/index.ts
CHANGED