react-redux-cache 0.0.10 → 0.0.11
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 +102 -15
- package/dist/createCache.d.ts +5 -3
- package/dist/createCache.js +16 -22
- package/dist/index.d.ts +1 -3
- package/dist/index.js +2 -10
- package/dist/mutate.d.ts +2 -4
- package/dist/mutate.js +18 -26
- package/dist/query.d.ts +2 -2
- package/dist/query.js +16 -19
- package/dist/reducer.js +3 -3
- package/dist/types.d.ts +4 -22
- package/dist/useMutation.d.ts +3 -4
- package/dist/useMutation.js +12 -18
- package/dist/useQuery.d.ts +1 -7
- package/dist/useQuery.js +7 -28
- package/dist/utilsAndConstants.d.ts +3 -8
- package/dist/utilsAndConstants.js +6 -11
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# react-redux-cache
|
|
2
2
|
|
|
3
|
-
**Powerful** yet **lightweight** data fetching and caching library that supports **normalization** unlike `react-query` and `rtk-query`, while having similar interface. Built on top of `redux`.
|
|
3
|
+
**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. Can be considered as `ApolloClient` for protocols other than `GraphQL`.
|
|
4
4
|
|
|
5
|
-
**Normalization** is the
|
|
5
|
+
**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**.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Remains **full control** of redux state with ability to write custom selectors, actions and reducers to manage cached state.
|
|
8
8
|
|
|
9
9
|
Usage example can be found in `example/` folder and run by `npm run example` command from the root folder.
|
|
10
10
|
|
|
@@ -17,29 +17,33 @@ Usage example can be found in `example/` folder and run by `npm run example` com
|
|
|
17
17
|
- [api.ts](https://github.com/gentlee/react-redux-cache#apits)
|
|
18
18
|
- [Usage](https://github.com/gentlee/react-redux-cache#usage)
|
|
19
19
|
- [Advanced](https://github.com/gentlee/react-redux-cache#advanced)
|
|
20
|
+
- [Infinite scroll pagination](https://github.com/gentlee/react-redux-cache#infinite-scroll-pagination)
|
|
20
21
|
- [redux-persist](https://github.com/gentlee/react-redux-cache#redux-persist)
|
|
21
22
|
|
|
22
23
|
### Installation
|
|
23
|
-
`react` and `redux` are peer dependencies.
|
|
24
|
+
`react`, `redux` and `react-redux` are peer dependencies.
|
|
24
25
|
```sh
|
|
25
|
-
npm add react-redux-cache react redux
|
|
26
|
+
npm add react-redux-cache react redux react-redux
|
|
26
27
|
```
|
|
27
28
|
### Initialization
|
|
28
|
-
|
|
29
|
-
All queries and mutations should be passed while initializing the cache
|
|
30
|
-
In this example we omit usage of actions and selectors.
|
|
29
|
+
The only function that needs to be imported is `createCache`, which creates fully typed reducer, hooks, actions, selectors and utils to be used in the app.
|
|
30
|
+
All typenames, queries and mutations should be passed while initializing the cache for proper typing.
|
|
31
31
|
#### cache.ts
|
|
32
32
|
```typescript
|
|
33
33
|
export const {
|
|
34
34
|
reducer,
|
|
35
|
-
hooks: {useMutation, useQuery, useSelectEntityById},
|
|
35
|
+
hooks: {useClient, useMutation, useQuery, useSelectEntityById},
|
|
36
|
+
// Actions, selectors and utils may be not used at all
|
|
37
|
+
selectors: {entitiesSelector, entitiesByTypenameSelector},
|
|
38
|
+
actions: {setQueryStateAndEntities, setMutationStateAndEntities, mergeEntityChanges},
|
|
39
|
+
utils: {applyEntityChanges},
|
|
36
40
|
} = createCache({
|
|
37
|
-
// This selector should
|
|
41
|
+
// This selector should return the cache state based on the path to its reducer.
|
|
38
42
|
cacheStateSelector: (state) => state.cache,
|
|
39
|
-
// Typenames provide a mapping of all typenames to their entity types.
|
|
43
|
+
// Typenames provide a mapping of all typenames to their entity types, which is needed for normalization.
|
|
40
44
|
// Empty objects with type casting can be used as values.
|
|
41
45
|
typenames: {
|
|
42
|
-
users: {} as User,
|
|
46
|
+
users: {} as User, // here `users` entities will have type `User`
|
|
43
47
|
banks: {} as Bank,
|
|
44
48
|
},
|
|
45
49
|
queries: {
|
|
@@ -54,6 +58,8 @@ export const {
|
|
|
54
58
|
```
|
|
55
59
|
#### store.ts
|
|
56
60
|
```typescript
|
|
61
|
+
// Create store as usual, passing the new cache reducer
|
|
62
|
+
// under the key, previously used in cacheStateSelector
|
|
57
63
|
const store = configureStore({
|
|
58
64
|
reducer: {
|
|
59
65
|
cache: reducer,
|
|
@@ -65,11 +71,16 @@ Query result should be of type `QueryResponse`, mutation result should be of typ
|
|
|
65
71
|
For normalization `normalizr` package is used in this example, but any other tool can be used if query result is of proper type.
|
|
66
72
|
Perfect implementation is when the backend already returns normalized data.
|
|
67
73
|
```typescript
|
|
74
|
+
|
|
75
|
+
// Example of query with normalization (recommended)
|
|
76
|
+
|
|
68
77
|
export const getUser = async (id: number) => {
|
|
69
78
|
const result = await ...
|
|
70
79
|
|
|
71
80
|
const normalizedResult: {
|
|
81
|
+
// result is id of the user
|
|
72
82
|
result: number
|
|
83
|
+
// entities contain all normalized objects
|
|
73
84
|
entities: {
|
|
74
85
|
users: Record<number, User>
|
|
75
86
|
banks: Record<string, Bank>
|
|
@@ -79,6 +90,15 @@ export const getUser = async (id: number) => {
|
|
|
79
90
|
return normalizedResult
|
|
80
91
|
}
|
|
81
92
|
|
|
93
|
+
// Example of query without normalization (not recommended)
|
|
94
|
+
|
|
95
|
+
export const getBank = (id: string) => {
|
|
96
|
+
const result: Bank = ...
|
|
97
|
+
return {result} // result is bank object, no entities passed
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Example of mutation with normalization
|
|
101
|
+
|
|
82
102
|
export const removeUser = async (id: number) => {
|
|
83
103
|
await ...
|
|
84
104
|
return {
|
|
@@ -93,8 +113,8 @@ export const removeUser = async (id: number) => {
|
|
|
93
113
|
export const UserScreen = () => {
|
|
94
114
|
const {id} = useParams()
|
|
95
115
|
|
|
96
|
-
// useQuery
|
|
97
|
-
// telling
|
|
116
|
+
// useQuery connects to redux state and if user with that id is already cached, fetch won't happen (with default cachePolicy 'cache-first')
|
|
117
|
+
// Infers all types from created cache, telling here that params and result are of type `number`.
|
|
98
118
|
const [{result: userId, loading, error}] = useQuery({
|
|
99
119
|
query: 'getUser',
|
|
100
120
|
params: Number(id),
|
|
@@ -104,7 +124,7 @@ export const UserScreen = () => {
|
|
|
104
124
|
mutation: 'updateUser',
|
|
105
125
|
})
|
|
106
126
|
|
|
107
|
-
// This selector is
|
|
127
|
+
// This selector is used for denormalization and returns entities with proper types - User and Bank
|
|
108
128
|
const user = useSelectEntityById(userId, 'users')
|
|
109
129
|
const bank = useSelectEntityById(user?.bankId, 'banks')
|
|
110
130
|
|
|
@@ -117,6 +137,73 @@ export const UserScreen = () => {
|
|
|
117
137
|
```
|
|
118
138
|
|
|
119
139
|
### Advanced
|
|
140
|
+
|
|
141
|
+
#### Infinite scroll pagination
|
|
142
|
+
|
|
143
|
+
Here is an example of `getUsers` query configuration with pagination support. You can check full implementation in `/example` folder.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
// createCache
|
|
147
|
+
|
|
148
|
+
...
|
|
149
|
+
} = createCache({
|
|
150
|
+
...
|
|
151
|
+
queries: {
|
|
152
|
+
getUsers: {
|
|
153
|
+
query: getUsers,
|
|
154
|
+
getCacheKey: () => 'all-pages', // single cache key is used for all pages
|
|
155
|
+
mergeResults: (oldResult, {result: newResult}) => {
|
|
156
|
+
if (!oldResult || newResult.page === 1) {
|
|
157
|
+
return newResult
|
|
158
|
+
}
|
|
159
|
+
if (newResult.page === oldResult.page + 1) {
|
|
160
|
+
return {
|
|
161
|
+
...newResult,
|
|
162
|
+
items: [...oldResult.items, ...newResult.items],
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return oldResult
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
...
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
// Component
|
|
173
|
+
|
|
174
|
+
export const GetUsersScreen = () => {
|
|
175
|
+
const {query} = useClient()
|
|
176
|
+
|
|
177
|
+
const [{result: usersResult, loading, error}, refetch] = useQuery({
|
|
178
|
+
query: 'getUsers',
|
|
179
|
+
params: 1 // page
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
const onLoadNextPage = () => {
|
|
183
|
+
const lastLoadedPage = usersResult?.page ?? 0
|
|
184
|
+
query({
|
|
185
|
+
query: 'getUsers',
|
|
186
|
+
params: lastLoadedPage + 1,
|
|
187
|
+
})
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const renderUser = (userId: number) => (
|
|
191
|
+
<UserRow key={userId} userId={userId}>
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
...
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<div>
|
|
198
|
+
{usersResult?.items.map(renderUser)}
|
|
199
|
+
<button onClick={refetch}>Refresh</button>
|
|
200
|
+
<button onClick={onLoadNextPage}>Load next page</button>
|
|
201
|
+
</div>
|
|
202
|
+
)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
```
|
|
206
|
+
|
|
120
207
|
#### redux-persist
|
|
121
208
|
|
|
122
209
|
Here is a simple `redux-persist` configuration:
|
package/dist/createCache.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { mergeEntityChanges, setMutationStateAndEntities, setQueryStateAndEntities } from './reducer';
|
|
2
|
-
import { Cache, EntitiesMap, Key,
|
|
2
|
+
import { Cache, EntitiesMap, Key, MutationResult, OptionalPartial, QueryOptions, QueryResult, Typenames } from './types';
|
|
3
3
|
import { useMutation } from './useMutation';
|
|
4
4
|
import { useQuery } from './useQuery';
|
|
5
|
+
import { applyEntityChanges } from './utilsAndConstants';
|
|
5
6
|
/**
|
|
6
7
|
* Creates reducer, actions and hooks for managing queries and mutations through redux cache.
|
|
7
8
|
*/
|
|
@@ -64,7 +65,6 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
|
|
|
64
65
|
mutate: <MK_1 extends keyof MP | keyof MR>(options: {
|
|
65
66
|
mutation: MK_1;
|
|
66
67
|
params: MK_1 extends keyof MP & keyof MR ? MP[MK_1] : never;
|
|
67
|
-
cacheOptions?: MutationCacheOptions | undefined;
|
|
68
68
|
}) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>;
|
|
69
69
|
};
|
|
70
70
|
/** Fetches query when params change and subscribes to query state. */
|
|
@@ -72,9 +72,11 @@ export declare const createCache: <T extends Typenames, QP, QR, MP, MR>(cache: O
|
|
|
72
72
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
73
73
|
useMutation: <MK_2 extends keyof MP | keyof MR>(options: {
|
|
74
74
|
mutation: MK_2;
|
|
75
|
-
cacheOptions?: MutationCacheOptions | undefined;
|
|
76
75
|
}) => readonly [(params: MK_2 extends keyof MP & keyof MR ? MP[MK_2] : never) => Promise<void>, import("./types").QueryMutationState<MK_2 extends keyof MP & keyof MR ? MP[MK_2] : never>, () => boolean];
|
|
77
76
|
/** Selects entity by id and subscribes to the changes. */
|
|
78
77
|
useSelectEntityById: <K_3 extends keyof T>(id: Key | null | undefined, typename: K_3) => T[K_3] | undefined;
|
|
79
78
|
};
|
|
79
|
+
utils: {
|
|
80
|
+
applyEntityChanges: (entities: EntitiesMap<T>, changes: import("./types").EntityChanges<T>) => EntitiesMap<T> | undefined;
|
|
81
|
+
};
|
|
80
82
|
};
|
package/dist/createCache.js
CHANGED
|
@@ -17,19 +17,16 @@ const createCache = (cache) => {
|
|
|
17
17
|
var _g, _h, _j;
|
|
18
18
|
// @ts-expect-error hot
|
|
19
19
|
const hotReloadEnabled = Boolean(module === null || module === void 0 ? void 0 : module.hot);
|
|
20
|
+
const abortControllers = new WeakMap();
|
|
20
21
|
// provide all optional fields
|
|
21
|
-
// and transform cacheOptions from QueryCachePolicy to QueryCacheOptions
|
|
22
22
|
(_a = cache.options) !== null && _a !== void 0 ? _a : (cache.options = {});
|
|
23
23
|
(_b = (_g = cache.options).logsEnabled) !== null && _b !== void 0 ? _b : (_g.logsEnabled = false);
|
|
24
24
|
(_c = (_h = cache.options).validateFunctionArguments) !== null && _c !== void 0 ? _c : (_h.validateFunctionArguments = utilsAndConstants_1.isDev);
|
|
25
25
|
(_d = (_j = cache.options).validateHookArguments) !== null && _d !== void 0 ? _d : (_j.validateHookArguments = utilsAndConstants_1.isDev && !hotReloadEnabled);
|
|
26
26
|
(_e = cache.queries) !== null && _e !== void 0 ? _e : (cache.queries = {});
|
|
27
27
|
(_f = cache.mutations) !== null && _f !== void 0 ? _f : (cache.mutations = {});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
queryInfo.cacheOptions = useQuery_1.queryCacheOptionsByPolicy[queryInfo.cacheOptions];
|
|
31
|
-
}
|
|
32
|
-
}
|
|
28
|
+
// @ts-expect-error for testing
|
|
29
|
+
cache.abortControllers = abortControllers;
|
|
33
30
|
const nonPartialCache = cache;
|
|
34
31
|
// make selectors
|
|
35
32
|
const entitiesSelector = (state) => {
|
|
@@ -65,23 +62,15 @@ const createCache = (cache) => {
|
|
|
65
62
|
const client = {
|
|
66
63
|
query: (options) => {
|
|
67
64
|
var _a, _b;
|
|
68
|
-
const { query: queryKey, params
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const getParamsKey = (_b = nonPartialCache.queries[queryKey].getParamsKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetParamsKey);
|
|
75
|
-
const cacheKey = getCacheKey
|
|
76
|
-
? // @ts-expect-error fix later
|
|
77
|
-
getCacheKey(params)
|
|
78
|
-
: // @ts-expect-error fix later
|
|
79
|
-
getParamsKey(params);
|
|
80
|
-
return (0, query_1.query)('query', true, store, nonPartialCache, queryKey, cacheKey, cacheOptions, params);
|
|
65
|
+
const { query: queryKey, params } = options;
|
|
66
|
+
const getParamsKey = (_a = nonPartialCache.queries[queryKey].getParamsKey) !== null && _a !== void 0 ? _a : (utilsAndConstants_1.defaultGetParamsKey);
|
|
67
|
+
const getCacheKey = (_b = nonPartialCache.queries[queryKey].getCacheKey) !== null && _b !== void 0 ? _b : getParamsKey;
|
|
68
|
+
// @ts-expect-error fix later
|
|
69
|
+
const cacheKey = getCacheKey(params);
|
|
70
|
+
return (0, query_1.query)('query', true, store, nonPartialCache, queryKey, cacheKey, params);
|
|
81
71
|
},
|
|
82
72
|
mutate: (options) => {
|
|
83
|
-
|
|
84
|
-
return (0, mutate_1.mutate)('mutate', true, store, nonPartialCache, options.mutation, (_a = options.cacheOptions) !== null && _a !== void 0 ? _a : useMutation_1.defaultMutationCacheOptions, options.params);
|
|
73
|
+
return (0, mutate_1.mutate)('mutate', true, store, nonPartialCache, options.mutation, options.params, abortControllers);
|
|
85
74
|
},
|
|
86
75
|
};
|
|
87
76
|
return client;
|
|
@@ -90,12 +79,17 @@ const createCache = (cache) => {
|
|
|
90
79
|
/** Fetches query when params change and subscribes to query state. */
|
|
91
80
|
useQuery: (options) => (0, useQuery_1.useQuery)(nonPartialCache, options),
|
|
92
81
|
/** Subscribes to provided mutation state and provides mutate function. */
|
|
93
|
-
useMutation: (options) => (0, useMutation_1.useMutation)(nonPartialCache, options),
|
|
82
|
+
useMutation: (options) => (0, useMutation_1.useMutation)(nonPartialCache, options, abortControllers),
|
|
94
83
|
/** Selects entity by id and subscribes to the changes. */
|
|
95
84
|
useSelectEntityById: (id, typename) => {
|
|
96
85
|
return (0, react_redux_1.useSelector)((state) => id == null ? undefined : nonPartialCache.cacheStateSelector(state).entities[typename][id]);
|
|
97
86
|
},
|
|
98
87
|
},
|
|
88
|
+
utils: {
|
|
89
|
+
applyEntityChanges: (entities, changes) => {
|
|
90
|
+
return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, nonPartialCache.options);
|
|
91
|
+
},
|
|
92
|
+
},
|
|
99
93
|
};
|
|
100
94
|
};
|
|
101
95
|
exports.createCache = createCache;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
export { createCache } from './createCache';
|
|
2
2
|
export type { ReduxCacheState } from './reducer';
|
|
3
3
|
export * from './types';
|
|
4
|
-
export {
|
|
5
|
-
export { defaultQueryCacheOptions, queryCacheOptionsByPolicy } from './useQuery';
|
|
6
|
-
export { defaultCacheOptions, defaultGetParamsKey, defaultQueryMutationState, processEntityChanges, } from './utilsAndConstants';
|
|
4
|
+
export { defaultGetParamsKey, defaultQueryMutationState } from './utilsAndConstants';
|
package/dist/index.js
CHANGED
|
@@ -14,26 +14,18 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.defaultQueryMutationState = exports.defaultGetParamsKey = exports.createCache = void 0;
|
|
18
18
|
var createCache_1 = require("./createCache");
|
|
19
19
|
Object.defineProperty(exports, "createCache", { enumerable: true, get: function () { return createCache_1.createCache; } });
|
|
20
20
|
__exportStar(require("./types"), exports);
|
|
21
|
-
var useMutation_1 = require("./useMutation");
|
|
22
|
-
Object.defineProperty(exports, "defaultMutationCacheOptions", { enumerable: true, get: function () { return useMutation_1.defaultMutationCacheOptions; } });
|
|
23
|
-
var useQuery_1 = require("./useQuery");
|
|
24
|
-
Object.defineProperty(exports, "defaultQueryCacheOptions", { enumerable: true, get: function () { return useQuery_1.defaultQueryCacheOptions; } });
|
|
25
|
-
Object.defineProperty(exports, "queryCacheOptionsByPolicy", { enumerable: true, get: function () { return useQuery_1.queryCacheOptionsByPolicy; } });
|
|
26
21
|
var utilsAndConstants_1 = require("./utilsAndConstants");
|
|
27
|
-
Object.defineProperty(exports, "defaultCacheOptions", { enumerable: true, get: function () { return utilsAndConstants_1.defaultCacheOptions; } });
|
|
28
22
|
Object.defineProperty(exports, "defaultGetParamsKey", { enumerable: true, get: function () { return utilsAndConstants_1.defaultGetParamsKey; } });
|
|
29
23
|
Object.defineProperty(exports, "defaultQueryMutationState", { enumerable: true, get: function () { return utilsAndConstants_1.defaultQueryMutationState; } });
|
|
30
|
-
Object.defineProperty(exports, "processEntityChanges", { enumerable: true, get: function () { return utilsAndConstants_1.processEntityChanges; } });
|
|
31
24
|
// Backlog
|
|
32
25
|
// ! high
|
|
33
|
-
// mutate
|
|
34
26
|
// cover with tests
|
|
35
|
-
// suport persist (no loading etc)
|
|
36
27
|
// ! medium
|
|
28
|
+
// allow multiple mutation with sam keys?
|
|
37
29
|
// type extractors from cache
|
|
38
30
|
// custom useStore
|
|
39
31
|
// return back deserialize selector?
|
package/dist/mutate.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
|
-
import { Cache, Key,
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const getAbortController: (store: Store, mutationKey: Key) => AbortController | undefined;
|
|
5
|
-
export declare const mutate: <T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof MR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, mutationKey: MK, cacheOptions: MutationCacheOptions, params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void | MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
|
2
|
+
import { Cache, Key, MutationResult, Typenames } from './types';
|
|
3
|
+
export declare const mutate: <T extends Typenames, QP, QR, MP, MR, MK extends keyof MP | keyof MR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, mutationKey: MK, params: MK extends keyof MP & keyof MR ? MP[MK] : never, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => Promise<void | MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>;
|
package/dist/mutate.js
CHANGED
|
@@ -9,17 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.mutate =
|
|
12
|
+
exports.mutate = void 0;
|
|
13
13
|
const reducer_1 = require("./reducer");
|
|
14
14
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
exports.getAbortController = getAbortController;
|
|
18
|
-
const mutate = (logTag, returnResult, store, cache, mutationKey, cacheOptions, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
let abortControllersOfStore = exports.abortControllers.get(store);
|
|
15
|
+
const mutate = (logTag, returnResult, store, cache, mutationKey, params, abortControllers) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
let abortControllersOfStore = abortControllers.get(store);
|
|
20
17
|
if (abortControllersOfStore === undefined) {
|
|
21
18
|
abortControllersOfStore = {};
|
|
22
|
-
|
|
19
|
+
abortControllers.set(store, abortControllersOfStore);
|
|
23
20
|
}
|
|
24
21
|
{
|
|
25
22
|
const abortController = abortControllersOfStore[mutationKey];
|
|
@@ -33,11 +30,10 @@ const mutate = (logTag, returnResult, store, cache, mutationKey, cacheOptions, p
|
|
|
33
30
|
abortController.abort();
|
|
34
31
|
}
|
|
35
32
|
else {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}));
|
|
33
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
34
|
+
loading: true,
|
|
35
|
+
result: undefined,
|
|
36
|
+
}));
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
const abortController = new AbortController();
|
|
@@ -64,23 +60,19 @@ const mutate = (logTag, returnResult, store, cache, mutationKey, cacheOptions, p
|
|
|
64
60
|
}
|
|
65
61
|
delete abortControllersOfStore[mutationKey];
|
|
66
62
|
if (error) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}));
|
|
72
|
-
}
|
|
63
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
64
|
+
error: error,
|
|
65
|
+
loading: false,
|
|
66
|
+
}));
|
|
73
67
|
return { error };
|
|
74
68
|
}
|
|
75
69
|
if (response) {
|
|
76
|
-
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey,
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
83
|
-
: undefined, cacheOptions.cacheEntities ? response : undefined));
|
|
70
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
71
|
+
error: undefined,
|
|
72
|
+
loading: false,
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
74
|
+
result: response.result,
|
|
75
|
+
}, response));
|
|
84
76
|
// @ts-expect-error fix later
|
|
85
77
|
return returnResult ? { result: response.result } : undefined;
|
|
86
78
|
}
|
package/dist/query.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Store } from 'redux';
|
|
2
|
-
import { Cache, Key,
|
|
3
|
-
export declare const query: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, queryKey: QK, cacheKey: Key,
|
|
2
|
+
import { Cache, Key, QueryResult, Typenames } from './types';
|
|
3
|
+
export declare const query: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(logTag: string, returnResult: boolean, store: Store, cache: Cache<T, QP, QR, MP, MR>, queryKey: QK, cacheKey: Key, params: QK extends keyof QP & keyof QR ? QP[QK] : never) => Promise<void | QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>;
|
package/dist/query.js
CHANGED
|
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
exports.query = void 0;
|
|
13
13
|
const reducer_1 = require("./reducer");
|
|
14
14
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
15
|
-
const query = (logTag, returnResult, store, cache, queryKey, cacheKey,
|
|
15
|
+
const query = (logTag, returnResult, store, cache, queryKey, cacheKey, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
var _a;
|
|
17
17
|
const logsEnabled = cache.options.logsEnabled;
|
|
18
18
|
const cacheStateSelector = cache.cacheStateSelector;
|
|
@@ -28,10 +28,9 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, cacheOpti
|
|
|
28
28
|
});
|
|
29
29
|
return returnResult ? { cancelled: true } : undefined;
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}));
|
|
31
|
+
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, {
|
|
32
|
+
loading: true,
|
|
33
|
+
}));
|
|
35
34
|
logsEnabled && (0, utilsAndConstants_1.log)(`${logTag} started`, { queryStateOnStart, params, cacheKey });
|
|
36
35
|
let response;
|
|
37
36
|
const fetchFn = cache.queries[queryKey].query;
|
|
@@ -47,20 +46,18 @@ const query = (logTag, returnResult, store, cache, queryKey, cacheKey, cacheOpti
|
|
|
47
46
|
}));
|
|
48
47
|
return returnResult ? { error } : undefined;
|
|
49
48
|
}
|
|
50
|
-
const newState =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
: undefined;
|
|
63
|
-
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, cacheOptions.cacheEntities ? response : undefined));
|
|
49
|
+
const newState = {
|
|
50
|
+
error: undefined,
|
|
51
|
+
loading: false,
|
|
52
|
+
result: cacheResultSelector
|
|
53
|
+
? undefined
|
|
54
|
+
: mergeResults
|
|
55
|
+
? mergeResults(
|
|
56
|
+
// @ts-expect-error fix later
|
|
57
|
+
(_a = cacheStateSelector(store.getState()).queries[queryKey][cacheKey]) === null || _a === void 0 ? void 0 : _a.result, response, params)
|
|
58
|
+
: response.result,
|
|
59
|
+
};
|
|
60
|
+
store.dispatch((0, reducer_1.setQueryStateAndEntities)(queryKey, cacheKey, newState, response));
|
|
64
61
|
// @ts-expect-error fix types
|
|
65
62
|
return returnResult
|
|
66
63
|
? {
|
package/dist/reducer.js
CHANGED
|
@@ -28,7 +28,7 @@ const createCacheReducer = (typenames, queries, mutations, cacheOptions) => {
|
|
|
28
28
|
switch (action.type) {
|
|
29
29
|
case '@RRC/SET_QUERY_STATE_AND_ENTITIES': {
|
|
30
30
|
const { queryKey, queryCacheKey, state: queryState, entityChagnes } = action;
|
|
31
|
-
const newEntities = entityChagnes && (0, utilsAndConstants_1.
|
|
31
|
+
const newEntities = entityChagnes && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChagnes, cacheOptions);
|
|
32
32
|
if (!queryState && !newEntities) {
|
|
33
33
|
return state;
|
|
34
34
|
}
|
|
@@ -36,7 +36,7 @@ const createCacheReducer = (typenames, queries, mutations, cacheOptions) => {
|
|
|
36
36
|
}
|
|
37
37
|
case '@RRC/SET_MUTATION_STATE_AND_ENTITIES': {
|
|
38
38
|
const { mutationKey, state: mutationState, entityChagnes } = action;
|
|
39
|
-
const newEntities = entityChagnes && (0, utilsAndConstants_1.
|
|
39
|
+
const newEntities = entityChagnes && (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChagnes, cacheOptions);
|
|
40
40
|
if (!mutationState && !newEntities) {
|
|
41
41
|
return state;
|
|
42
42
|
}
|
|
@@ -44,7 +44,7 @@ const createCacheReducer = (typenames, queries, mutations, cacheOptions) => {
|
|
|
44
44
|
}
|
|
45
45
|
case '@RRC/MERGE_ENTITY_CHANGES': {
|
|
46
46
|
const { changes } = action;
|
|
47
|
-
const newEntities = (0, utilsAndConstants_1.
|
|
47
|
+
const newEntities = (0, utilsAndConstants_1.applyEntityChanges)(state.entities, changes, cacheOptions);
|
|
48
48
|
return newEntities ? Object.assign(Object.assign({}, state), { entities: newEntities }) : state;
|
|
49
49
|
}
|
|
50
50
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -60,12 +60,10 @@ export type Query<T extends Typenames, P, R> = (params: P) => Promise<QueryRespo
|
|
|
60
60
|
export type QueryInfo<T extends Typenames, P, R, S> = {
|
|
61
61
|
query: Query<T, P, R>;
|
|
62
62
|
/**
|
|
63
|
-
* Cache policy
|
|
64
|
-
*
|
|
65
|
-
* @param cache-first for each params key fetch is not called if cache exists.
|
|
66
|
-
* @param cache-and-fetch for each params key result is taken from cache and fetch is called.
|
|
63
|
+
* Cache policy.
|
|
64
|
+
* @default cache-first
|
|
67
65
|
*/
|
|
68
|
-
|
|
66
|
+
cachePolicy?: QueryCachePolicy;
|
|
69
67
|
/**
|
|
70
68
|
* Selector for query result from redux state.
|
|
71
69
|
* Can prevent hook from doing unnecessary fetches.
|
|
@@ -91,23 +89,12 @@ export type UseQueryOptions<T extends Typenames, QP, QR, MP, MR, QK extends keyo
|
|
|
91
89
|
query: QK;
|
|
92
90
|
params: QK extends keyof (QP | QR) ? QP[QK] : never;
|
|
93
91
|
skip?: boolean;
|
|
94
|
-
} & Pick<QK extends keyof (QP | QR) ? QueryInfo<T, QP[QK], QR[QK], ReduxCacheState<T, QP, QR, MP, MR>> : never, '
|
|
92
|
+
} & Pick<QK extends keyof (QP | QR) ? QueryInfo<T, QP[QK], QR[QK], ReduxCacheState<T, QP, QR, MP, MR>> : never, 'cachePolicy' | 'mergeResults' | 'getCacheKey'>;
|
|
95
93
|
/**
|
|
96
94
|
* @param cache-first for each params key fetch is not called if cache exists.
|
|
97
95
|
* @param cache-and-fetch for each params key result is taken from cache and fetch is called.
|
|
98
96
|
*/
|
|
99
97
|
export type QueryCachePolicy = 'cache-first' | 'cache-and-fetch';
|
|
100
|
-
export type QueryCacheOptions = {
|
|
101
|
-
/**
|
|
102
|
-
* @param cache-first for each params key fetch is not called if cache exists.
|
|
103
|
-
* @param cache-and-fetch for each params key result is taken from cache and fetch is called.
|
|
104
|
-
*/
|
|
105
|
-
policy: QueryCachePolicy;
|
|
106
|
-
/** If `false`, query state is not saved in the store. Default is `true`. */
|
|
107
|
-
cacheQueryState: boolean;
|
|
108
|
-
/** If `false`, entities from response are not saved to the store. Default is `true`. */
|
|
109
|
-
cacheEntities: boolean;
|
|
110
|
-
};
|
|
111
98
|
export type QueryResponse<T extends Typenames, R> = EntityChanges<T> & {
|
|
112
99
|
/** Normalized result of a query. */
|
|
113
100
|
result: R;
|
|
@@ -123,11 +110,6 @@ export type Mutation<T extends Typenames, P, R> = (params: P,
|
|
|
123
110
|
abortSignal: AbortSignal) => Promise<MutationResponse<T, R>>;
|
|
124
111
|
export type MutationInfo<T extends Typenames, P, R> = {
|
|
125
112
|
mutation: Mutation<T, P, R>;
|
|
126
|
-
cacheOptions?: MutationCacheOptions;
|
|
127
|
-
};
|
|
128
|
-
export type MutationCacheOptions = Pick<QueryCacheOptions, 'cacheEntities'> & {
|
|
129
|
-
/** If `false`, mutation state is not saved in the store. Default is `true`. */
|
|
130
|
-
cacheMutationState: boolean;
|
|
131
113
|
};
|
|
132
114
|
export type MutationResponse<T extends Typenames, R> = EntityChanges<T> & {
|
|
133
115
|
/** Normalized result of a mutation. */
|
package/dist/useMutation.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { Store } from 'redux';
|
|
2
|
+
import { Cache, Key, QueryMutationState, Typenames } from './types';
|
|
3
3
|
export declare const useMutation: <T extends Typenames, MP, MR, MK extends keyof MP | keyof MR>(cache: Cache<T, unknown, unknown, MP, MR>, options: {
|
|
4
4
|
mutation: MK;
|
|
5
|
-
|
|
6
|
-
}) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
|
5
|
+
}, abortControllers: WeakMap<Store, Record<Key, AbortController>>) => readonly [(params: MK extends keyof MP & keyof MR ? MP[MK] : never) => Promise<void>, QueryMutationState<MK extends keyof MP & keyof MR ? MP[MK] : never>, () => boolean];
|
package/dist/useMutation.js
CHANGED
|
@@ -9,19 +9,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.useMutation =
|
|
12
|
+
exports.useMutation = void 0;
|
|
13
13
|
const react_1 = require("react");
|
|
14
14
|
const react_redux_1 = require("react-redux");
|
|
15
15
|
const mutate_1 = require("./mutate");
|
|
16
16
|
const reducer_1 = require("./reducer");
|
|
17
17
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
};
|
|
22
|
-
const useMutation = (cache, options) => {
|
|
23
|
-
var _a, _b;
|
|
24
|
-
const { mutation: mutationKey, cacheOptions = (_a = cache.mutations[mutationKey].cacheOptions) !== null && _a !== void 0 ? _a : exports.defaultMutationCacheOptions, } = options;
|
|
18
|
+
const useMutation = (cache, options, abortControllers) => {
|
|
19
|
+
var _a;
|
|
20
|
+
const { mutation: mutationKey } = options;
|
|
25
21
|
// Check values that should be set once.
|
|
26
22
|
// Can be removed from deps.
|
|
27
23
|
cache.options.validateHookArguments &&
|
|
@@ -33,8 +29,6 @@ const useMutation = (cache, options) => {
|
|
|
33
29
|
['cache.options.logsEnabled', cache.options.logsEnabled],
|
|
34
30
|
['cacheStateSelector', cache.cacheStateSelector],
|
|
35
31
|
['mutationKey', mutationKey],
|
|
36
|
-
['cacheOptions.cacheEntities', cacheOptions.cacheEntities],
|
|
37
|
-
['cacheOptions.cacheMutationState', cacheOptions.cacheMutationState],
|
|
38
32
|
]
|
|
39
33
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
40
34
|
.forEach((args) => (0, utilsAndConstants_1.useAssertValueNotChanged)(...args));
|
|
@@ -54,27 +48,27 @@ const useMutation = (cache, options) => {
|
|
|
54
48
|
},
|
|
55
49
|
// mutate
|
|
56
50
|
(params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
57
|
-
yield (0, mutate_1.mutate)('useMutation.mutate', false, store, cache, mutationKey,
|
|
51
|
+
yield (0, mutate_1.mutate)('useMutation.mutate', false, store, cache, mutationKey, params, abortControllers);
|
|
58
52
|
}),
|
|
59
53
|
// abort
|
|
60
54
|
() => {
|
|
61
|
-
|
|
55
|
+
var _a;
|
|
56
|
+
const abortController = (_a = abortControllers.get(store)) === null || _a === void 0 ? void 0 : _a[mutationKey];
|
|
62
57
|
if (abortController === undefined || abortController.signal.aborted) {
|
|
63
58
|
return false;
|
|
64
59
|
}
|
|
65
60
|
abortController.abort();
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}));
|
|
61
|
+
store.dispatch((0, reducer_1.setMutationStateAndEntities)(mutationKey, {
|
|
62
|
+
loading: false,
|
|
63
|
+
}));
|
|
70
64
|
return true;
|
|
71
65
|
},
|
|
72
66
|
];
|
|
73
67
|
},
|
|
74
68
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
75
|
-
[store
|
|
69
|
+
[store]);
|
|
76
70
|
// @ts-expect-error fix later
|
|
77
|
-
const mutationState = (
|
|
71
|
+
const mutationState = (_a = (0, react_redux_1.useSelector)(mutationStateSelector)) !== null && _a !== void 0 ? _a : utilsAndConstants_1.defaultQueryMutationState;
|
|
78
72
|
cache.options.logsEnabled &&
|
|
79
73
|
(0, utilsAndConstants_1.log)('useMutation', {
|
|
80
74
|
options,
|
package/dist/useQuery.d.ts
CHANGED
|
@@ -1,8 +1,2 @@
|
|
|
1
|
-
import { Cache,
|
|
2
|
-
export declare const queryCacheOptionsByPolicy: Record<QueryCachePolicy, QueryCacheOptions>;
|
|
3
|
-
export declare const defaultQueryCacheOptions: {
|
|
4
|
-
readonly policy: "cache-first";
|
|
5
|
-
readonly cacheQueryState: true;
|
|
6
|
-
readonly cacheEntities: true;
|
|
7
|
-
};
|
|
1
|
+
import { Cache, QueryMutationState, Typenames, UseQueryOptions } from './types';
|
|
8
2
|
export declare const useQuery: <T extends Typenames, QP, QR, MP, MR, QK extends keyof QP | keyof QR>(cache: Cache<T, QP, QR, MP, MR>, options: UseQueryOptions<T, QP, QR, MP, MR, QK>) => readonly [QueryMutationState<QK extends keyof QP & keyof QR ? QR[QK] : never>, () => Promise<void>];
|
package/dist/useQuery.js
CHANGED
|
@@ -9,31 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.useQuery =
|
|
12
|
+
exports.useQuery = void 0;
|
|
13
13
|
const react_1 = require("react");
|
|
14
14
|
const react_redux_1 = require("react-redux");
|
|
15
15
|
const query_1 = require("./query");
|
|
16
16
|
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
17
|
-
const cacheFirstOptions = {
|
|
18
|
-
policy: 'cache-first',
|
|
19
|
-
cacheQueryState: true,
|
|
20
|
-
cacheEntities: true,
|
|
21
|
-
};
|
|
22
|
-
exports.queryCacheOptionsByPolicy = {
|
|
23
|
-
'cache-first': cacheFirstOptions,
|
|
24
|
-
'cache-and-fetch': Object.assign(Object.assign({}, cacheFirstOptions), { policy: 'cache-and-fetch' }),
|
|
25
|
-
};
|
|
26
|
-
exports.defaultQueryCacheOptions = cacheFirstOptions;
|
|
27
17
|
const useQuery = (cache, options) => {
|
|
28
18
|
var _a, _b, _c;
|
|
29
|
-
const { query: queryKey, skip, params,
|
|
19
|
+
const { query: queryKey, skip, params, cachePolicy = (_a = cache.queries[queryKey].cachePolicy) !== null && _a !== void 0 ? _a : 'cache-first', getCacheKey = cache.queries[queryKey].getCacheKey, } = options;
|
|
30
20
|
const logsEnabled = cache.options.logsEnabled;
|
|
31
21
|
const getParamsKey = (_b = cache.queries[queryKey].getParamsKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetParamsKey);
|
|
32
22
|
const cacheResultSelector = cache.queries[queryKey].resultSelector;
|
|
33
23
|
const cacheStateSelector = cache.cacheStateSelector;
|
|
34
|
-
const cacheOptions = typeof cacheOptionsOrPolicy === 'string'
|
|
35
|
-
? exports.queryCacheOptionsByPolicy[cacheOptionsOrPolicy]
|
|
36
|
-
: cacheOptionsOrPolicy;
|
|
37
24
|
const store = (0, react_redux_1.useStore)();
|
|
38
25
|
// Check values that should be set once.
|
|
39
26
|
cache.options.validateHookArguments &&
|
|
@@ -44,8 +31,6 @@ const useQuery = (cache, options) => {
|
|
|
44
31
|
['cache', cache],
|
|
45
32
|
['cache.queries', cache.queries],
|
|
46
33
|
['cacheStateSelector', cache.cacheStateSelector],
|
|
47
|
-
['cacheOptions.cacheEntities', cacheOptions.cacheEntities],
|
|
48
|
-
['cacheOptions.cacheQueryState', cacheOptions.cacheQueryState],
|
|
49
34
|
['options.query', options.query],
|
|
50
35
|
['queryKey', queryKey],
|
|
51
36
|
['resultSelector', cache.queries[queryKey].resultSelector],
|
|
@@ -78,15 +63,9 @@ const useQuery = (cache, options) => {
|
|
|
78
63
|
const resultFromSelector = (resultSelector && (0, react_redux_1.useSelector)(resultSelector));
|
|
79
64
|
const hasResultFromSelector = resultFromSelector !== undefined;
|
|
80
65
|
const fetch = (0, react_1.useCallback)(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
81
|
-
yield (0, query_1.query)('useQuery.fetch', false, store, cache, queryKey, cacheKey,
|
|
66
|
+
yield (0, query_1.query)('useQuery.fetch', false, store, cache, queryKey, cacheKey, params);
|
|
82
67
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
83
|
-
}), [
|
|
84
|
-
cacheKey,
|
|
85
|
-
paramsKey,
|
|
86
|
-
cacheOptions.policy,
|
|
87
|
-
cacheOptions.cacheEntities,
|
|
88
|
-
cacheOptions.cacheQueryState,
|
|
89
|
-
]);
|
|
68
|
+
}), [cacheKey, paramsKey]);
|
|
90
69
|
const queryStateFromSelector = (_c = (0, react_redux_1.useSelector)((state) => {
|
|
91
70
|
const queryState = cacheStateSelector(state).queries[queryKey][cacheKey];
|
|
92
71
|
return queryState; // TODO proper type
|
|
@@ -99,17 +78,17 @@ const useQuery = (cache, options) => {
|
|
|
99
78
|
logsEnabled && (0, utilsAndConstants_1.log)('useQuery.useEffect skip fetch', { skip, paramsKey });
|
|
100
79
|
return;
|
|
101
80
|
}
|
|
102
|
-
if (queryState.result != null &&
|
|
81
|
+
if (queryState.result != null && cachePolicy === 'cache-first') {
|
|
103
82
|
logsEnabled &&
|
|
104
83
|
(0, utilsAndConstants_1.log)('useQuery.useEffect don`t fetch due to cache policy', {
|
|
105
84
|
result: queryState.result,
|
|
106
|
-
|
|
85
|
+
cachePolicy,
|
|
107
86
|
});
|
|
108
87
|
return;
|
|
109
88
|
}
|
|
110
89
|
fetch();
|
|
111
90
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
112
|
-
}, [paramsKey,
|
|
91
|
+
}, [paramsKey, cachePolicy, skip]);
|
|
113
92
|
logsEnabled &&
|
|
114
93
|
(0, utilsAndConstants_1.log)('useQuery', {
|
|
115
94
|
paramsKey,
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { CacheOptions, EntitiesMap, EntityChanges, Typenames } from './types';
|
|
2
2
|
export declare const PACKAGE_SHORT_NAME = "RRC";
|
|
3
3
|
export declare const isDev: boolean;
|
|
4
|
-
export declare const defaultCacheOptions: {
|
|
5
|
-
logsEnabled: boolean;
|
|
6
|
-
validateFunctionArguments: boolean;
|
|
7
|
-
validateHookArguments: boolean;
|
|
8
|
-
};
|
|
9
4
|
export declare const defaultQueryMutationState: {
|
|
10
5
|
readonly loading: false;
|
|
11
6
|
readonly error: undefined;
|
|
@@ -14,7 +9,7 @@ export declare const defaultGetParamsKey: <P = unknown>(params: P) => string;
|
|
|
14
9
|
export declare const useAssertValueNotChanged: (name: string, value: unknown) => void;
|
|
15
10
|
export declare const log: (tag: string, data?: unknown) => void;
|
|
16
11
|
/**
|
|
17
|
-
*
|
|
18
|
-
* @return `undefined` if nothing to change, otherwise
|
|
12
|
+
* Apply changes to the entities map.
|
|
13
|
+
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
19
14
|
*/
|
|
20
|
-
export declare const
|
|
15
|
+
export declare const applyEntityChanges: <T extends Typenames>(entities: EntitiesMap<T>, changes: EntityChanges<T>, options: CacheOptions) => EntitiesMap<T> | undefined;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.applyEntityChanges = exports.log = exports.useAssertValueNotChanged = exports.defaultGetParamsKey = exports.defaultQueryMutationState = exports.isDev = exports.PACKAGE_SHORT_NAME = void 0;
|
|
4
4
|
const react_1 = require("react");
|
|
5
5
|
exports.PACKAGE_SHORT_NAME = 'RRC';
|
|
6
6
|
exports.isDev = (() => {
|
|
@@ -12,11 +12,6 @@ exports.isDev = (() => {
|
|
|
12
12
|
return process.env.NODE_ENV === 'development';
|
|
13
13
|
}
|
|
14
14
|
})();
|
|
15
|
-
exports.defaultCacheOptions = {
|
|
16
|
-
logsEnabled: false,
|
|
17
|
-
validateFunctionArguments: true,
|
|
18
|
-
validateHookArguments: true,
|
|
19
|
-
};
|
|
20
15
|
exports.defaultQueryMutationState = { loading: false, error: undefined };
|
|
21
16
|
const defaultGetParamsKey = (params) => {
|
|
22
17
|
switch (typeof params) {
|
|
@@ -45,10 +40,10 @@ const log = (tag, data) => {
|
|
|
45
40
|
};
|
|
46
41
|
exports.log = log;
|
|
47
42
|
/**
|
|
48
|
-
*
|
|
49
|
-
* @return `undefined` if nothing to change, otherwise
|
|
43
|
+
* Apply changes to the entities map.
|
|
44
|
+
* @return `undefined` if nothing to change, otherwise new entities map with applied changes.
|
|
50
45
|
*/
|
|
51
|
-
const
|
|
46
|
+
const applyEntityChanges = (entities, changes, options) => {
|
|
52
47
|
var _a, _b, _c;
|
|
53
48
|
if (options.validateFunctionArguments) {
|
|
54
49
|
// check for merge and entities both set
|
|
@@ -99,11 +94,11 @@ const processEntityChanges = (entities, changes, options) => {
|
|
|
99
94
|
result[typename] = newEntities;
|
|
100
95
|
}
|
|
101
96
|
options.logsEnabled &&
|
|
102
|
-
(0, exports.log)('
|
|
97
|
+
(0, exports.log)('applyEntityChanges', {
|
|
103
98
|
entities,
|
|
104
99
|
changes,
|
|
105
100
|
result,
|
|
106
101
|
});
|
|
107
102
|
return result;
|
|
108
103
|
};
|
|
109
|
-
exports.
|
|
104
|
+
exports.applyEntityChanges = applyEntityChanges;
|
package/package.json
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
"name": "react-redux-cache",
|
|
3
3
|
"author": "Alexander Danilov",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.11",
|
|
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",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"example": "(cd example && yarn && yarn start)",
|
|
11
11
|
"clean": "rm -rf dist",
|
|
12
|
-
"
|
|
12
|
+
"lint": "yarn eslint src",
|
|
13
|
+
"build": "yarn clean && yarn lint && tsc && yarn pack -f package.tgz",
|
|
13
14
|
"test": "node node_modules/jest/bin/jest.js",
|
|
14
15
|
"prepublishOnly": "yarn build && yarn test"
|
|
15
16
|
},
|