@data-client/core 0.13.5 → 0.14.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/README.md +2 -1
- package/dist/index.js +164 -191
- package/dist/index.umd.min.js +1 -1
- package/legacy/actions.js +1 -1
- package/legacy/controller/Controller.js +8 -17
- 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/manager/LogoutManager.js +2 -2
- package/legacy/manager/NetworkManager.js +27 -29
- package/legacy/manager/PollingSubscription.js +3 -3
- package/legacy/manager/SubscriptionManager.js +3 -3
- package/legacy/state/reducer/fetchReducer.js +6 -10
- package/legacy/state/reducer/invalidateReducer.js +2 -2
- package/legacy/state/reducer/setReducer.js +3 -3
- package/legacy/state/reducer/setResponseReducer.js +17 -17
- package/legacy/types.js +1 -1
- package/lib/actions.d.ts +34 -40
- package/lib/actions.d.ts.map +1 -1
- package/lib/actions.js +1 -1
- package/lib/controller/Controller.d.ts +1 -1
- package/lib/controller/Controller.d.ts.map +1 -1
- package/lib/controller/Controller.js +8 -17
- 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/{createSet.d.ts → actions/createSet.d.ts} +2 -2
- 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/manager/LogoutManager.js +2 -2
- package/lib/manager/NetworkManager.d.ts +2 -2
- package/lib/manager/NetworkManager.d.ts.map +1 -1
- package/lib/manager/NetworkManager.js +27 -29
- package/lib/manager/PollingSubscription.js +3 -3
- package/lib/manager/SubscriptionManager.js +3 -3
- package/lib/state/reducer/expireReducer.d.ts +2 -2
- 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 +2 -2
- package/lib/state/reducer/invalidateReducer.js +2 -2
- package/lib/state/reducer/setReducer.d.ts +26 -1
- package/lib/state/reducer/setReducer.d.ts.map +1 -1
- package/lib/state/reducer/setReducer.js +3 -3
- package/lib/state/reducer/setResponseReducer.d.ts +28 -1
- 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 +36 -42
- package/src/controller/Controller.ts +15 -22
- 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/manager/LogoutManager.ts +1 -1
- package/src/manager/NetworkManager.ts +26 -27
- 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 +42 -45
- package/src/state/reducer/fetchReducer.ts +12 -18
- package/src/state/reducer/invalidateReducer.ts +1 -1
- package/src/state/reducer/setReducer.ts +4 -10
- package/src/state/reducer/setResponseReducer.ts +18 -20
- package/src/types.ts +4 -4
- package/ts3.4/actions.d.ts +35 -41
- package/ts3.4/controller/Controller.d.ts +1 -1
- 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/{createSet.d.ts → actions/createSet.d.ts} +2 -2
- 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/manager/NetworkManager.d.ts +2 -2
- package/ts3.4/state/reducer/expireReducer.d.ts +2 -2
- package/ts3.4/state/reducer/invalidateReducer.d.ts +2 -2
- package/ts3.4/state/reducer/setReducer.d.ts +26 -1
- package/ts3.4/state/reducer/setResponseReducer.d.ts +28 -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/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.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/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 -43
- 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
|
@@ -36,8 +36,8 @@ describe('Controller', () => {
|
|
|
36
36
|
tags: ['a', 'best', 'react'],
|
|
37
37
|
};
|
|
38
38
|
const { entities, result } = normalize(
|
|
39
|
-
payload,
|
|
40
39
|
CoolerArticleResource.get.schema,
|
|
40
|
+
payload,
|
|
41
41
|
);
|
|
42
42
|
const fetchKey = CoolerArticleResource.get.key({ id: payload.id });
|
|
43
43
|
const state = {
|
|
@@ -73,8 +73,8 @@ describe('Controller', () => {
|
|
|
73
73
|
tags: ['a', 'best', 'react'],
|
|
74
74
|
};
|
|
75
75
|
const { entities, result } = normalize(
|
|
76
|
-
payload,
|
|
77
76
|
CoolerArticleResource.get.schema,
|
|
77
|
+
payload,
|
|
78
78
|
);
|
|
79
79
|
const fetchKey = CoolerArticleResource.get.key({ id: payload.id });
|
|
80
80
|
const state = {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { EXPIREALL_TYPE } from '../../actionTypes.js';
|
|
2
|
+
import type { ExpireAllAction } from '../../types.js';
|
|
3
|
+
|
|
4
|
+
export function createExpireAll(
|
|
5
|
+
testKey: (key: string) => boolean,
|
|
6
|
+
): ExpireAllAction {
|
|
7
|
+
return {
|
|
8
|
+
type: EXPIREALL_TYPE,
|
|
9
|
+
testKey,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -1,39 +1,35 @@
|
|
|
1
1
|
import type { EndpointInterface, NI } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
3
|
+
import { FETCH_TYPE } from '../../actionTypes.js';
|
|
4
|
+
import type { FetchAction, FetchMeta } from '../../types.js';
|
|
5
|
+
import { EndpointUpdateFunction } from '../types.js';
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Requesting a fetch to begin
|
|
9
9
|
*/
|
|
10
|
-
export
|
|
10
|
+
export function createFetch<
|
|
11
11
|
E extends EndpointInterface & { update?: EndpointUpdateFunction<E> },
|
|
12
12
|
>(
|
|
13
13
|
endpoint: E,
|
|
14
14
|
{ args }: { args: readonly [...Parameters<E>] },
|
|
15
15
|
): FetchAction<E> {
|
|
16
|
-
const key = endpoint.key(...args);
|
|
17
16
|
let resolve: (value?: any | PromiseLike<any>) => void = 0 as any;
|
|
18
17
|
let reject: (reason?: any) => void = 0 as any;
|
|
19
18
|
const promise = new Promise<any>((a, b) => {
|
|
20
19
|
[resolve, reject] = [a, b];
|
|
21
20
|
});
|
|
22
|
-
const meta: FetchMeta
|
|
23
|
-
|
|
24
|
-
key,
|
|
25
|
-
throttle: !endpoint.sideEffect,
|
|
21
|
+
const meta: FetchMeta = {
|
|
22
|
+
fetchedAt: Date.now(),
|
|
26
23
|
resolve,
|
|
27
24
|
reject,
|
|
28
25
|
promise,
|
|
29
|
-
createdAt: Date.now(),
|
|
30
|
-
nm: false,
|
|
31
26
|
};
|
|
32
27
|
|
|
33
28
|
return {
|
|
34
29
|
type: FETCH_TYPE,
|
|
35
|
-
payload: () => endpoint(...args) as any,
|
|
36
|
-
meta,
|
|
37
30
|
endpoint,
|
|
31
|
+
args,
|
|
32
|
+
key: endpoint.key(...args),
|
|
33
|
+
meta,
|
|
38
34
|
};
|
|
39
35
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { EndpointInterface } from '@data-client/normalizr';
|
|
2
|
+
|
|
3
|
+
import { INVALIDATE_TYPE } from '../../actionTypes.js';
|
|
4
|
+
import type { InvalidateAction } from '../../types.js';
|
|
5
|
+
|
|
6
|
+
export function createInvalidate<E extends EndpointInterface>(
|
|
7
|
+
endpoint: E,
|
|
8
|
+
{ args }: { args: readonly [...Parameters<E>] },
|
|
9
|
+
): InvalidateAction {
|
|
10
|
+
return {
|
|
11
|
+
type: INVALIDATE_TYPE,
|
|
12
|
+
key: endpoint.key(...args),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { INVALIDATEALL_TYPE } from '../../actionTypes.js';
|
|
2
|
+
import type { InvalidateAllAction } from '../../types.js';
|
|
3
|
+
|
|
4
|
+
export function createInvalidateAll(
|
|
5
|
+
testKey: (key: string) => boolean,
|
|
6
|
+
): InvalidateAllAction {
|
|
7
|
+
return {
|
|
8
|
+
type: INVALIDATEALL_TYPE,
|
|
9
|
+
testKey,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ActionMeta } from '../../actions.js';
|
|
2
|
+
|
|
3
|
+
export function createMeta(
|
|
4
|
+
expiryLength: number,
|
|
5
|
+
fetchedAt?: number,
|
|
6
|
+
): ActionMeta {
|
|
7
|
+
const now = Date.now();
|
|
8
|
+
return {
|
|
9
|
+
fetchedAt: fetchedAt ?? now,
|
|
10
|
+
date: now,
|
|
11
|
+
expiresAt: now + expiryLength,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { EndpointInterface } from '@data-client/normalizr';
|
|
2
|
+
|
|
3
|
+
import { createMeta } from './createMeta.js';
|
|
4
|
+
import { OPTIMISTIC_TYPE } from '../../actionTypes.js';
|
|
5
|
+
import type { OptimisticAction } from '../../types.js';
|
|
6
|
+
import type { EndpointUpdateFunction } from '../types.js';
|
|
7
|
+
|
|
8
|
+
export function createOptimistic<
|
|
9
|
+
E extends EndpointInterface & {
|
|
10
|
+
update?: EndpointUpdateFunction<E>;
|
|
11
|
+
},
|
|
12
|
+
>(
|
|
13
|
+
endpoint: E,
|
|
14
|
+
args: readonly [...Parameters<E>],
|
|
15
|
+
fetchedAt: number,
|
|
16
|
+
): OptimisticAction<E> {
|
|
17
|
+
/* istanbul ignore next */
|
|
18
|
+
if (
|
|
19
|
+
process.env.NODE_ENV === 'development' &&
|
|
20
|
+
(endpoint.dataExpiryLength ?? 0) < 0
|
|
21
|
+
) {
|
|
22
|
+
throw new Error('Negative expiry length are not allowed.');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
type: OPTIMISTIC_TYPE,
|
|
27
|
+
endpoint,
|
|
28
|
+
args,
|
|
29
|
+
key: endpoint.key(...args),
|
|
30
|
+
meta: createMeta(endpoint.dataExpiryLength ?? 60000, fetchedAt),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Denormalize,
|
|
3
|
+
Queryable,
|
|
4
|
+
SchemaArgs,
|
|
5
|
+
} from '@data-client/normalizr';
|
|
6
|
+
|
|
7
|
+
import { createMeta } from './createMeta.js';
|
|
8
|
+
import { SET_TYPE } from '../../actionTypes.js';
|
|
9
|
+
import type { SetAction } from '../../types.js';
|
|
10
|
+
import ensurePojo from '../ensurePojo.js';
|
|
11
|
+
|
|
12
|
+
export function createSet<S extends Queryable>(
|
|
13
|
+
schema: S,
|
|
14
|
+
{
|
|
15
|
+
args,
|
|
16
|
+
fetchedAt,
|
|
17
|
+
value,
|
|
18
|
+
}: {
|
|
19
|
+
args: readonly [...SchemaArgs<S>];
|
|
20
|
+
value: {} | ((previousValue: Denormalize<S>) => {});
|
|
21
|
+
fetchedAt?: number;
|
|
22
|
+
},
|
|
23
|
+
): SetAction<S> {
|
|
24
|
+
return {
|
|
25
|
+
type: SET_TYPE,
|
|
26
|
+
schema,
|
|
27
|
+
value,
|
|
28
|
+
args: args.map(ensurePojo) as SchemaArgs<S>,
|
|
29
|
+
meta: createMeta(60000, fetchedAt),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { EndpointInterface, ResolveType } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
3
|
+
import { createMeta } from './createMeta.js';
|
|
4
|
+
import { SET_RESPONSE_TYPE } from '../../actionTypes.js';
|
|
5
|
+
import type { SetResponseAction } from '../../types.js';
|
|
6
|
+
import ensurePojo from '../ensurePojo.js';
|
|
7
|
+
import { EndpointUpdateFunction } from '../types.js';
|
|
7
8
|
|
|
8
|
-
export
|
|
9
|
+
export function createSetResponse<
|
|
9
10
|
E extends EndpointInterface & {
|
|
10
11
|
update?: EndpointUpdateFunction<E>;
|
|
11
12
|
},
|
|
@@ -19,7 +20,7 @@ export default function createSetResponse<
|
|
|
19
20
|
},
|
|
20
21
|
): SetResponseAction<E>;
|
|
21
22
|
|
|
22
|
-
export
|
|
23
|
+
export function createSetResponse<
|
|
23
24
|
E extends EndpointInterface & {
|
|
24
25
|
update?: EndpointUpdateFunction<E>;
|
|
25
26
|
},
|
|
@@ -33,7 +34,7 @@ export default function createSetResponse<
|
|
|
33
34
|
},
|
|
34
35
|
): SetResponseAction<E>;
|
|
35
36
|
|
|
36
|
-
export
|
|
37
|
+
export function createSetResponse<
|
|
37
38
|
E extends EndpointInterface & {
|
|
38
39
|
update?: EndpointUpdateFunction<E>;
|
|
39
40
|
},
|
|
@@ -53,27 +54,20 @@ export default function createSetResponse<
|
|
|
53
54
|
): SetResponseAction<E> {
|
|
54
55
|
const expiryLength: number =
|
|
55
56
|
error ?
|
|
56
|
-
endpoint.errorExpiryLength ?? 1000
|
|
57
|
-
: endpoint.dataExpiryLength ?? 60000;
|
|
57
|
+
(endpoint.errorExpiryLength ?? 1000)
|
|
58
|
+
: (endpoint.dataExpiryLength ?? 60000);
|
|
58
59
|
/* istanbul ignore next */
|
|
59
60
|
if (process.env.NODE_ENV === 'development' && expiryLength < 0) {
|
|
60
61
|
throw new Error('Negative expiry length are not allowed.');
|
|
61
62
|
}
|
|
62
|
-
const now = Date.now();
|
|
63
|
-
const meta: SetResponseMeta = {
|
|
64
|
-
args: args.map(ensurePojo),
|
|
65
|
-
fetchedAt: fetchedAt ?? now,
|
|
66
|
-
date: now,
|
|
67
|
-
expiresAt: now + expiryLength,
|
|
68
|
-
key: endpoint.key(...args),
|
|
69
|
-
};
|
|
70
63
|
|
|
71
|
-
|
|
64
|
+
return {
|
|
72
65
|
type: SET_RESPONSE_TYPE,
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
endpoint,
|
|
67
|
+
response,
|
|
68
|
+
args: args.map(ensurePojo),
|
|
69
|
+
key: endpoint.key(...args),
|
|
70
|
+
meta: createMeta(expiryLength, fetchedAt),
|
|
71
|
+
error,
|
|
76
72
|
};
|
|
77
|
-
if (error) (action as any).error = true;
|
|
78
|
-
return action;
|
|
79
73
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { EndpointInterface } from '@data-client/normalizr';
|
|
2
2
|
|
|
3
|
-
import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '
|
|
4
|
-
import type { SubscribeAction, UnsubscribeAction } from '
|
|
3
|
+
import { SUBSCRIBE_TYPE, UNSUBSCRIBE_TYPE } from '../../actionTypes.js';
|
|
4
|
+
import type { SubscribeAction, UnsubscribeAction } from '../../types.js';
|
|
5
5
|
|
|
6
6
|
export function createSubscription<E extends EndpointInterface>(
|
|
7
7
|
endpoint: E,
|
|
@@ -10,10 +10,8 @@ export function createSubscription<E extends EndpointInterface>(
|
|
|
10
10
|
return {
|
|
11
11
|
type: SUBSCRIBE_TYPE,
|
|
12
12
|
endpoint,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
key: endpoint.key(...args),
|
|
16
|
-
},
|
|
13
|
+
args,
|
|
14
|
+
key: endpoint.key(...args),
|
|
17
15
|
};
|
|
18
16
|
}
|
|
19
17
|
|
|
@@ -24,9 +22,7 @@ export function createUnsubscription<E extends EndpointInterface>(
|
|
|
24
22
|
return {
|
|
25
23
|
type: UNSUBSCRIBE_TYPE,
|
|
26
24
|
endpoint,
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
key: endpoint.key(...args),
|
|
30
|
-
},
|
|
25
|
+
args,
|
|
26
|
+
key: endpoint.key(...args),
|
|
31
27
|
};
|
|
32
28
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export {
|
|
2
|
+
createSubscription,
|
|
3
|
+
createUnsubscription,
|
|
4
|
+
} from './createSubscription.js';
|
|
5
|
+
export { createSetResponse } from './createSetResponse.js';
|
|
6
|
+
export { createSet } from './createSet.js';
|
|
7
|
+
export { createReset } from './createReset.js';
|
|
8
|
+
export { createOptimistic } from './createOptimistic.js';
|
|
9
|
+
export { createMeta } from './createMeta.js';
|
|
10
|
+
export { createInvalidateAll } from './createInvalidateAll.js';
|
|
11
|
+
export { createInvalidate } from './createInvalidate.js';
|
|
12
|
+
export { createFetch } from './createFetch.js';
|
|
13
|
+
export { createExpireAll } from './createExpireAll.js';
|
package/src/index.ts
CHANGED
|
@@ -39,9 +39,7 @@ export type {
|
|
|
39
39
|
DataClientDispatch,
|
|
40
40
|
GenericDispatch,
|
|
41
41
|
} from './controller/Controller.js';
|
|
42
|
-
export
|
|
43
|
-
export { default as createSet } from './controller/createSet.js';
|
|
44
|
-
export { default as createSetResponse } from './controller/createSetResponse.js';
|
|
42
|
+
export * as actions from './controller/actions/index.js';
|
|
45
43
|
|
|
46
44
|
export * from './controller/types.js';
|
|
47
45
|
export * as actionTypes from './actionTypes.js';
|
|
@@ -1,6 +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
4
|
import type {
|
|
5
5
|
FetchAction,
|
|
6
6
|
Manager,
|
|
@@ -62,15 +62,14 @@ export default class NetworkManager implements Manager {
|
|
|
62
62
|
case SET_RESPONSE_TYPE:
|
|
63
63
|
// only set after new state is computed
|
|
64
64
|
return next(action).then(() => {
|
|
65
|
-
if (action.
|
|
65
|
+
if (action.key in this.fetched) {
|
|
66
66
|
// Note: meta *must* be set by reducer so this should be safe
|
|
67
|
-
const error =
|
|
68
|
-
controller.getState().meta[action.meta.key]?.error;
|
|
67
|
+
const error = controller.getState().meta[action.key]?.error;
|
|
69
68
|
// processing errors result in state meta having error, so we should reject the promise
|
|
70
69
|
if (error) {
|
|
71
70
|
this.handleSet(
|
|
72
71
|
createSetResponse(action.endpoint, {
|
|
73
|
-
args: action.
|
|
72
|
+
args: action.args,
|
|
74
73
|
response: error,
|
|
75
74
|
fetchedAt: action.meta.fetchedAt,
|
|
76
75
|
error: true,
|
|
@@ -103,7 +102,7 @@ export default class NetworkManager implements Manager {
|
|
|
103
102
|
/** Used by DevtoolsManager to determine whether to log an action */
|
|
104
103
|
skipLogging(action: ActionTypes) {
|
|
105
104
|
/* istanbul ignore next */
|
|
106
|
-
return action.type === FETCH_TYPE && action.
|
|
105
|
+
return action.type === FETCH_TYPE && action.key in this.fetched;
|
|
107
106
|
}
|
|
108
107
|
|
|
109
108
|
/** On mount */
|
|
@@ -149,15 +148,15 @@ export default class NetworkManager implements Manager {
|
|
|
149
148
|
* Will then start a promise for a key and potentially start the network
|
|
150
149
|
* fetch.
|
|
151
150
|
*
|
|
152
|
-
* Uses throttle
|
|
151
|
+
* Uses throttle endpoints without sideEffects. This is valuable
|
|
153
152
|
* for ensures mutation requests always go through.
|
|
154
153
|
*/
|
|
155
154
|
protected handleFetch(action: FetchAction) {
|
|
156
|
-
const
|
|
157
|
-
const
|
|
155
|
+
const { resolve, reject, fetchedAt } = action.meta;
|
|
156
|
+
const throttle = !action.endpoint.sideEffect;
|
|
158
157
|
|
|
159
158
|
const deferedFetch = () => {
|
|
160
|
-
let promise =
|
|
159
|
+
let promise = action.endpoint(...action.args);
|
|
161
160
|
const resolvePromise = (
|
|
162
161
|
promise: Promise<string | number | object | null>,
|
|
163
162
|
) =>
|
|
@@ -177,7 +176,7 @@ export default class NetworkManager implements Manager {
|
|
|
177
176
|
promise = resolvePromise(promise);
|
|
178
177
|
}
|
|
179
178
|
promise = promise
|
|
180
|
-
.then(
|
|
179
|
+
.then(response => {
|
|
181
180
|
let lastReset = this.getLastReset();
|
|
182
181
|
|
|
183
182
|
/* istanbul ignore else */
|
|
@@ -189,23 +188,23 @@ export default class NetworkManager implements Manager {
|
|
|
189
188
|
}
|
|
190
189
|
|
|
191
190
|
// don't update state with promises started before last clear
|
|
192
|
-
if (
|
|
191
|
+
if (fetchedAt >= lastReset) {
|
|
193
192
|
this.controller.resolve(action.endpoint, {
|
|
194
|
-
args: action.
|
|
195
|
-
response
|
|
196
|
-
fetchedAt
|
|
193
|
+
args: action.args,
|
|
194
|
+
response,
|
|
195
|
+
fetchedAt,
|
|
197
196
|
});
|
|
198
197
|
}
|
|
199
|
-
return
|
|
198
|
+
return response;
|
|
200
199
|
})
|
|
201
200
|
.catch(error => {
|
|
202
201
|
const lastReset = this.getLastReset();
|
|
203
202
|
// don't update state with promises started before last clear
|
|
204
|
-
if (
|
|
203
|
+
if (fetchedAt >= lastReset) {
|
|
205
204
|
this.controller.resolve(action.endpoint, {
|
|
206
|
-
args: action.
|
|
205
|
+
args: action.args,
|
|
207
206
|
response: error,
|
|
208
|
-
fetchedAt
|
|
207
|
+
fetchedAt,
|
|
209
208
|
error: true,
|
|
210
209
|
});
|
|
211
210
|
}
|
|
@@ -215,7 +214,7 @@ export default class NetworkManager implements Manager {
|
|
|
215
214
|
};
|
|
216
215
|
|
|
217
216
|
if (throttle) {
|
|
218
|
-
return this.throttle(key, deferedFetch,
|
|
217
|
+
return this.throttle(action.key, deferedFetch, fetchedAt)
|
|
219
218
|
.then(data => resolve(data))
|
|
220
219
|
.catch(error => reject(error));
|
|
221
220
|
} else {
|
|
@@ -229,16 +228,16 @@ export default class NetworkManager implements Manager {
|
|
|
229
228
|
*/
|
|
230
229
|
protected handleSet(action: SetResponseAction) {
|
|
231
230
|
// this can still turn out to be untrue since this is async
|
|
232
|
-
if (action.
|
|
231
|
+
if (action.key in this.fetched) {
|
|
233
232
|
let promiseHandler: (value?: any) => void;
|
|
234
233
|
if (action.error) {
|
|
235
|
-
promiseHandler = this.rejectors[action.
|
|
234
|
+
promiseHandler = this.rejectors[action.key];
|
|
236
235
|
} else {
|
|
237
|
-
promiseHandler = this.resolvers[action.
|
|
236
|
+
promiseHandler = this.resolvers[action.key];
|
|
238
237
|
}
|
|
239
|
-
promiseHandler(action.
|
|
238
|
+
promiseHandler(action.response);
|
|
240
239
|
// since we're resolved we no longer need to keep track of this promise
|
|
241
|
-
this.clear(action.
|
|
240
|
+
this.clear(action.key);
|
|
242
241
|
}
|
|
243
242
|
}
|
|
244
243
|
|
|
@@ -266,7 +265,7 @@ export default class NetworkManager implements Manager {
|
|
|
266
265
|
protected throttle(
|
|
267
266
|
key: string,
|
|
268
267
|
fetch: () => Promise<any>,
|
|
269
|
-
|
|
268
|
+
fetchedAt: number,
|
|
270
269
|
) {
|
|
271
270
|
const lastReset = this.getLastReset();
|
|
272
271
|
// we're already fetching so reuse the promise
|
|
@@ -279,7 +278,7 @@ export default class NetworkManager implements Manager {
|
|
|
279
278
|
this.resolvers[key] = resolve;
|
|
280
279
|
this.rejectors[key] = reject;
|
|
281
280
|
});
|
|
282
|
-
this.fetchedAt[key] =
|
|
281
|
+
this.fetchedAt[key] = fetchedAt;
|
|
283
282
|
|
|
284
283
|
this.idleCallback(
|
|
285
284
|
() => {
|
|
@@ -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) {
|