@data-client/core 0.14.10 → 0.14.16
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/README.md +7 -1
- package/data_client_logo_and_text.svg +9 -8
- package/dist/index.js +62 -40
- package/dist/index.umd.min.js +1 -1
- package/legacy/actionTypes.js +23 -12
- package/legacy/actions.js +1 -1
- package/legacy/controller/actions/createExpireAll.js +3 -3
- package/legacy/controller/actions/createFetch.js +3 -3
- package/legacy/controller/actions/createInvalidate.js +3 -3
- package/legacy/controller/actions/createInvalidateAll.js +3 -3
- package/legacy/controller/actions/createOptimistic.js +3 -3
- package/legacy/controller/actions/createReset.js +3 -3
- package/legacy/controller/actions/createSet.js +3 -3
- package/legacy/controller/actions/createSetResponse.js +3 -3
- package/legacy/controller/actions/createSubscription.js +4 -4
- package/legacy/index.js +2 -1
- package/legacy/manager/LogoutManager.js +3 -3
- package/legacy/manager/NetworkManager.js +6 -6
- package/legacy/manager/SubscriptionManager.js +4 -4
- package/legacy/state/reducer/createReducer.js +11 -11
- package/legacy/state/reducer/invalidateReducer.js +3 -3
- package/legacy/state/reducer/setResponseReducer.js +4 -4
- package/lib/actionTypes.d.ts +11 -0
- package/lib/actionTypes.d.ts.map +1 -1
- package/lib/actionTypes.js +23 -12
- package/lib/actions.d.ts +13 -12
- package/lib/actions.d.ts.map +1 -1
- package/lib/actions.js +1 -1
- package/lib/controller/Controller.d.ts +2 -2
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/actions/createExpireAll.js +3 -3
- package/lib/controller/actions/createFetch.js +3 -3
- package/lib/controller/actions/createInvalidate.js +3 -3
- package/lib/controller/actions/createInvalidateAll.js +3 -3
- package/lib/controller/actions/createOptimistic.js +3 -3
- package/lib/controller/actions/createReset.js +3 -3
- package/lib/controller/actions/createSet.js +3 -3
- package/lib/controller/actions/createSetResponse.js +3 -3
- package/lib/controller/actions/createSubscription.js +4 -4
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/manager/LogoutManager.js +3 -3
- package/lib/manager/NetworkManager.js +6 -6
- package/lib/manager/SubscriptionManager.js +4 -4
- package/lib/state/reducer/createReducer.js +11 -11
- package/lib/state/reducer/invalidateReducer.js +3 -3
- package/lib/state/reducer/setResponseReducer.js +4 -4
- package/package.json +2 -2
- package/src/actionTypes.ts +23 -11
- package/src/actions.ts +23 -22
- package/src/controller/actions/createExpireAll.ts +2 -2
- package/src/controller/actions/createFetch.ts +2 -2
- package/src/controller/actions/createInvalidate.ts +2 -2
- package/src/controller/actions/createInvalidateAll.ts +2 -2
- package/src/controller/actions/createOptimistic.ts +2 -2
- package/src/controller/actions/createReset.ts +2 -2
- package/src/controller/actions/createSet.ts +2 -2
- package/src/controller/actions/createSetResponse.ts +2 -2
- package/src/controller/actions/createSubscription.ts +3 -3
- package/src/index.ts +1 -0
- package/src/manager/LogoutManager.ts +2 -2
- package/src/manager/NetworkManager.ts +5 -5
- package/src/manager/SubscriptionManager.ts +3 -3
- package/src/manager/__tests__/logoutManager.ts +3 -3
- package/src/manager/__tests__/networkManager.ts +6 -6
- package/src/manager/__tests__/subscriptionManager.ts +4 -4
- package/src/state/__tests__/reducer.ts +22 -22
- package/src/state/reducer/createReducer.ts +18 -18
- package/src/state/reducer/invalidateReducer.ts +2 -2
- package/src/state/reducer/setResponseReducer.ts +3 -3
- package/ts3.4/actionTypes.d.ts +11 -0
- package/ts3.4/actions.d.ts +13 -12
- package/ts3.4/controller/Controller.d.ts +2 -2
- package/ts3.4/index.d.ts +1 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INVALIDATEALL } from '../../actionTypes.js';
|
|
2
2
|
import type { InvalidateAllAction } from '../../types.js';
|
|
3
3
|
|
|
4
4
|
export function createInvalidateAll(
|
|
5
5
|
testKey: (key: string) => boolean,
|
|
6
6
|
): InvalidateAllAction {
|
|
7
7
|
return {
|
|
8
|
-
type:
|
|
8
|
+
type: INVALIDATEALL,
|
|
9
9
|
testKey,
|
|
10
10
|
};
|
|
11
11
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EndpointInterface } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
3
|
import { createMeta } from './createMeta.js';
|
|
4
|
-
import {
|
|
4
|
+
import { OPTIMISTIC } from '../../actionTypes.js';
|
|
5
5
|
import type { OptimisticAction } from '../../types.js';
|
|
6
6
|
import type { EndpointUpdateFunction } from '../types.js';
|
|
7
7
|
|
|
@@ -23,7 +23,7 @@ export function createOptimistic<
|
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
return {
|
|
26
|
-
type:
|
|
26
|
+
type: OPTIMISTIC,
|
|
27
27
|
key: endpoint.key(...args),
|
|
28
28
|
args,
|
|
29
29
|
endpoint,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RESET } from '../../actionTypes.js';
|
|
2
2
|
import type { ResetAction } from '../../types.js';
|
|
3
3
|
|
|
4
4
|
export function createReset(): ResetAction {
|
|
5
5
|
return {
|
|
6
|
-
type:
|
|
6
|
+
type: RESET,
|
|
7
7
|
date: Date.now(),
|
|
8
8
|
};
|
|
9
9
|
}
|
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
} from '@data-client/normalizr';
|
|
6
6
|
|
|
7
7
|
import { createMeta } from './createMeta.js';
|
|
8
|
-
import {
|
|
8
|
+
import { SET } from '../../actionTypes.js';
|
|
9
9
|
import type { SetAction } from '../../types.js';
|
|
10
10
|
import ensurePojo from '../ensurePojo.js';
|
|
11
11
|
|
|
@@ -22,7 +22,7 @@ export function createSet<S extends Queryable>(
|
|
|
22
22
|
},
|
|
23
23
|
): SetAction<S> {
|
|
24
24
|
return {
|
|
25
|
-
type:
|
|
25
|
+
type: SET,
|
|
26
26
|
value,
|
|
27
27
|
args: args.map(ensurePojo) as SchemaArgs<S>,
|
|
28
28
|
schema,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EndpointInterface, ResolveType } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
3
|
import { createMeta } from './createMeta.js';
|
|
4
|
-
import {
|
|
4
|
+
import { SET_RESPONSE } from '../../actionTypes.js';
|
|
5
5
|
import type { SetResponseAction } from '../../types.js';
|
|
6
6
|
import ensurePojo from '../ensurePojo.js';
|
|
7
7
|
import { EndpointUpdateFunction } from '../types.js';
|
|
@@ -62,7 +62,7 @@ export function createSetResponse<
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
return {
|
|
65
|
-
type:
|
|
65
|
+
type: SET_RESPONSE,
|
|
66
66
|
key: endpoint.key(...args),
|
|
67
67
|
response,
|
|
68
68
|
args: args.map(ensurePojo),
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { EndpointInterface } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { SUBSCRIBE, UNSUBSCRIBE } from '../../actionTypes.js';
|
|
4
4
|
import type { SubscribeAction, UnsubscribeAction } from '../../types.js';
|
|
5
5
|
|
|
6
6
|
export function createSubscription<E extends EndpointInterface>(
|
|
@@ -8,7 +8,7 @@ export function createSubscription<E extends EndpointInterface>(
|
|
|
8
8
|
{ args }: { args: readonly [...Parameters<E>] },
|
|
9
9
|
): SubscribeAction<E> {
|
|
10
10
|
return {
|
|
11
|
-
type:
|
|
11
|
+
type: SUBSCRIBE,
|
|
12
12
|
endpoint,
|
|
13
13
|
args,
|
|
14
14
|
key: endpoint.key(...args),
|
|
@@ -20,7 +20,7 @@ export function createUnsubscription<E extends EndpointInterface>(
|
|
|
20
20
|
{ args }: { args: readonly [...Parameters<E>] },
|
|
21
21
|
): UnsubscribeAction<E> {
|
|
22
22
|
return {
|
|
23
|
-
type:
|
|
23
|
+
type: UNSUBSCRIBE,
|
|
24
24
|
key: endpoint.key(...args),
|
|
25
25
|
args,
|
|
26
26
|
endpoint,
|
package/src/index.ts
CHANGED
|
@@ -43,6 +43,7 @@ export type {
|
|
|
43
43
|
export * as actions from './controller/actions/index.js';
|
|
44
44
|
|
|
45
45
|
export * from './controller/types.js';
|
|
46
|
+
/** @see https://dataclient.io/docs/api/Actions */
|
|
46
47
|
export * as actionTypes from './actionTypes.js';
|
|
47
48
|
/* istanbul ignore next */
|
|
48
49
|
export * from './types.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SET_RESPONSE } from '../actionTypes.js';
|
|
2
2
|
import type Controller from '../controller/Controller.js';
|
|
3
3
|
import { UnknownError } from '../index.js';
|
|
4
4
|
import type { Manager, Middleware } from '../types.js';
|
|
@@ -16,7 +16,7 @@ export default class LogoutManager implements Manager {
|
|
|
16
16
|
middleware: Middleware = controller => next => async action => {
|
|
17
17
|
await next(action);
|
|
18
18
|
if (
|
|
19
|
-
action.type ===
|
|
19
|
+
action.type === SET_RESPONSE &&
|
|
20
20
|
action.error &&
|
|
21
21
|
this.shouldLogout(action.response)
|
|
22
22
|
) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SET_RESPONSE, FETCH, RESET } from '../actionTypes.js';
|
|
2
2
|
import { createSetResponse } from '../controller/actions/index.js';
|
|
3
3
|
import Controller from '../controller/Controller.js';
|
|
4
4
|
import type {
|
|
@@ -46,7 +46,7 @@ export default class NetworkManager implements Manager {
|
|
|
46
46
|
this.controller = controller;
|
|
47
47
|
return next => action => {
|
|
48
48
|
switch (action.type) {
|
|
49
|
-
case
|
|
49
|
+
case FETCH:
|
|
50
50
|
this.handleFetch(action);
|
|
51
51
|
// This is the only case that causes any state change
|
|
52
52
|
// It's important to intercept other fetches as we don't want to trigger reducers during
|
|
@@ -58,7 +58,7 @@ export default class NetworkManager implements Manager {
|
|
|
58
58
|
return next(action);
|
|
59
59
|
}
|
|
60
60
|
return Promise.resolve();
|
|
61
|
-
case
|
|
61
|
+
case SET_RESPONSE:
|
|
62
62
|
// only set after new state is computed
|
|
63
63
|
return next(action).then(() => {
|
|
64
64
|
if (action.key in this.fetched) {
|
|
@@ -79,7 +79,7 @@ export default class NetworkManager implements Manager {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
});
|
|
82
|
-
case
|
|
82
|
+
case RESET: {
|
|
83
83
|
const rejectors = { ...this.rejectors };
|
|
84
84
|
|
|
85
85
|
this.clearAll();
|
|
@@ -112,7 +112,7 @@ export default class NetworkManager implements Manager {
|
|
|
112
112
|
/** Used by DevtoolsManager to determine whether to log an action */
|
|
113
113
|
skipLogging(action: ActionTypes) {
|
|
114
114
|
/* istanbul ignore next */
|
|
115
|
-
return action.type ===
|
|
115
|
+
return action.type === FETCH && action.key in this.fetched;
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
allSettled() {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SUBSCRIBE, UNSUBSCRIBE } from '../actionTypes.js';
|
|
2
2
|
import Controller from '../controller/Controller.js';
|
|
3
3
|
import type {
|
|
4
4
|
Manager,
|
|
@@ -51,14 +51,14 @@ export default class SubscriptionManager<
|
|
|
51
51
|
this.controller = controller;
|
|
52
52
|
return next => action => {
|
|
53
53
|
switch (action.type) {
|
|
54
|
-
case
|
|
54
|
+
case SUBSCRIBE:
|
|
55
55
|
try {
|
|
56
56
|
this.handleSubscribe(action);
|
|
57
57
|
} catch (e) {
|
|
58
58
|
console.error(e);
|
|
59
59
|
}
|
|
60
60
|
return Promise.resolve();
|
|
61
|
-
case
|
|
61
|
+
case UNSUBSCRIBE:
|
|
62
62
|
this.handleUnsubscribe(action);
|
|
63
63
|
return Promise.resolve();
|
|
64
64
|
default:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CoolerArticleResource } from '__tests__/new';
|
|
2
2
|
|
|
3
3
|
import { Controller, initialState } from '../..';
|
|
4
|
-
import {
|
|
4
|
+
import { FETCH, RESET } from '../../actionTypes';
|
|
5
5
|
import { createSetResponse } from '../../controller/actions';
|
|
6
6
|
import LogoutManager from '../LogoutManager.js';
|
|
7
7
|
|
|
@@ -73,7 +73,7 @@ describe('LogoutManager', () => {
|
|
|
73
73
|
await manager.middleware(API)(next)(action);
|
|
74
74
|
|
|
75
75
|
expect(dispatch.mock.calls.length).toBe(1);
|
|
76
|
-
expect(dispatch.mock.calls[0][0]?.type).toBe(
|
|
76
|
+
expect(dispatch.mock.calls[0][0]?.type).toBe(RESET);
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
it('should call custom handleLogout', async () => {
|
|
@@ -97,7 +97,7 @@ describe('LogoutManager', () => {
|
|
|
97
97
|
});
|
|
98
98
|
|
|
99
99
|
it('should let other actions pass through', async () => {
|
|
100
|
-
const action = { type:
|
|
100
|
+
const action = { type: FETCH };
|
|
101
101
|
next.mockReset();
|
|
102
102
|
|
|
103
103
|
await manager.middleware(API)(next)(action as any);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Endpoint } from '@data-client/endpoint';
|
|
2
2
|
import { Article, ArticleResource } from '__tests__/new';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { SET_RESPONSE } from '../../actionTypes';
|
|
5
5
|
import { createFetch } from '../../controller/actions';
|
|
6
6
|
import Controller from '../../controller/Controller';
|
|
7
7
|
import NetworkManager from '../../manager/NetworkManager';
|
|
@@ -159,7 +159,7 @@ describe('NetworkManager', () => {
|
|
|
159
159
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
160
160
|
|
|
161
161
|
const action: SetResponseAction = {
|
|
162
|
-
type:
|
|
162
|
+
type: SET_RESPONSE,
|
|
163
163
|
endpoint: fetchResolveAction.endpoint,
|
|
164
164
|
response,
|
|
165
165
|
args: fetchResolveAction.args,
|
|
@@ -195,7 +195,7 @@ describe('NetworkManager', () => {
|
|
|
195
195
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
196
196
|
|
|
197
197
|
const action: SetResponseAction = {
|
|
198
|
-
type:
|
|
198
|
+
type: SET_RESPONSE,
|
|
199
199
|
endpoint: fetchSetWithUpdatersAction.endpoint,
|
|
200
200
|
response,
|
|
201
201
|
args: fetchSetWithUpdatersAction.args,
|
|
@@ -231,7 +231,7 @@ describe('NetworkManager', () => {
|
|
|
231
231
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
232
232
|
|
|
233
233
|
const action: SetResponseAction = {
|
|
234
|
-
type:
|
|
234
|
+
type: SET_RESPONSE,
|
|
235
235
|
endpoint: fetchRpcWithUpdatersAction.endpoint,
|
|
236
236
|
response,
|
|
237
237
|
args: fetchRpcWithUpdatersAction.args,
|
|
@@ -267,7 +267,7 @@ describe('NetworkManager', () => {
|
|
|
267
267
|
// mutations resolve before dispatch, so we must wait for next tick to see set
|
|
268
268
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
269
269
|
expect(dispatch).toHaveBeenCalledWith({
|
|
270
|
-
type:
|
|
270
|
+
type: SET_RESPONSE,
|
|
271
271
|
endpoint: fetchRpcWithUpdatersAndOptimisticAction.endpoint,
|
|
272
272
|
response,
|
|
273
273
|
args: fetchRpcWithUpdatersAndOptimisticAction.args,
|
|
@@ -338,7 +338,7 @@ describe('NetworkManager', () => {
|
|
|
338
338
|
} catch (error) {
|
|
339
339
|
expect(next).not.toHaveBeenCalled();
|
|
340
340
|
expect(dispatch).toHaveBeenCalledWith({
|
|
341
|
-
type:
|
|
341
|
+
type: SET_RESPONSE,
|
|
342
342
|
response: error,
|
|
343
343
|
key: fetchRejectAction.key,
|
|
344
344
|
meta: {
|
|
@@ -4,7 +4,7 @@ import { actionTypes, Controller, initialState } from '../..';
|
|
|
4
4
|
import { SubscribeAction, UnsubscribeAction } from '../../types';
|
|
5
5
|
import SubscriptionManager, { Subscription } from '../SubscriptionManager.js';
|
|
6
6
|
|
|
7
|
-
const {
|
|
7
|
+
const { UNSUBSCRIBE, SUBSCRIBE, SET_RESPONSE } = actionTypes;
|
|
8
8
|
|
|
9
9
|
function onError(e: any) {
|
|
10
10
|
e.preventDefault();
|
|
@@ -55,7 +55,7 @@ describe('SubscriptionManager', () => {
|
|
|
55
55
|
() => Promise.reject(new Error('Failed'))
|
|
56
56
|
: () => Promise.resolve(response);
|
|
57
57
|
return {
|
|
58
|
-
type:
|
|
58
|
+
type: SUBSCRIBE,
|
|
59
59
|
endpoint: PollingArticleResource.get.extend({ fetch }),
|
|
60
60
|
args: [{ id: response.id }],
|
|
61
61
|
key: PollingArticleResource.get.key({ id: response.id }),
|
|
@@ -65,7 +65,7 @@ describe('SubscriptionManager', () => {
|
|
|
65
65
|
response: Record<string, any>,
|
|
66
66
|
): UnsubscribeAction {
|
|
67
67
|
return {
|
|
68
|
-
type:
|
|
68
|
+
type: UNSUBSCRIBE,
|
|
69
69
|
endpoint: PollingArticleResource.get,
|
|
70
70
|
key: PollingArticleResource.get.key({ id: response.id }),
|
|
71
71
|
args: [{ id: response.id }],
|
|
@@ -185,7 +185,7 @@ describe('SubscriptionManager', () => {
|
|
|
185
185
|
});
|
|
186
186
|
|
|
187
187
|
it('should let other actions pass through', () => {
|
|
188
|
-
const action = { type:
|
|
188
|
+
const action = { type: SET_RESPONSE };
|
|
189
189
|
next.mockReset();
|
|
190
190
|
|
|
191
191
|
manager.middleware(API)(next)(action as any);
|
|
@@ -3,12 +3,12 @@ import { ArticleResource, Article, PaginatedArticle } from '__tests__/new';
|
|
|
3
3
|
|
|
4
4
|
import { Controller } from '../..';
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
INVALIDATE,
|
|
7
|
+
FETCH,
|
|
8
|
+
RESET,
|
|
9
|
+
GC,
|
|
10
|
+
SET_RESPONSE,
|
|
11
|
+
SET,
|
|
12
12
|
} from '../../actionTypes';
|
|
13
13
|
import {
|
|
14
14
|
State,
|
|
@@ -36,7 +36,7 @@ describe('reducer', () => {
|
|
|
36
36
|
const id = 20;
|
|
37
37
|
const response = { id, title: 'hi', content: 'this is the content' };
|
|
38
38
|
const action: SetResponseAction = {
|
|
39
|
-
type:
|
|
39
|
+
type: SET_RESPONSE,
|
|
40
40
|
response,
|
|
41
41
|
endpoint: ArticleResource.get,
|
|
42
42
|
args: [{ id }],
|
|
@@ -215,7 +215,7 @@ describe('reducer', () => {
|
|
|
215
215
|
static key = 'Counter';
|
|
216
216
|
}
|
|
217
217
|
const action: SetAction = {
|
|
218
|
-
type:
|
|
218
|
+
type: SET,
|
|
219
219
|
value,
|
|
220
220
|
schema: Counter,
|
|
221
221
|
args: [{ id }],
|
|
@@ -242,7 +242,7 @@ describe('reducer', () => {
|
|
|
242
242
|
static key = 'Counter';
|
|
243
243
|
}
|
|
244
244
|
const action: SetAction = {
|
|
245
|
-
type:
|
|
245
|
+
type: SET,
|
|
246
246
|
value,
|
|
247
247
|
schema: Counter,
|
|
248
248
|
args: [{ id }],
|
|
@@ -270,7 +270,7 @@ describe('reducer', () => {
|
|
|
270
270
|
const id = 20;
|
|
271
271
|
const value = { id, title: 'hi', content: 'this is the content' };
|
|
272
272
|
const action: SetAction = {
|
|
273
|
-
type:
|
|
273
|
+
type: SET,
|
|
274
274
|
value,
|
|
275
275
|
schema: Article,
|
|
276
276
|
args: [{ id }],
|
|
@@ -292,7 +292,7 @@ describe('reducer', () => {
|
|
|
292
292
|
const id = 20;
|
|
293
293
|
const value = { id, title: 'hi', content: 'this is the content' };
|
|
294
294
|
const action: SetAction = {
|
|
295
|
-
type:
|
|
295
|
+
type: SET,
|
|
296
296
|
value,
|
|
297
297
|
schema: Article,
|
|
298
298
|
args: [{ id }],
|
|
@@ -314,7 +314,7 @@ describe('reducer', () => {
|
|
|
314
314
|
const id = 20;
|
|
315
315
|
const response = { id, title: 'hi', content: 'this is the content' };
|
|
316
316
|
const action: SetResponseAction = {
|
|
317
|
-
type:
|
|
317
|
+
type: SET_RESPONSE,
|
|
318
318
|
response,
|
|
319
319
|
endpoint: ArticleResource.get,
|
|
320
320
|
args: [{ id }],
|
|
@@ -335,7 +335,7 @@ describe('reducer', () => {
|
|
|
335
335
|
it('purge should delete entities', () => {
|
|
336
336
|
const id = 20;
|
|
337
337
|
const action: SetResponseAction = {
|
|
338
|
-
type:
|
|
338
|
+
type: SET_RESPONSE,
|
|
339
339
|
response: { id },
|
|
340
340
|
endpoint: ArticleResource.delete,
|
|
341
341
|
args: [{ id }],
|
|
@@ -485,7 +485,7 @@ describe('reducer', () => {
|
|
|
485
485
|
it('invalidates resources correctly', () => {
|
|
486
486
|
const id = 20;
|
|
487
487
|
const action: InvalidateAction = {
|
|
488
|
-
type:
|
|
488
|
+
type: INVALIDATE,
|
|
489
489
|
key: id.toString(),
|
|
490
490
|
};
|
|
491
491
|
const iniState: any = {
|
|
@@ -522,7 +522,7 @@ describe('reducer', () => {
|
|
|
522
522
|
const id = 20;
|
|
523
523
|
const error = new Error('hi');
|
|
524
524
|
const action: SetResponseAction = {
|
|
525
|
-
type:
|
|
525
|
+
type: SET_RESPONSE,
|
|
526
526
|
response: error,
|
|
527
527
|
endpoint: ArticleResource.get,
|
|
528
528
|
args: [{ id }],
|
|
@@ -542,7 +542,7 @@ describe('reducer', () => {
|
|
|
542
542
|
const id = 20;
|
|
543
543
|
const error = new Error('hi');
|
|
544
544
|
const action: SetResponseAction = {
|
|
545
|
-
type:
|
|
545
|
+
type: SET_RESPONSE,
|
|
546
546
|
response: error,
|
|
547
547
|
endpoint: ArticleResource.get,
|
|
548
548
|
args: [{ id }],
|
|
@@ -563,7 +563,7 @@ describe('reducer', () => {
|
|
|
563
563
|
const id = 20;
|
|
564
564
|
const error = new Error('hi');
|
|
565
565
|
const action: SetResponseAction = {
|
|
566
|
-
type:
|
|
566
|
+
type: SET_RESPONSE,
|
|
567
567
|
response: error,
|
|
568
568
|
endpoint: ArticleResource.delete,
|
|
569
569
|
args: [{ id }],
|
|
@@ -595,7 +595,7 @@ describe('reducer', () => {
|
|
|
595
595
|
.mockImplementation(() => {});
|
|
596
596
|
try {
|
|
597
597
|
const action: FetchAction = {
|
|
598
|
-
type:
|
|
598
|
+
type: FETCH,
|
|
599
599
|
endpoint: ArticleResource.get,
|
|
600
600
|
args: [{ id: 5 }],
|
|
601
601
|
key: ArticleResource.get.url({ id: 5 }),
|
|
@@ -640,7 +640,7 @@ describe('reducer', () => {
|
|
|
640
640
|
|
|
641
641
|
it('reset should delete all entries', () => {
|
|
642
642
|
const action: ResetAction = {
|
|
643
|
-
type:
|
|
643
|
+
type: RESET,
|
|
644
644
|
date: Date.now(),
|
|
645
645
|
};
|
|
646
646
|
const iniState: any = {
|
|
@@ -697,7 +697,7 @@ describe('reducer', () => {
|
|
|
697
697
|
|
|
698
698
|
it('empty targets should do nothing', () => {
|
|
699
699
|
const action: GCAction = {
|
|
700
|
-
type:
|
|
700
|
+
type: GC,
|
|
701
701
|
entities: [],
|
|
702
702
|
endpoints: [],
|
|
703
703
|
};
|
|
@@ -710,7 +710,7 @@ describe('reducer', () => {
|
|
|
710
710
|
|
|
711
711
|
it('empty deleting entities should work', () => {
|
|
712
712
|
const action: GCAction = {
|
|
713
|
-
type:
|
|
713
|
+
type: GC,
|
|
714
714
|
entities: [
|
|
715
715
|
[Article.key, '10'],
|
|
716
716
|
[Article.key, '250'],
|
|
@@ -729,7 +729,7 @@ describe('reducer', () => {
|
|
|
729
729
|
|
|
730
730
|
it('empty deleting nonexistant things should passthrough', () => {
|
|
731
731
|
const action: GCAction = {
|
|
732
|
-
type:
|
|
732
|
+
type: GC,
|
|
733
733
|
entities: [
|
|
734
734
|
[Article.key, '100000000'],
|
|
735
735
|
['sillythings', '10'],
|
|
@@ -4,15 +4,15 @@ import { invalidateReducer } from './invalidateReducer.js';
|
|
|
4
4
|
import { setReducer } from './setReducer.js';
|
|
5
5
|
import { setResponseReducer } from './setResponseReducer.js';
|
|
6
6
|
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
SET,
|
|
8
|
+
INVALIDATE,
|
|
9
|
+
RESET,
|
|
10
|
+
FETCH,
|
|
11
|
+
GC,
|
|
12
|
+
OPTIMISTIC,
|
|
13
|
+
INVALIDATEALL,
|
|
14
|
+
EXPIREALL,
|
|
15
|
+
SET_RESPONSE,
|
|
16
16
|
} from '../../actionTypes.js';
|
|
17
17
|
import type Controller from '../../controller/Controller.js';
|
|
18
18
|
import type { ActionTypes, State } from '../../types.js';
|
|
@@ -24,7 +24,7 @@ export default function createReducer(controller: Controller): ReducerType {
|
|
|
24
24
|
): State<unknown> {
|
|
25
25
|
if (!state) state = initialState;
|
|
26
26
|
switch (action.type) {
|
|
27
|
-
case
|
|
27
|
+
case GC:
|
|
28
28
|
// inline deletes are fine as these should have 0 refcounts
|
|
29
29
|
action.entities.forEach(([key, pk]) => {
|
|
30
30
|
delete (state as any).entities[key]?.[pk];
|
|
@@ -35,25 +35,25 @@ export default function createReducer(controller: Controller): ReducerType {
|
|
|
35
35
|
delete (state as any).meta[fetchKey];
|
|
36
36
|
});
|
|
37
37
|
return state;
|
|
38
|
-
case
|
|
38
|
+
case FETCH:
|
|
39
39
|
return fetchReducer(state, action);
|
|
40
40
|
|
|
41
|
-
case
|
|
41
|
+
case OPTIMISTIC:
|
|
42
42
|
// eslint-disable-next-line no-fallthrough
|
|
43
|
-
case
|
|
43
|
+
case SET_RESPONSE:
|
|
44
44
|
return setResponseReducer(state, action, controller);
|
|
45
45
|
|
|
46
|
-
case
|
|
46
|
+
case SET:
|
|
47
47
|
return setReducer(state, action, controller);
|
|
48
48
|
|
|
49
|
-
case
|
|
50
|
-
case
|
|
49
|
+
case INVALIDATEALL:
|
|
50
|
+
case INVALIDATE:
|
|
51
51
|
return invalidateReducer(state, action);
|
|
52
52
|
|
|
53
|
-
case
|
|
53
|
+
case EXPIREALL:
|
|
54
54
|
return expireReducer(state, action);
|
|
55
55
|
|
|
56
|
-
case
|
|
56
|
+
case RESET:
|
|
57
57
|
return { ...initialState, lastReset: action.date };
|
|
58
58
|
|
|
59
59
|
default:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { INVALIDATE } from '../../actionTypes.js';
|
|
2
2
|
import type {
|
|
3
3
|
State,
|
|
4
4
|
InvalidateAllAction,
|
|
@@ -21,7 +21,7 @@ export function invalidateReducer(
|
|
|
21
21
|
delete itemMeta.error;
|
|
22
22
|
meta[key] = itemMeta;
|
|
23
23
|
};
|
|
24
|
-
if (action.type ===
|
|
24
|
+
if (action.type === INVALIDATE) {
|
|
25
25
|
invalidateKey(action.key);
|
|
26
26
|
} else {
|
|
27
27
|
Object.keys(endpoints).forEach(key => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { normalize } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { OPTIMISTIC } from '../../actionTypes.js';
|
|
4
4
|
import AbortOptimistic from '../../controller/AbortOptimistic.js';
|
|
5
5
|
import type Controller from '../../controller/Controller.js';
|
|
6
6
|
import type {
|
|
@@ -20,7 +20,7 @@ export function setResponseReducer(
|
|
|
20
20
|
try {
|
|
21
21
|
let response: any;
|
|
22
22
|
// for true set's response is contained in action
|
|
23
|
-
if (action.type ===
|
|
23
|
+
if (action.type === OPTIMISTIC) {
|
|
24
24
|
// this should never happen
|
|
25
25
|
/* istanbul ignore if */
|
|
26
26
|
if (!action.endpoint.getOptimisticResponse) return state;
|
|
@@ -142,7 +142,7 @@ function filterOptimistic(
|
|
|
142
142
|
return state.optimistic.filter(
|
|
143
143
|
optimisticAction =>
|
|
144
144
|
optimisticAction.key !== resolvingAction.key ||
|
|
145
|
-
(optimisticAction.type ===
|
|
145
|
+
(optimisticAction.type === OPTIMISTIC ?
|
|
146
146
|
optimisticAction.meta.fetchedAt !== resolvingAction.meta.fetchedAt
|
|
147
147
|
: optimisticAction.meta.date > resolvingAction.meta.date),
|
|
148
148
|
);
|
package/ts3.4/actionTypes.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
export declare const FETCH: "rdc/fetch";
|
|
2
|
+
export declare const SET: "rdc/set";
|
|
3
|
+
export declare const SET_RESPONSE: "rdc/setresponse";
|
|
4
|
+
export declare const OPTIMISTIC: "rdc/optimistic";
|
|
5
|
+
export declare const RESET: "rdc/reset";
|
|
6
|
+
export declare const SUBSCRIBE: "rdc/subscribe";
|
|
7
|
+
export declare const UNSUBSCRIBE: "rdc/unsubscribe";
|
|
8
|
+
export declare const INVALIDATE: "rdc/invalidate";
|
|
9
|
+
export declare const INVALIDATEALL: "rdc/invalidateall";
|
|
10
|
+
export declare const EXPIREALL: "rdc/expireall";
|
|
11
|
+
export declare const GC: "rdc/gc";
|
|
1
12
|
export declare const FETCH_TYPE: "rdc/fetch";
|
|
2
13
|
export declare const SET_TYPE: "rdc/set";
|
|
3
14
|
export declare const SET_RESPONSE_TYPE: "rdc/setresponse";
|