react-redux-cache 0.3.0 → 0.4.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 +45 -22
- package/dist/createActions.d.ts +6 -0
- package/dist/createActions.js +6 -6
- package/dist/createCache.d.ts +25 -12
- package/dist/createCache.js +25 -19
- package/dist/index.js +3 -3
- package/dist/types.d.ts +1 -1
- package/dist/utilsAndConstants.d.ts +0 -4
- package/dist/utilsAndConstants.js +0 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
<details>
|
|
2
|
+
<summary>Donations 🙌</summary>
|
|
3
|
+
<b>BTC:</b> bc1qs0sq7agz5j30qnqz9m60xj4tt8th6aazgw7kxr <br>
|
|
4
|
+
<b>ETH:</b> 0x1D834755b5e889703930AC9b784CB625B3cd833E <br>
|
|
5
|
+
<b>USDT(Tron):</b> TPrCq8LxGykQ4as3o1oB8V7x1w2YPU2o5n <br>
|
|
6
|
+
<b>TON:</b> EQAtBuFWI3H_LpHfEToil4iYemtfmyzlaJpahM3tFSoxojvV <br>
|
|
7
|
+
<b>DOGE:</b> D7GMQdKhKC9ymbT9PtcetSFTQjyPRRfkwT <br>
|
|
8
|
+
</details>
|
|
8
9
|
|
|
9
10
|
# react-redux-cache
|
|
10
11
|
|
|
11
|
-
**Powerful** yet **lightweight** data fetching and caching library that supports **normalization** unlike `react-query` and `rtk-query`, while having similar but very simple interface. Built on top of `redux`, fully typed and written on Typescript.
|
|
12
|
+
**Powerful** yet **lightweight** data fetching and caching library that supports **normalization** unlike `react-query` and `rtk-query`, while having similar but very simple interface. Built on top of `redux`, covered with tests, fully typed and written on Typescript.
|
|
12
13
|
|
|
13
14
|
**Normalization** is the best way to keep the state of the app **consistent** between different views, reduces the number of fetches and allows to show cached data when navigating, which greatly improves **user experience**.
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with **full control** over its storage - redux store, with ability to write custom selectors, actions and reducers to manage cached state.
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
Example of **normalized** redux state, generated by cache reducer from `/example` project:
|
|
18
19
|
```js
|
|
19
|
-
|
|
20
|
+
{
|
|
20
21
|
entities: {
|
|
21
22
|
// each typename has its own map of entities, stored by id
|
|
22
23
|
users: {
|
|
@@ -50,6 +51,38 @@ const state = {
|
|
|
50
51
|
}
|
|
51
52
|
```
|
|
52
53
|
|
|
54
|
+
Example of **not normalized** redux state, generated by cache reducer from `/example` project:
|
|
55
|
+
```js
|
|
56
|
+
{
|
|
57
|
+
// entities map is used for normalization and is empty here
|
|
58
|
+
entities: {},
|
|
59
|
+
queries: {
|
|
60
|
+
// each query has its own map of query states, stored by cache key, which is generated from query params
|
|
61
|
+
getUser: {
|
|
62
|
+
"2": {loading: false, error: undefined, result: {id: 2, bank: {id: "2", name: "Bank 2"}, name: "User 2"}},
|
|
63
|
+
"3": {loading: true}
|
|
64
|
+
},
|
|
65
|
+
getUsers: {
|
|
66
|
+
// example of paginated state under custom cache key
|
|
67
|
+
"all-pages": {
|
|
68
|
+
loading: false,
|
|
69
|
+
result: {
|
|
70
|
+
items: [
|
|
71
|
+
{id: 0, bank: {id: "0", name: "Bank 0"}, name: "User 0 *"},
|
|
72
|
+
{id: 1, bank: {id: "1", name: "Bank 1"}, name: "User 1 *"},
|
|
73
|
+
{id: 2, bank: {id: "2", name: "Bank 2"}, name: "User 2"}
|
|
74
|
+
],
|
|
75
|
+
page: 1
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
mutations: {
|
|
81
|
+
// each mutation has its own state as well
|
|
82
|
+
updateUser: {loading: false, result: {id: 1, bank: {id: "1", name: "Bank 1"}, name: "User 1 *"}}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
53
86
|
|
|
54
87
|
### Table of contents
|
|
55
88
|
|
|
@@ -80,17 +113,7 @@ All typenames, queries and mutations should be passed while initializing the cac
|
|
|
80
113
|
export const {
|
|
81
114
|
cache,
|
|
82
115
|
reducer,
|
|
83
|
-
hooks: {useClient, useMutation, useQuery
|
|
84
|
-
// Actions, selectors and utils may be not used at all
|
|
85
|
-
selectors: {entitiesSelector, entitiesByTypenameSelector},
|
|
86
|
-
actions: {
|
|
87
|
-
updateQueryStateAndEntities,
|
|
88
|
-
updateMutationStateAndEntities,
|
|
89
|
-
mergeEntityChanges,
|
|
90
|
-
clearQueryState,
|
|
91
|
-
clearMutationState,
|
|
92
|
-
},
|
|
93
|
-
utils: {applyEntityChanges},
|
|
116
|
+
hooks: {useClient, useMutation, useQuery},
|
|
94
117
|
} = createCache({
|
|
95
118
|
// Used as prefix for actions and in default cacheStateSelector for selecting cache state from redux state.
|
|
96
119
|
name: 'cache',
|
package/dist/createActions.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { EntityChanges, Key, QueryMutationState, Typenames } from './types';
|
|
2
2
|
export type ActionMap<N extends string, T extends Typenames, QR, MR> = ReturnType<typeof createActions<N, T, QR, MR>>;
|
|
3
3
|
export declare const createActions: <N extends string, T extends Typenames, QR, MR>(name: N) => {
|
|
4
|
+
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
4
5
|
updateQueryStateAndEntities: {
|
|
5
6
|
<K extends keyof QR>(queryKey: K, queryCacheKey: Key, state?: Partial<QueryMutationState<QR[K]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
6
7
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
@@ -11,6 +12,7 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
11
12
|
};
|
|
12
13
|
type: `@rrc/${N}/updateQueryStateAndEntities`;
|
|
13
14
|
};
|
|
15
|
+
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
14
16
|
updateMutationStateAndEntities: {
|
|
15
17
|
<K_1 extends keyof MR>(mutationKey: K_1, state?: Partial<QueryMutationState<MR[K_1]>> | undefined, entityChagnes?: EntityChanges<T> | undefined): {
|
|
16
18
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
@@ -20,6 +22,7 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
20
22
|
};
|
|
21
23
|
type: `@rrc/${N}/updateMutationStateAndEntities`;
|
|
22
24
|
};
|
|
25
|
+
/** Merge EntityChanges to the state. */
|
|
23
26
|
mergeEntityChanges: {
|
|
24
27
|
(changes: EntityChanges<T>): {
|
|
25
28
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
@@ -27,6 +30,8 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
27
30
|
};
|
|
28
31
|
type: `@rrc/${N}/mergeEntityChanges`;
|
|
29
32
|
};
|
|
33
|
+
/** Clear states for provided query keys and cache keys.
|
|
34
|
+
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
30
35
|
clearQueryState: {
|
|
31
36
|
<K_2 extends keyof QR>(queryKeys: {
|
|
32
37
|
key: K_2;
|
|
@@ -40,6 +45,7 @@ export declare const createActions: <N extends string, T extends Typenames, QR,
|
|
|
40
45
|
};
|
|
41
46
|
type: `@rrc/${N}/clearQueryState`;
|
|
42
47
|
};
|
|
48
|
+
/** Clear states for provided mutation keys. */
|
|
43
49
|
clearMutationState: {
|
|
44
50
|
<K_3 extends keyof MR>(mutationKeys: K_3[]): {
|
|
45
51
|
type: `@rrc/${N}/clearMutationState`;
|
package/dist/createActions.js
CHANGED
|
@@ -5,7 +5,6 @@ const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
|
5
5
|
const createActions = (name) => {
|
|
6
6
|
const actionPrefix = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/${name}/`;
|
|
7
7
|
const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`;
|
|
8
|
-
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
9
8
|
const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChagnes) => ({
|
|
10
9
|
type: updateQueryStateAndEntitiesType,
|
|
11
10
|
queryKey,
|
|
@@ -15,7 +14,6 @@ const createActions = (name) => {
|
|
|
15
14
|
});
|
|
16
15
|
updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType;
|
|
17
16
|
const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`;
|
|
18
|
-
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
19
17
|
const updateMutationStateAndEntities = (mutationKey, state, entityChagnes) => ({
|
|
20
18
|
type: updateMutationStateAndEntitiesType,
|
|
21
19
|
mutationKey,
|
|
@@ -24,32 +22,34 @@ const createActions = (name) => {
|
|
|
24
22
|
});
|
|
25
23
|
updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType;
|
|
26
24
|
const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`;
|
|
27
|
-
/** Merge EntityChanges to the state. */
|
|
28
25
|
const mergeEntityChanges = (changes) => ({
|
|
29
26
|
type: mergeEntityChangesType,
|
|
30
27
|
changes,
|
|
31
28
|
});
|
|
32
29
|
mergeEntityChanges.type = mergeEntityChangesType;
|
|
33
30
|
const clearQueryStateType = `${actionPrefix}clearQueryState`;
|
|
34
|
-
/** Clear states for provided query keys and cache keys.
|
|
35
|
-
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
36
31
|
const clearQueryState = (queryKeys) => ({
|
|
37
32
|
type: clearQueryStateType,
|
|
38
33
|
queryKeys,
|
|
39
34
|
});
|
|
40
35
|
clearQueryState.type = clearQueryStateType;
|
|
41
36
|
const clearMutationStateType = `${actionPrefix}clearMutationState`;
|
|
42
|
-
/** Clear states for provided mutation keys. */
|
|
43
37
|
const clearMutationState = (mutationKeys) => ({
|
|
44
38
|
type: clearMutationStateType,
|
|
45
39
|
mutationKeys,
|
|
46
40
|
});
|
|
47
41
|
clearMutationState.type = clearMutationStateType;
|
|
48
42
|
return {
|
|
43
|
+
/** Updates query state, and optionally merges entity changes in a single action. */
|
|
49
44
|
updateQueryStateAndEntities,
|
|
45
|
+
/** Updates mutation state, and optionally merges entity changes in a single action. */
|
|
50
46
|
updateMutationStateAndEntities,
|
|
47
|
+
/** Merge EntityChanges to the state. */
|
|
51
48
|
mergeEntityChanges,
|
|
49
|
+
/** Clear states for provided query keys and cache keys.
|
|
50
|
+
* If cache key for query key is not provided, the whole state for query key is cleared. */
|
|
52
51
|
clearQueryState,
|
|
52
|
+
/** Clear states for provided mutation keys. */
|
|
53
53
|
clearMutationState,
|
|
54
54
|
};
|
|
55
55
|
};
|
package/dist/createCache.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Cache,
|
|
1
|
+
import type { Cache, Key, MutationResult, OptionalPartial, QueryMutationState, QueryOptions, QueryResult, Typenames } from './types';
|
|
2
2
|
import { useMutation } from './useMutation';
|
|
3
3
|
import { useQuery } from './useQuery';
|
|
4
4
|
import { applyEntityChanges } from './utilsAndConstants';
|
|
@@ -6,10 +6,11 @@ import { applyEntityChanges } from './utilsAndConstants';
|
|
|
6
6
|
* Creates reducer, actions and hooks for managing queries and mutations through redux cache.
|
|
7
7
|
*/
|
|
8
8
|
export declare const createCache: <N extends string, T extends Typenames, QP, QR, MP, MR>(partialCache: OptionalPartial<Cache<N, T, QP, QR, MP, MR>, "options" | "queries" | "mutations" | "cacheStateSelector">) => {
|
|
9
|
+
/** Keeps all options, passed while creating the cache. */
|
|
9
10
|
cache: Cache<N, T, QP, QR, MP, MR>;
|
|
10
11
|
/** Reducer of the cache, should be added to redux store. */
|
|
11
12
|
reducer: (state: {
|
|
12
|
-
entities: EntitiesMap<T>;
|
|
13
|
+
entities: import("./types").EntitiesMap<T>;
|
|
13
14
|
queries: { [QK in keyof QR]: import("./types").Dict<QueryMutationState<QR[QK]>>; };
|
|
14
15
|
mutations: { [MK in keyof MR]: QueryMutationState<MR[MK]>; };
|
|
15
16
|
} | undefined, action: {
|
|
@@ -36,7 +37,7 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
36
37
|
type: `@rrc/${N}/clearMutationState`;
|
|
37
38
|
mutationKeys: (keyof MR)[];
|
|
38
39
|
}) => {
|
|
39
|
-
entities: EntitiesMap<T>;
|
|
40
|
+
entities: import("./types").EntitiesMap<T>;
|
|
40
41
|
queries: { [QK in keyof QR]: import("./types").Dict<QueryMutationState<QR[QK]>>; };
|
|
41
42
|
mutations: { [MK in keyof MR]: QueryMutationState<MR[MK]>; };
|
|
42
43
|
};
|
|
@@ -86,24 +87,34 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
86
87
|
mutationKeys: K_3[];
|
|
87
88
|
};
|
|
88
89
|
type: `@rrc/${N}/clearMutationState`;
|
|
89
|
-
};
|
|
90
|
+
}; /** Selects query latest result. */
|
|
90
91
|
};
|
|
91
92
|
selectors: {
|
|
93
|
+
/** Selects query state. */
|
|
92
94
|
selectQueryState: <QK_1 extends keyof QR | keyof QP>(state: unknown, query: QK_1, cacheKey: Key) => QueryMutationState<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never> | undefined;
|
|
95
|
+
/** Selects query latest result. */
|
|
93
96
|
selectQueryResult: <QK_2 extends keyof QR | keyof QP>(state: unknown, query: QK_2, cacheKey: Key) => (QK_2 extends keyof QP & keyof QR ? QR[QK_2] : never) | undefined;
|
|
97
|
+
/** Selects query loading state. */
|
|
94
98
|
selectQueryLoading: <QK_3 extends keyof QR | keyof QP>(state: unknown, query: QK_3, cacheKey: Key) => boolean | undefined;
|
|
99
|
+
/** Selects query latest error. */
|
|
95
100
|
selectQueryError: <QK_4 extends keyof QR | keyof QP>(state: unknown, query: QK_4, cacheKey: Key) => Error | undefined;
|
|
101
|
+
/** Selects mutation state. */
|
|
96
102
|
selectMutationState: <MK_1 extends keyof MP | keyof MR>(state: unknown, mutation: MK_1) => QueryMutationState<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>;
|
|
103
|
+
/** Selects mutation latest result. */
|
|
97
104
|
selectMutationResult: <MK_2 extends keyof MP | keyof MR>(state: unknown, mutation: MK_2) => (MK_2 extends keyof MP & keyof MR ? MR[MK_2] : never) | undefined;
|
|
105
|
+
/** Selects mutation loading state. */
|
|
98
106
|
selectMutationLoading: <MK_3 extends keyof MP | keyof MR>(state: unknown, mutation: MK_3) => boolean;
|
|
107
|
+
/** Selects mutation latest error. */
|
|
99
108
|
selectMutationError: <MK_4 extends keyof MP | keyof MR>(state: unknown, mutation: MK_4) => Error | undefined;
|
|
100
|
-
/**
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
|
|
109
|
+
/** Selects entity by id and typename. */
|
|
110
|
+
selectEntityById: <TN extends keyof T>(state: unknown, id: Key | null | undefined, typename: TN) => T[TN] | undefined;
|
|
111
|
+
/** Selects all entities. */
|
|
112
|
+
selectEntities: (state: unknown) => import("./types").EntitiesMap<T>;
|
|
113
|
+
/** Selects all entities of provided typename. */
|
|
114
|
+
selectEntitiesByTypename: <TN_1 extends keyof T>(state: unknown, typename: TN_1) => import("./types").EntitiesMap<T>[TN_1];
|
|
104
115
|
};
|
|
105
116
|
hooks: {
|
|
106
|
-
/** Returns client object with query
|
|
117
|
+
/** Returns client object with query and mutate functions. */
|
|
107
118
|
useClient: () => {
|
|
108
119
|
query: <QK_5 extends keyof QR | keyof QP>(options: Omit<QueryOptions<T, QP, QR, MR, QK_5>, "cachePolicy">) => Promise<QueryResult<QK_5 extends keyof QP & keyof QR ? QR[QK_5] : never>>;
|
|
109
120
|
mutate: <MK_5 extends keyof MP | keyof MR>(options: {
|
|
@@ -117,10 +128,12 @@ export declare const createCache: <N extends string, T extends Typenames, QP, QR
|
|
|
117
128
|
useMutation: <MK_6 extends keyof MP | keyof MR>(options: {
|
|
118
129
|
mutation: MK_6;
|
|
119
130
|
}) => readonly [(params: MK_6 extends keyof MP & keyof MR ? MP[MK_6] : never) => Promise<MutationResult<MK_6 extends infer T_2 ? T_2 extends MK_6 ? T_2 extends keyof MP & keyof MR ? MR[T_2] : never : never : never>>, QueryMutationState<MK_6 extends keyof MP & keyof MR ? MP[MK_6] : never>, () => boolean];
|
|
120
|
-
/** Selects entity by id and subscribes to the changes. */
|
|
121
|
-
useSelectEntityById: <TN_1 extends keyof T>(id: Key | null | undefined, typename: TN_1) => T[TN_1] | undefined;
|
|
122
131
|
};
|
|
123
132
|
utils: {
|
|
124
|
-
|
|
133
|
+
/**
|
|
134
|
+
* Apply changes to the entities map.
|
|
135
|
+
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
136
|
+
*/
|
|
137
|
+
applyEntityChanges: (entities: import("./types").EntitiesMap<T>, changes: import("./types").EntityChanges<T>) => import("./types").EntitiesMap<T> | undefined;
|
|
125
138
|
};
|
|
126
139
|
};
|
package/dist/createCache.js
CHANGED
|
@@ -29,13 +29,6 @@ const createCache = (partialCache) => {
|
|
|
29
29
|
partialCache.abortControllers = abortControllers;
|
|
30
30
|
const cache = partialCache;
|
|
31
31
|
// make selectors
|
|
32
|
-
const entitiesSelector = (state) => {
|
|
33
|
-
return cache.cacheStateSelector(state).entities;
|
|
34
|
-
};
|
|
35
|
-
const enitityMapSelectorByTypename = Object.keys(partialCache.typenames).reduce((result, x) => {
|
|
36
|
-
result[x] = (state) => cache.cacheStateSelector(state).entities[x];
|
|
37
|
-
return result;
|
|
38
|
-
}, {});
|
|
39
32
|
const selectQueryState = (state, query, cacheKey) => {
|
|
40
33
|
// @ts-expect-error fix later
|
|
41
34
|
return cache.cacheStateSelector(state).queries[query][cacheKey];
|
|
@@ -46,43 +39,58 @@ const createCache = (partialCache) => {
|
|
|
46
39
|
};
|
|
47
40
|
const actions = (0, createActions_1.createActions)(cache.name);
|
|
48
41
|
return {
|
|
42
|
+
/** Keeps all options, passed while creating the cache. */
|
|
49
43
|
cache,
|
|
50
44
|
/** Reducer of the cache, should be added to redux store. */
|
|
51
45
|
reducer: (0, reducer_1.createCacheReducer)(actions, cache.typenames, Object.keys(cache.queries), cache.options),
|
|
52
46
|
actions,
|
|
53
47
|
selectors: {
|
|
48
|
+
/** Selects query state. */
|
|
54
49
|
selectQueryState,
|
|
50
|
+
/** Selects query latest result. */
|
|
55
51
|
selectQueryResult: (state, query, cacheKey) => {
|
|
56
52
|
var _a;
|
|
57
53
|
return (_a = selectQueryState(state, query, cacheKey)) === null || _a === void 0 ? void 0 : _a.result;
|
|
58
54
|
},
|
|
55
|
+
/** Selects query loading state. */
|
|
59
56
|
selectQueryLoading: (state, query, cacheKey) => {
|
|
60
57
|
var _a;
|
|
61
58
|
return (_a = selectQueryState(state, query, cacheKey)) === null || _a === void 0 ? void 0 : _a.loading;
|
|
62
59
|
},
|
|
60
|
+
/** Selects query latest error. */
|
|
63
61
|
selectQueryError: (state, query, cacheKey) => {
|
|
64
62
|
var _a;
|
|
65
63
|
return (_a = selectQueryState(state, query, cacheKey)) === null || _a === void 0 ? void 0 : _a.error;
|
|
66
64
|
},
|
|
65
|
+
/** Selects mutation state. */
|
|
67
66
|
selectMutationState,
|
|
67
|
+
/** Selects mutation latest result. */
|
|
68
68
|
selectMutationResult: (state, mutation) => {
|
|
69
69
|
return selectMutationState(state, mutation).result;
|
|
70
70
|
},
|
|
71
|
+
/** Selects mutation loading state. */
|
|
71
72
|
selectMutationLoading: (state, mutation) => {
|
|
72
73
|
return selectMutationState(state, mutation).loading;
|
|
73
74
|
},
|
|
75
|
+
/** Selects mutation latest error. */
|
|
74
76
|
selectMutationError: (state, mutation) => {
|
|
75
77
|
return selectMutationState(state, mutation).error;
|
|
76
78
|
},
|
|
77
|
-
/**
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
/** Selects entity by id and typename. */
|
|
80
|
+
selectEntityById: (state, id, typename) => {
|
|
81
|
+
return id == null ? undefined : cache.cacheStateSelector(state).entities[typename][id];
|
|
82
|
+
},
|
|
83
|
+
/** Selects all entities. */
|
|
84
|
+
selectEntities: (state) => {
|
|
85
|
+
return cache.cacheStateSelector(state).entities;
|
|
86
|
+
},
|
|
87
|
+
/** Selects all entities of provided typename. */
|
|
88
|
+
selectEntitiesByTypename: (state, typename) => {
|
|
89
|
+
return cache.cacheStateSelector(state).entities[typename];
|
|
82
90
|
},
|
|
83
91
|
},
|
|
84
92
|
hooks: {
|
|
85
|
-
/** Returns client object with query
|
|
93
|
+
/** Returns client object with query and mutate functions. */
|
|
86
94
|
useClient: () => {
|
|
87
95
|
const store = (0, react_redux_1.useStore)();
|
|
88
96
|
return (0, react_1.useMemo)(() => {
|
|
@@ -106,14 +114,12 @@ const createCache = (partialCache) => {
|
|
|
106
114
|
useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, options),
|
|
107
115
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
108
116
|
useMutation: (options) => (0, useMutation_1.useMutation)(cache, actions, options, abortControllers),
|
|
109
|
-
/** Selects entity by id and subscribes to the changes. */
|
|
110
|
-
useSelectEntityById: (id, typename) => {
|
|
111
|
-
return (0, react_redux_1.useSelector)((state) =>
|
|
112
|
-
// TODO move to selectors?
|
|
113
|
-
id == null ? undefined : cache.cacheStateSelector(state).entities[typename][id]);
|
|
114
|
-
},
|
|
115
117
|
},
|
|
116
118
|
utils: {
|
|
119
|
+
/**
|
|
120
|
+
* Apply changes to the entities map.
|
|
121
|
+
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
122
|
+
*/
|
|
117
123
|
applyEntityChanges: (entities, changes) => {
|
|
118
124
|
return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options);
|
|
119
125
|
},
|
package/dist/index.js
CHANGED
|
@@ -23,12 +23,11 @@ Object.defineProperty(exports, "defaultGetCacheKey", { enumerable: true, get: fu
|
|
|
23
23
|
Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.DEFAULT_QUERY_MUTATION_STATE; } });
|
|
24
24
|
// Backlog
|
|
25
25
|
// ! high
|
|
26
|
-
// add example
|
|
26
|
+
// add not optimized not normalized example, and not normalized state example to readme
|
|
27
27
|
// try use skip for refreshing strategy?
|
|
28
28
|
// optimistic response
|
|
29
|
-
// make query key / cache key difference more clear in the docs, or
|
|
29
|
+
// make query key / cache key difference more clear in the docs, and/or rename queryKey -> query
|
|
30
30
|
// ! medium
|
|
31
|
-
// make named caches to produce named hooks, actions etc (same as slices in RTK)?
|
|
32
31
|
// allow multiple mutation with same keys?
|
|
33
32
|
// type extractors from cache
|
|
34
33
|
// custom useStore
|
|
@@ -42,6 +41,7 @@ Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true,
|
|
|
42
41
|
// make error type generic
|
|
43
42
|
// don't cache result if resultSelector set? throw error if mergeResult set with resultSelector?
|
|
44
43
|
// ! low
|
|
44
|
+
// access to currently loading queries and mutations?
|
|
45
45
|
// add params to the state?
|
|
46
46
|
// cancellation to queries
|
|
47
47
|
// if mutation & query alrady loading - make options: last, throttle, debounce, parallel?
|
package/dist/types.d.ts
CHANGED
|
@@ -121,7 +121,7 @@ export type MutationResult<R> = {
|
|
|
121
121
|
export type QueryMutationState<R> = {
|
|
122
122
|
/** `true` when query or mutation is currently in progress. */
|
|
123
123
|
loading: boolean;
|
|
124
|
-
/** Result of the latest successfull
|
|
124
|
+
/** Result of the latest successfull response. */
|
|
125
125
|
result?: R;
|
|
126
126
|
/** Error of the latest response. */
|
|
127
127
|
error?: Error;
|
|
@@ -7,8 +7,4 @@ export declare const DEFAULT_QUERY_MUTATION_STATE: {
|
|
|
7
7
|
};
|
|
8
8
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key;
|
|
9
9
|
export declare const log: (tag: string, data?: unknown) => void;
|
|
10
|
-
/**
|
|
11
|
-
* Apply changes to the entities map.
|
|
12
|
-
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
13
|
-
*/
|
|
14
10
|
export declare const applyEntityChanges: <T extends Typenames>(entities: EntitiesMap<T>, changes: EntityChanges<T>, options: CacheOptions) => EntitiesMap<T> | undefined;
|
|
@@ -28,10 +28,6 @@ const log = (tag, data) => {
|
|
|
28
28
|
console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data);
|
|
29
29
|
};
|
|
30
30
|
exports.log = log;
|
|
31
|
-
/**
|
|
32
|
-
* Apply changes to the entities map.
|
|
33
|
-
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
34
|
-
*/
|
|
35
31
|
const applyEntityChanges = (entities, changes, options) => {
|
|
36
32
|
var _a, _b, _c;
|
|
37
33
|
if (options.validateFunctionArguments) {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "react-redux-cache",
|
|
3
3
|
"author": "Alexander Danilov",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.4.1",
|
|
6
6
|
"description": "Powerful data fetching and caching library that supports normalization, built on top of redux",
|
|
7
7
|
"main": "dist/index.js",
|
|
8
8
|
"types": "dist/index.d.ts",
|