react-redux-cache 0.21.0 → 0.22.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 +36 -2
- package/dist/cjs/createActions.js +63 -64
- package/dist/cjs/createCache.js +132 -204
- package/dist/cjs/createReducer.js +287 -271
- package/dist/cjs/createSelectors.js +58 -67
- package/dist/cjs/index.js +26 -89
- package/dist/cjs/mutate.js +69 -153
- package/dist/cjs/query.js +70 -162
- package/dist/cjs/types.js +2 -2
- package/dist/cjs/useMutation.js +42 -71
- package/dist/cjs/useQuery.js +56 -113
- package/dist/cjs/utilsAndConstants.js +151 -175
- package/dist/esm/createActions.js +59 -60
- package/dist/esm/createCache.js +128 -205
- package/dist/esm/createReducer.js +283 -258
- package/dist/esm/createSelectors.js +54 -63
- package/dist/esm/index.js +3 -5
- package/dist/esm/mutate.js +65 -142
- package/dist/esm/query.js +66 -149
- package/dist/esm/types.js +1 -1
- package/dist/esm/useMutation.js +38 -77
- package/dist/esm/useQuery.js +52 -119
- package/dist/esm/utilsAndConstants.js +140 -162
- package/dist/types/createActions.d.ts +83 -107
- package/dist/types/createCache.d.ts +409 -711
- package/dist/types/createReducer.d.ts +3 -11
- package/dist/types/createSelectors.d.ts +18 -80
- package/dist/types/index.d.ts +3 -5
- package/dist/types/mutate.d.ts +4 -94
- package/dist/types/query.d.ts +4 -123
- package/dist/types/types.d.ts +185 -346
- package/dist/types/useMutation.d.ts +4 -39
- package/dist/types/useQuery.d.ts +4 -40
- package/dist/types/utilsAndConstants.d.ts +23 -53
- package/package.json +16 -14
package/dist/esm/createCache.js
CHANGED
|
@@ -1,207 +1,130 @@
|
|
|
1
|
-
import {useMemo} from 'react'
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
applyEntityChanges,
|
|
12
|
-
createStateComparer,
|
|
13
|
-
defaultGetCacheKey,
|
|
14
|
-
EMPTY_OBJECT,
|
|
15
|
-
FetchPolicy,
|
|
16
|
-
IS_DEV,
|
|
17
|
-
logWarn,
|
|
18
|
-
optionalUtils,
|
|
19
|
-
} from './utilsAndConstants'
|
|
20
|
-
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { createActions } from './createActions';
|
|
3
|
+
import { createReducer } from './createReducer';
|
|
4
|
+
import { createSelectors } from './createSelectors';
|
|
5
|
+
import { mutate as mutateImpl } from './mutate';
|
|
6
|
+
import { query as queryImpl } from './query';
|
|
7
|
+
import { useMutation } from './useMutation';
|
|
8
|
+
import { useQuery } from './useQuery';
|
|
9
|
+
import { applyEntityChanges, createStateComparer, defaultGetCacheKey, EMPTY_OBJECT, FetchPolicy, IS_DEV, logWarn, optionalUtils, } from './utilsAndConstants';
|
|
21
10
|
export const withTypenames = () => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
store,
|
|
139
|
-
cache,
|
|
140
|
-
actions,
|
|
141
|
-
selectors,
|
|
142
|
-
options.mutation,
|
|
143
|
-
options.params,
|
|
144
|
-
abortControllers,
|
|
145
|
-
options.onCompleted,
|
|
146
|
-
options.onSuccess,
|
|
147
|
-
options.onError
|
|
148
|
-
)
|
|
149
|
-
},
|
|
150
|
-
}
|
|
151
|
-
return client
|
|
152
|
-
}
|
|
153
|
-
return {
|
|
154
|
-
cache,
|
|
155
|
-
reducer,
|
|
156
|
-
actions: {
|
|
157
|
-
updateQueryStateAndEntities,
|
|
158
|
-
updateMutationStateAndEntities,
|
|
159
|
-
mergeEntityChanges,
|
|
160
|
-
invalidateQuery,
|
|
161
|
-
clearQueryState,
|
|
162
|
-
clearMutationState,
|
|
163
|
-
clearCache,
|
|
11
|
+
return {
|
|
12
|
+
createCache: (partialCache) => {
|
|
13
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
|
|
14
|
+
var _p, _q, _r, _s, _t, _u, _v;
|
|
15
|
+
const abortControllers = new WeakMap();
|
|
16
|
+
(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {});
|
|
17
|
+
(_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0 ? _b : (_p.mutableCollections = false);
|
|
18
|
+
(_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0 ? _c : (_q.logsEnabled = false);
|
|
19
|
+
(_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0 ? _d : (_r.additionalValidation = IS_DEV);
|
|
20
|
+
(_e = (_s = partialCache.options).deepComparisonEnabled) !== null && _e !== void 0 ? _e : (_s.deepComparisonEnabled = true);
|
|
21
|
+
(_f = partialCache.globals) !== null && _f !== void 0 ? _f : (partialCache.globals = {});
|
|
22
|
+
(_g = (_t = partialCache.globals).queries) !== null && _g !== void 0 ? _g : (_t.queries = {});
|
|
23
|
+
(_h = (_u = partialCache.globals.queries).fetchPolicy) !== null && _h !== void 0 ? _h : (_u.fetchPolicy = FetchPolicy.NoCacheOrExpired);
|
|
24
|
+
(_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0 ? _j : (_v.skipFetch = false);
|
|
25
|
+
(_k = partialCache.cacheStateSelector) !== null && _k !== void 0 ? _k : (partialCache.cacheStateSelector = (state) => state[cache.name]);
|
|
26
|
+
(_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {});
|
|
27
|
+
(_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {});
|
|
28
|
+
partialCache.abortControllers = abortControllers;
|
|
29
|
+
try {
|
|
30
|
+
(_o = partialCache.storeHooks) !== null && _o !== void 0 ? _o : (partialCache.storeHooks = {
|
|
31
|
+
useStore: require('react-redux').useStore,
|
|
32
|
+
useSelector: require('react-redux').useSelector,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
throw e;
|
|
37
|
+
}
|
|
38
|
+
const cache = partialCache;
|
|
39
|
+
if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
|
|
40
|
+
logWarn('createCache', 'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true');
|
|
41
|
+
}
|
|
42
|
+
const setDefaultComparer = (target) => {
|
|
43
|
+
if ((target === null || target === void 0 ? void 0 : target.selectorComparer) != null && typeof target.selectorComparer === 'object') {
|
|
44
|
+
target.selectorComparer = createStateComparer(target.selectorComparer);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
setDefaultComparer(cache.globals.queries);
|
|
48
|
+
for (const queryKey in partialCache.queries) {
|
|
49
|
+
setDefaultComparer(partialCache.queries[queryKey]);
|
|
50
|
+
}
|
|
51
|
+
const selectors = Object.assign({ selectCacheState: cache.cacheStateSelector }, createSelectors(cache));
|
|
52
|
+
const { selectCacheState, selectQueryState, selectQueryResult, selectQueryLoading, selectQueryError, selectQueryParams, selectQueryExpiresAt, selectMutationState, selectMutationResult, selectMutationLoading, selectMutationError, selectMutationParams, selectEntityById, selectEntities, selectEntitiesByTypename, } = selectors;
|
|
53
|
+
const actions = createActions(cache.name);
|
|
54
|
+
const { updateQueryStateAndEntities, updateMutationStateAndEntities, mergeEntityChanges, invalidateQuery, clearQueryState, clearMutationState, clearCache, } = actions;
|
|
55
|
+
const reducer = createReducer(actions, Object.keys(cache.queries), cache.options);
|
|
56
|
+
const createClient = (store) => {
|
|
57
|
+
const client = {
|
|
58
|
+
query: (options) => {
|
|
59
|
+
var _a;
|
|
60
|
+
const { query: queryKey, params } = options;
|
|
61
|
+
const getCacheKey = (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : (defaultGetCacheKey);
|
|
62
|
+
const cacheKey = getCacheKey(params);
|
|
63
|
+
return queryImpl('query', store, cache, actions, selectors, queryKey, cacheKey, params, options.secondsToLive, options.onlyIfExpired, options.skipFetch, options.mergeResults, options.onCompleted, options.onSuccess, options.onError);
|
|
64
|
+
},
|
|
65
|
+
mutate: (options) => {
|
|
66
|
+
return mutateImpl('mutate', store, cache, actions, selectors, options.mutation, options.params, abortControllers, options.onCompleted, options.onSuccess, options.onError);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
return client;
|
|
70
|
+
};
|
|
71
|
+
return {
|
|
72
|
+
cache,
|
|
73
|
+
reducer,
|
|
74
|
+
actions: {
|
|
75
|
+
updateQueryStateAndEntities,
|
|
76
|
+
updateMutationStateAndEntities,
|
|
77
|
+
mergeEntityChanges,
|
|
78
|
+
invalidateQuery,
|
|
79
|
+
clearQueryState,
|
|
80
|
+
clearMutationState,
|
|
81
|
+
clearCache,
|
|
82
|
+
},
|
|
83
|
+
selectors: {
|
|
84
|
+
selectCacheState,
|
|
85
|
+
selectQueryState,
|
|
86
|
+
selectQueryResult,
|
|
87
|
+
selectQueryLoading,
|
|
88
|
+
selectQueryError,
|
|
89
|
+
selectQueryParams,
|
|
90
|
+
selectQueryExpiresAt,
|
|
91
|
+
selectMutationState,
|
|
92
|
+
selectMutationResult,
|
|
93
|
+
selectMutationLoading,
|
|
94
|
+
selectMutationError,
|
|
95
|
+
selectMutationParams,
|
|
96
|
+
selectEntityById,
|
|
97
|
+
selectEntities,
|
|
98
|
+
selectEntitiesByTypename,
|
|
99
|
+
},
|
|
100
|
+
hooks: {
|
|
101
|
+
useClient: () => {
|
|
102
|
+
const store = cache.storeHooks.useStore();
|
|
103
|
+
return useMemo(() => createClient(store), [store]);
|
|
104
|
+
},
|
|
105
|
+
useQuery: (options) => useQuery(cache, actions, selectors, options),
|
|
106
|
+
useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
|
|
107
|
+
useSelectEntityById: (id, typename) => {
|
|
108
|
+
return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename));
|
|
109
|
+
},
|
|
110
|
+
useEntitiesByTypename: (typename) => {
|
|
111
|
+
if (cache.options.mutableCollections) {
|
|
112
|
+
cache.storeHooks.useSelector((state) => { var _a; return (_a = selectEntitiesByTypename(state, typename)) === null || _a === void 0 ? void 0 : _a._changeKey; });
|
|
113
|
+
}
|
|
114
|
+
return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename));
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
utils: {
|
|
118
|
+
createClient,
|
|
119
|
+
getInitialState: () => {
|
|
120
|
+
return reducer(undefined, EMPTY_OBJECT);
|
|
121
|
+
},
|
|
122
|
+
applyEntityChanges: (entities, changes) => {
|
|
123
|
+
return applyEntityChanges(entities, changes, cache.options);
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
};
|
|
164
127
|
},
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
selectQueryResult,
|
|
169
|
-
selectQueryLoading,
|
|
170
|
-
selectQueryError,
|
|
171
|
-
selectQueryParams,
|
|
172
|
-
selectQueryExpiresAt,
|
|
173
|
-
selectMutationState,
|
|
174
|
-
selectMutationResult,
|
|
175
|
-
selectMutationLoading,
|
|
176
|
-
selectMutationError,
|
|
177
|
-
selectMutationParams,
|
|
178
|
-
selectEntityById,
|
|
179
|
-
selectEntities,
|
|
180
|
-
selectEntitiesByTypename,
|
|
181
|
-
},
|
|
182
|
-
hooks: {
|
|
183
|
-
useClient: () => {
|
|
184
|
-
const store = cache.storeHooks.useStore()
|
|
185
|
-
return useMemo(() => createClient(store), [store])
|
|
186
|
-
},
|
|
187
|
-
useQuery: (options) => useQuery(cache, actions, selectors, options),
|
|
188
|
-
useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
|
|
189
|
-
useSelectEntityById: (id, typename) => {
|
|
190
|
-
return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
|
|
191
|
-
},
|
|
192
|
-
},
|
|
193
|
-
utils: {
|
|
194
|
-
createClient,
|
|
195
|
-
getInitialState: () => {
|
|
196
|
-
return reducer(undefined, EMPTY_OBJECT)
|
|
197
|
-
},
|
|
198
|
-
applyEntityChanges: (entities, changes) => {
|
|
199
|
-
return applyEntityChanges(entities, changes, cache.options)
|
|
200
|
-
},
|
|
201
|
-
},
|
|
202
|
-
}
|
|
203
|
-
},
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export const createCache = withTypenames().createCache
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
export const createCache = withTypenames().createCache;
|