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