react-redux-cache 0.20.0 → 0.22.0
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 +38 -3
- package/dist/cjs/createCache.js +34 -20
- package/dist/cjs/createReducer.js +183 -110
- package/dist/cjs/createSelectors.js +1 -1
- package/dist/cjs/utilsAndConstants.js +41 -12
- package/dist/esm/createCache.js +34 -20
- package/dist/esm/createReducer.js +194 -114
- package/dist/esm/createSelectors.js +1 -1
- package/dist/esm/utilsAndConstants.js +39 -11
- package/dist/types/createCache.d.ts +45 -20
- package/dist/types/createSelectors.d.ts +7 -7
- package/dist/types/types.d.ts +28 -9
- package/dist/types/utilsAndConstants.d.ts +9 -3
- package/package.json +6 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, '__esModule', {value: true})
|
|
3
|
-
exports.
|
|
3
|
+
exports.incrementChangeKey =
|
|
4
|
+
exports.FetchPolicy =
|
|
4
5
|
exports.createStateComparer =
|
|
5
6
|
exports.isEmptyObject =
|
|
6
7
|
exports.applyEntityChanges =
|
|
@@ -55,7 +56,7 @@ const defaultGetCacheKey = (params) => {
|
|
|
55
56
|
}
|
|
56
57
|
exports.defaultGetCacheKey = defaultGetCacheKey
|
|
57
58
|
const applyEntityChanges = (entities, changes, options) => {
|
|
58
|
-
var _a, _b, _c
|
|
59
|
+
var _a, _b, _c
|
|
59
60
|
if (changes.merge && changes.entities) {
|
|
60
61
|
;(0, exports.logWarn)('applyEntityChanges', 'merge and entities should not be both set')
|
|
61
62
|
}
|
|
@@ -63,6 +64,7 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
63
64
|
if (!merge && !replace && !remove) {
|
|
64
65
|
return undefined
|
|
65
66
|
}
|
|
67
|
+
const mutable = options.mutableCollections
|
|
66
68
|
const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined
|
|
67
69
|
let result
|
|
68
70
|
const objectWithAllTypenames = Object.assign(
|
|
@@ -108,37 +110,48 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
108
110
|
)
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
|
-
const oldEntities =
|
|
113
|
+
const oldEntities = entities[typename]
|
|
112
114
|
let newEntities
|
|
113
115
|
entitiesToRemove === null || entitiesToRemove === void 0
|
|
114
116
|
? void 0
|
|
115
117
|
: entitiesToRemove.forEach((id) => {
|
|
116
|
-
if (oldEntities[id]) {
|
|
118
|
+
if (oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id]) {
|
|
117
119
|
newEntities !== null && newEntities !== void 0
|
|
118
120
|
? newEntities
|
|
119
|
-
: (newEntities = Object.assign({}, oldEntities))
|
|
121
|
+
: (newEntities = mutable ? oldEntities : Object.assign({}, oldEntities))
|
|
120
122
|
delete newEntities[id]
|
|
121
123
|
}
|
|
122
124
|
})
|
|
123
125
|
if (entitiesToReplace) {
|
|
124
126
|
for (const id in entitiesToReplace) {
|
|
125
127
|
const newEntity = entitiesToReplace[id]
|
|
126
|
-
if (
|
|
128
|
+
if (
|
|
129
|
+
oldEntities === undefined ||
|
|
130
|
+
!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntities[id], newEntity))
|
|
131
|
+
) {
|
|
127
132
|
newEntities !== null && newEntities !== void 0
|
|
128
133
|
? newEntities
|
|
129
|
-
: (newEntities =
|
|
134
|
+
: (newEntities = mutable
|
|
135
|
+
? oldEntities !== null && oldEntities !== void 0
|
|
136
|
+
? oldEntities
|
|
137
|
+
: {}
|
|
138
|
+
: Object.assign({}, oldEntities))
|
|
130
139
|
newEntities[id] = newEntity
|
|
131
140
|
}
|
|
132
141
|
}
|
|
133
142
|
}
|
|
134
143
|
if (entitiesToMerge) {
|
|
135
144
|
for (const id in entitiesToMerge) {
|
|
136
|
-
const oldEntity = oldEntities[id]
|
|
145
|
+
const oldEntity = oldEntities === null || oldEntities === void 0 ? void 0 : oldEntities[id]
|
|
137
146
|
const newEntity = Object.assign(Object.assign({}, oldEntity), entitiesToMerge[id])
|
|
138
147
|
if (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntity, newEntity))) {
|
|
139
148
|
newEntities !== null && newEntities !== void 0
|
|
140
149
|
? newEntities
|
|
141
|
-
: (newEntities =
|
|
150
|
+
: (newEntities = mutable
|
|
151
|
+
? oldEntities !== null && oldEntities !== void 0
|
|
152
|
+
? oldEntities
|
|
153
|
+
: {}
|
|
154
|
+
: Object.assign({}, oldEntities))
|
|
142
155
|
newEntities[id] = newEntity
|
|
143
156
|
}
|
|
144
157
|
}
|
|
@@ -146,7 +159,15 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
146
159
|
if (!newEntities) {
|
|
147
160
|
continue
|
|
148
161
|
}
|
|
149
|
-
|
|
162
|
+
if (mutable) {
|
|
163
|
+
;(0, exports.incrementChangeKey)(newEntities)
|
|
164
|
+
if (result === undefined) {
|
|
165
|
+
;(0, exports.incrementChangeKey)(entities)
|
|
166
|
+
result = entities
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
result !== null && result !== void 0 ? result : (result = Object.assign({}, entities))
|
|
170
|
+
}
|
|
150
171
|
result[typename] = newEntities
|
|
151
172
|
}
|
|
152
173
|
options.logsEnabled &&
|
|
@@ -159,8 +180,8 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
159
180
|
return result
|
|
160
181
|
}
|
|
161
182
|
exports.applyEntityChanges = applyEntityChanges
|
|
162
|
-
const isEmptyObject = (
|
|
163
|
-
for (const _ in
|
|
183
|
+
const isEmptyObject = (obj) => {
|
|
184
|
+
for (const _ in obj) {
|
|
164
185
|
return false
|
|
165
186
|
}
|
|
166
187
|
return true
|
|
@@ -190,3 +211,11 @@ exports.FetchPolicy = {
|
|
|
190
211
|
},
|
|
191
212
|
Always: () => true,
|
|
192
213
|
}
|
|
214
|
+
const incrementChangeKey = (mutable) => {
|
|
215
|
+
if (mutable._changeKey === undefined) {
|
|
216
|
+
mutable._changeKey = 0
|
|
217
|
+
} else {
|
|
218
|
+
mutable._changeKey += 1
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
exports.incrementChangeKey = incrementChangeKey
|
package/dist/esm/createCache.js
CHANGED
|
@@ -21,36 +21,39 @@ import {
|
|
|
21
21
|
export const withTypenames = () => {
|
|
22
22
|
return {
|
|
23
23
|
createCache: (partialCache) => {
|
|
24
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m
|
|
25
|
-
var
|
|
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
26
|
const abortControllers = new WeakMap()
|
|
27
27
|
;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
|
|
28
|
-
;(_b = (
|
|
28
|
+
;(_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0
|
|
29
29
|
? _b
|
|
30
|
-
: (
|
|
31
|
-
;(_c = (
|
|
30
|
+
: (_p.mutableCollections = false)
|
|
31
|
+
;(_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0
|
|
32
32
|
? _c
|
|
33
|
-
: (
|
|
34
|
-
;(_d = (
|
|
33
|
+
: (_q.logsEnabled = false)
|
|
34
|
+
;(_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0
|
|
35
35
|
? _d
|
|
36
|
-
: (
|
|
37
|
-
;(_e = partialCache.
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
;(_h = (
|
|
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
43
|
? _h
|
|
44
|
-
: (
|
|
45
|
-
;(_j = partialCache.
|
|
44
|
+
: (_u.fetchPolicy = FetchPolicy.NoCacheOrExpired)
|
|
45
|
+
;(_j = (_v = partialCache.globals.queries).skipFetch) !== null && _j !== void 0
|
|
46
46
|
? _j
|
|
47
|
+
: (_v.skipFetch = false)
|
|
48
|
+
;(_k = partialCache.cacheStateSelector) !== null && _k !== void 0
|
|
49
|
+
? _k
|
|
47
50
|
: (partialCache.cacheStateSelector = (state) => state[cache.name])
|
|
48
|
-
;(
|
|
49
|
-
;(
|
|
51
|
+
;(_l = partialCache.mutations) !== null && _l !== void 0 ? _l : (partialCache.mutations = {})
|
|
52
|
+
;(_m = partialCache.queries) !== null && _m !== void 0 ? _m : (partialCache.queries = {})
|
|
50
53
|
partialCache.abortControllers = abortControllers
|
|
51
54
|
try {
|
|
52
|
-
;(
|
|
53
|
-
?
|
|
55
|
+
;(_o = partialCache.storeHooks) !== null && _o !== void 0
|
|
56
|
+
? _o
|
|
54
57
|
: (partialCache.storeHooks = {
|
|
55
58
|
useStore: require('react-redux').useStore,
|
|
56
59
|
useSelector: require('react-redux').useSelector,
|
|
@@ -189,6 +192,17 @@ export const withTypenames = () => {
|
|
|
189
192
|
useSelectEntityById: (id, typename) => {
|
|
190
193
|
return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
|
|
191
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
|
+
})
|
|
203
|
+
}
|
|
204
|
+
return cache.storeHooks.useSelector((state) => selectEntitiesByTypename(state, typename))
|
|
205
|
+
},
|
|
192
206
|
},
|
|
193
207
|
utils: {
|
|
194
208
|
createClient,
|
|
@@ -2,45 +2,69 @@ var __rest =
|
|
|
2
2
|
(this && this.__rest) ||
|
|
3
3
|
function (s, e) {
|
|
4
4
|
var t = {}
|
|
5
|
-
for (var p in s) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) {
|
|
13
|
-
t[p[i]] = s[p[i]]
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
5
|
+
for (var p in s) {if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
6
|
+
t[p] = s[p];}
|
|
7
|
+
if (s != null && typeof Object.getOwnPropertySymbols === 'function')
|
|
8
|
+
{for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
9
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
10
|
+
t[p[i]] = s[p[i]];
|
|
11
|
+
}}
|
|
17
12
|
return t
|
|
18
13
|
}
|
|
19
|
-
import {
|
|
20
|
-
|
|
14
|
+
import {
|
|
15
|
+
applyEntityChanges,
|
|
16
|
+
EMPTY_OBJECT,
|
|
17
|
+
incrementChangeKey,
|
|
18
|
+
isEmptyObject,
|
|
19
|
+
logDebug,
|
|
20
|
+
optionalUtils,
|
|
21
|
+
} from './utilsAndConstants'
|
|
21
22
|
const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
|
|
22
23
|
const optionalMutationKeys = ['error', 'result', 'params', 'loading']
|
|
23
|
-
|
|
24
24
|
export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
25
|
-
const
|
|
26
|
-
entities: Object.freeze({}),
|
|
27
|
-
queries: Object.freeze(
|
|
28
|
-
queryKeys.reduce((result, x) => {
|
|
29
|
-
result[x] = Object.freeze({})
|
|
30
|
-
return result
|
|
31
|
-
}, {})
|
|
32
|
-
),
|
|
33
|
-
mutations: Object.freeze({}),
|
|
34
|
-
})
|
|
25
|
+
const mutable = cacheOptions.mutableCollections
|
|
35
26
|
cacheOptions.logsEnabled &&
|
|
36
27
|
logDebug('createCacheReducer', {
|
|
37
28
|
queryKeys,
|
|
38
|
-
|
|
29
|
+
mutable,
|
|
39
30
|
})
|
|
31
|
+
const getMutableInitialState = mutable
|
|
32
|
+
? () => {
|
|
33
|
+
return {
|
|
34
|
+
entities: {},
|
|
35
|
+
queries: queryKeys.reduce((result, x) => {
|
|
36
|
+
result[x] = {}
|
|
37
|
+
return result
|
|
38
|
+
}, {}),
|
|
39
|
+
mutations: {},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
: undefined
|
|
43
|
+
const immutableInitialState = mutable
|
|
44
|
+
? undefined
|
|
45
|
+
: Object.freeze({
|
|
46
|
+
entities: Object.freeze({}),
|
|
47
|
+
queries: Object.freeze(
|
|
48
|
+
queryKeys.reduce((result, x) => {
|
|
49
|
+
result[x] = Object.freeze({})
|
|
50
|
+
return result
|
|
51
|
+
}, {})
|
|
52
|
+
),
|
|
53
|
+
mutations: Object.freeze({}),
|
|
54
|
+
})
|
|
55
|
+
const {
|
|
56
|
+
clearCache,
|
|
57
|
+
clearMutationState,
|
|
58
|
+
clearQueryState,
|
|
59
|
+
invalidateQuery,
|
|
60
|
+
mergeEntityChanges,
|
|
61
|
+
updateMutationStateAndEntities,
|
|
62
|
+
updateQueryStateAndEntities,
|
|
63
|
+
} = actions
|
|
40
64
|
const deepEqual = cacheOptions.deepComparisonEnabled ? optionalUtils.deepEqual : undefined
|
|
41
|
-
return (state =
|
|
65
|
+
return (state = mutable ? getMutableInitialState() : immutableInitialState, action) => {
|
|
42
66
|
switch (action.type) {
|
|
43
|
-
case
|
|
67
|
+
case updateQueryStateAndEntities.type: {
|
|
44
68
|
const {queryKey, queryCacheKey, state: queryState, entityChanges} = action
|
|
45
69
|
const oldQueryState = state.queries[queryKey][queryCacheKey]
|
|
46
70
|
let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState)
|
|
@@ -84,23 +108,36 @@ export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
84
108
|
if (newQueryState) {
|
|
85
109
|
if (!isEmptyObject(newQueryState)) {
|
|
86
110
|
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
111
|
+
if (mutable) {
|
|
112
|
+
newState.queries[queryKey][queryCacheKey] = newQueryState
|
|
113
|
+
incrementChangeKey(newState.queries)
|
|
114
|
+
incrementChangeKey(newState.queries[queryKey])
|
|
115
|
+
} else {
|
|
116
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), {
|
|
117
|
+
[queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), {
|
|
118
|
+
[queryCacheKey]: newQueryState,
|
|
119
|
+
}),
|
|
120
|
+
})
|
|
121
|
+
} else if (oldQueryState !== undefined) {
|
|
97
122
|
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
98
|
-
|
|
123
|
+
if (mutable) {
|
|
124
|
+
delete newState.queries[queryKey][queryCacheKey]
|
|
125
|
+
incrementChangeKey(newState.queries)
|
|
126
|
+
incrementChangeKey(newState.queries[queryKey])
|
|
127
|
+
} else {
|
|
128
|
+
const _a = state.queries[queryKey],
|
|
129
|
+
_b = queryCacheKey,
|
|
130
|
+
_ = _a[_b],
|
|
131
|
+
withoutCacheKey = __rest(_a, [typeof _b === 'symbol' ? _b : _b + ''])
|
|
132
|
+
newState.queries = Object.assign(Object.assign({}, state.queries), {
|
|
133
|
+
[queryKey]: withoutCacheKey,
|
|
134
|
+
})
|
|
135
|
+
}
|
|
99
136
|
}
|
|
100
137
|
}
|
|
101
138
|
return newState !== null && newState !== void 0 ? newState : state
|
|
102
139
|
}
|
|
103
|
-
case
|
|
140
|
+
case updateMutationStateAndEntities.type: {
|
|
104
141
|
const {mutationKey, state: mutationState, entityChanges} = action
|
|
105
142
|
const oldMutationState = state.mutations[mutationKey]
|
|
106
143
|
let newMutationState =
|
|
@@ -139,112 +176,150 @@ export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
139
176
|
const newEntities = entityChanges && applyEntityChanges(state.entities, entityChanges, cacheOptions)
|
|
140
177
|
let newState
|
|
141
178
|
if (newEntities) {
|
|
142
|
-
newState
|
|
143
|
-
newState.entities = newEntities
|
|
179
|
+
newState = Object.assign(Object.assign({}, state), {entities: newEntities})
|
|
144
180
|
}
|
|
145
181
|
if (newMutationState) {
|
|
146
182
|
if (!isEmptyObject(newMutationState)) {
|
|
147
183
|
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
148
|
-
|
|
149
|
-
[mutationKey]
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
184
|
+
if (mutable) {
|
|
185
|
+
state.mutations[mutationKey] = newMutationState
|
|
186
|
+
incrementChangeKey(state.mutations)
|
|
187
|
+
} else {
|
|
188
|
+
newState.mutations = Object.assign(Object.assign({}, state.mutations), {
|
|
189
|
+
[mutationKey]: newMutationState,
|
|
190
|
+
})
|
|
191
|
+
} else if (oldMutationState !== undefined) {
|
|
156
192
|
newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
|
|
157
|
-
|
|
193
|
+
if (mutable) {
|
|
194
|
+
delete state.mutations[mutationKey]
|
|
195
|
+
incrementChangeKey(state.mutations)
|
|
196
|
+
} else {
|
|
197
|
+
const _c = state.mutations,
|
|
198
|
+
_d = mutationKey,
|
|
199
|
+
_ = _c[_d],
|
|
200
|
+
withoutMutationKey = __rest(_c, [typeof _d === 'symbol' ? _d : _d + ''])
|
|
201
|
+
newState.mutations = withoutMutationKey
|
|
202
|
+
}
|
|
158
203
|
}
|
|
159
204
|
}
|
|
160
205
|
return newState !== null && newState !== void 0 ? newState : state
|
|
161
206
|
}
|
|
162
|
-
case
|
|
207
|
+
case mergeEntityChanges.type: {
|
|
163
208
|
const {changes} = action
|
|
164
209
|
const newEntities = applyEntityChanges(state.entities, changes, cacheOptions)
|
|
165
210
|
return newEntities ? Object.assign(Object.assign({}, state), {entities: newEntities}) : state
|
|
166
211
|
}
|
|
167
|
-
case
|
|
212
|
+
case invalidateQuery.type: {
|
|
168
213
|
const {queries: queriesToInvalidate} = action
|
|
169
214
|
if (queriesToInvalidate.length === 0) {
|
|
170
215
|
return state
|
|
171
216
|
}
|
|
172
217
|
const now = Date.now()
|
|
173
|
-
let
|
|
218
|
+
let newStatesByQueryKey
|
|
219
|
+
const copiedQueryKeys = mutable ? undefined : new Set()
|
|
174
220
|
for (const {query: queryKey, cacheKey, expiresAt = now} of queriesToInvalidate) {
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
221
|
+
const statesByCacheKey = (
|
|
222
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
223
|
+
? newStatesByQueryKey
|
|
224
|
+
: state.queries
|
|
225
|
+
)[queryKey]
|
|
226
|
+
const cacheKeysToInvalidate = cacheKey != null ? [cacheKey] : Object.keys(statesByCacheKey)
|
|
227
|
+
for (const cacheKey of cacheKeysToInvalidate) {
|
|
228
|
+
const queryState = statesByCacheKey[cacheKey]
|
|
229
|
+
if (!queryState || queryState.expiresAt === expiresAt) {
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
if (mutable) {
|
|
233
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
234
|
+
? newStatesByQueryKey
|
|
235
|
+
: (newStatesByQueryKey = state.queries)
|
|
236
|
+
incrementChangeKey(newStatesByQueryKey[queryKey])
|
|
237
|
+
} else {
|
|
238
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
239
|
+
? newStatesByQueryKey
|
|
240
|
+
: (newStatesByQueryKey = Object.assign({}, state.queries))
|
|
241
|
+
if (!copiedQueryKeys.has(queryKey)) {
|
|
242
|
+
newStatesByQueryKey[queryKey] = Object.assign({}, newStatesByQueryKey[queryKey])
|
|
243
|
+
copiedQueryKeys.add(queryKey)
|
|
192
244
|
}
|
|
193
245
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
if (expiresAt === undefined) {
|
|
206
|
-
delete newQueries[queryKey][cacheKey].expiresAt
|
|
207
|
-
}
|
|
246
|
+
if (expiresAt !== undefined) {
|
|
247
|
+
newStatesByQueryKey[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {
|
|
248
|
+
expiresAt,
|
|
249
|
+
})
|
|
250
|
+
} else {
|
|
251
|
+
const {expiresAt: _} = queryState,
|
|
252
|
+
newQueryState = __rest(queryState, ['expiresAt'])
|
|
253
|
+
if (isEmptyObject(newQueryState)) {
|
|
254
|
+
delete newStatesByQueryKey[queryKey][cacheKey]
|
|
255
|
+
} else {
|
|
256
|
+
newStatesByQueryKey[queryKey][cacheKey] = newQueryState
|
|
208
257
|
}
|
|
209
258
|
}
|
|
210
259
|
}
|
|
211
260
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
261
|
+
if (!newStatesByQueryKey) {
|
|
262
|
+
return state
|
|
263
|
+
}
|
|
264
|
+
if (mutable) {
|
|
265
|
+
incrementChangeKey(newStatesByQueryKey)
|
|
266
|
+
}
|
|
267
|
+
return Object.assign(Object.assign({}, state), {queries: newStatesByQueryKey})
|
|
215
268
|
}
|
|
216
|
-
case
|
|
269
|
+
case clearQueryState.type: {
|
|
217
270
|
const {queries: queriesToClear} = action
|
|
218
271
|
if (queriesToClear.length === 0) {
|
|
219
272
|
return state
|
|
220
273
|
}
|
|
221
|
-
let
|
|
274
|
+
let newStatesByQueryKey
|
|
275
|
+
const copiedQueryKeys = mutable ? undefined : new Set()
|
|
222
276
|
for (const {query: queryKey, cacheKey} of queriesToClear) {
|
|
223
|
-
const
|
|
224
|
-
|
|
225
|
-
|
|
277
|
+
const statesByCacheKey = (
|
|
278
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
279
|
+
? newStatesByQueryKey
|
|
280
|
+
: state.queries
|
|
281
|
+
)[queryKey]
|
|
226
282
|
if (cacheKey != null) {
|
|
227
|
-
if (
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
283
|
+
if (!statesByCacheKey[cacheKey]) {
|
|
284
|
+
continue
|
|
285
|
+
}
|
|
286
|
+
if (mutable) {
|
|
287
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
288
|
+
? newStatesByQueryKey
|
|
289
|
+
: (newStatesByQueryKey = state.queries)
|
|
290
|
+
incrementChangeKey(newStatesByQueryKey[queryKey])
|
|
291
|
+
} else {
|
|
292
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
293
|
+
? newStatesByQueryKey
|
|
294
|
+
: (newStatesByQueryKey = Object.assign({}, state.queries))
|
|
295
|
+
if (!copiedQueryKeys.has(queryKey)) {
|
|
296
|
+
newStatesByQueryKey[queryKey] = Object.assign({}, newStatesByQueryKey[queryKey])
|
|
297
|
+
copiedQueryKeys.add(queryKey)
|
|
233
298
|
}
|
|
234
|
-
delete newQueries[queryKey][cacheKey]
|
|
235
299
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
300
|
+
delete newStatesByQueryKey[queryKey][cacheKey]
|
|
301
|
+
} else if (mutable) {
|
|
302
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
303
|
+
? newStatesByQueryKey
|
|
304
|
+
: (newStatesByQueryKey = state.queries)
|
|
305
|
+
newStatesByQueryKey[queryKey] = {}
|
|
306
|
+
} else if (statesByCacheKey !== EMPTY_OBJECT) {
|
|
307
|
+
newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
|
|
308
|
+
? newStatesByQueryKey
|
|
309
|
+
: (newStatesByQueryKey = Object.assign({}, state.queries))
|
|
310
|
+
newStatesByQueryKey[queryKey] = EMPTY_OBJECT
|
|
311
|
+
copiedQueryKeys.add(queryKey)
|
|
241
312
|
}
|
|
242
313
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
314
|
+
if (newStatesByQueryKey === undefined) {
|
|
315
|
+
return state
|
|
316
|
+
}
|
|
317
|
+
if (mutable) {
|
|
318
|
+
incrementChangeKey(newStatesByQueryKey)
|
|
319
|
+
}
|
|
320
|
+
return Object.assign(Object.assign({}, state), {queries: newStatesByQueryKey})
|
|
246
321
|
}
|
|
247
|
-
case
|
|
322
|
+
case clearMutationState.type: {
|
|
248
323
|
const {mutationKeys} = action
|
|
249
324
|
if (mutationKeys.length === 0) {
|
|
250
325
|
return state
|
|
@@ -254,16 +329,21 @@ export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
254
329
|
if (state.mutations[mutation]) {
|
|
255
330
|
newMutations !== null && newMutations !== void 0
|
|
256
331
|
? newMutations
|
|
257
|
-
: (newMutations = Object.assign({}, state.mutations))
|
|
332
|
+
: (newMutations = mutable ? state.mutations : Object.assign({}, state.mutations))
|
|
258
333
|
delete newMutations[mutation]
|
|
259
334
|
}
|
|
260
335
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
336
|
+
if (newMutations === undefined) {
|
|
337
|
+
return state
|
|
338
|
+
}
|
|
339
|
+
if (mutable) {
|
|
340
|
+
incrementChangeKey(newMutations)
|
|
341
|
+
}
|
|
342
|
+
return Object.assign(Object.assign({}, state), {mutations: newMutations})
|
|
264
343
|
}
|
|
265
|
-
case
|
|
344
|
+
case clearCache.type: {
|
|
266
345
|
const {stateToKeep} = action
|
|
346
|
+
const initialState = mutable ? getMutableInitialState() : immutableInitialState
|
|
267
347
|
return stateToKeep ? Object.assign(Object.assign({}, initialState), stateToKeep) : initialState
|
|
268
348
|
}
|
|
269
349
|
}
|
|
@@ -22,6 +22,7 @@ export const createSelectors = (cache) => {
|
|
|
22
22
|
: EMPTY_OBJECT
|
|
23
23
|
}
|
|
24
24
|
return {
|
|
25
|
+
selectEntityById,
|
|
25
26
|
selectQueryState,
|
|
26
27
|
selectQueryResult: (state, query, cacheKey) => {
|
|
27
28
|
return selectQueryState(state, query, cacheKey).result
|
|
@@ -53,7 +54,6 @@ export const createSelectors = (cache) => {
|
|
|
53
54
|
selectMutationParams: (state, mutation) => {
|
|
54
55
|
return selectMutationState(state, mutation).params
|
|
55
56
|
},
|
|
56
|
-
selectEntityById,
|
|
57
57
|
selectEntities: (state) => {
|
|
58
58
|
return cache.cacheStateSelector(state).entities
|
|
59
59
|
},
|