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