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