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.
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
  Object.defineProperty(exports, '__esModule', {value: true})
3
- exports.FetchPolicy =
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, _d
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 = (_d = entities[typename]) !== null && _d !== void 0 ? _d : exports.EMPTY_OBJECT
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 (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntities[id], newEntity))) {
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 = Object.assign({}, oldEntities))
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 = Object.assign({}, oldEntities))
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
- result !== null && result !== void 0 ? result : (result = Object.assign({}, entities))
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 = (o) => {
163
- for (const _ in o) {
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
@@ -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 _o, _p, _q, _r, _s, _t
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 = (_o = partialCache.options).logsEnabled) !== null && _b !== void 0
28
+ ;(_b = (_p = partialCache.options).mutableCollections) !== null && _b !== void 0
29
29
  ? _b
30
- : (_o.logsEnabled = false)
31
- ;(_c = (_p = partialCache.options).additionalValidation) !== null && _c !== void 0
30
+ : (_p.mutableCollections = false)
31
+ ;(_c = (_q = partialCache.options).logsEnabled) !== null && _c !== void 0
32
32
  ? _c
33
- : (_p.additionalValidation = IS_DEV)
34
- ;(_d = (_q = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0
33
+ : (_q.logsEnabled = false)
34
+ ;(_d = (_r = partialCache.options).additionalValidation) !== null && _d !== void 0
35
35
  ? _d
36
- : (_q.deepComparisonEnabled = true)
37
- ;(_e = partialCache.globals) !== null && _e !== void 0 ? _e : (partialCache.globals = {})
38
- ;(_f = (_r = partialCache.globals).queries) !== null && _f !== void 0 ? _f : (_r.queries = {})
39
- ;(_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0
40
- ? _g
41
- : (_s.fetchPolicy = FetchPolicy.NoCacheOrExpired)
42
- ;(_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0
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
- : (_t.skipFetch = false)
45
- ;(_j = partialCache.cacheStateSelector) !== null && _j !== void 0
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
- ;(_k = partialCache.mutations) !== null && _k !== void 0 ? _k : (partialCache.mutations = {})
49
- ;(_l = partialCache.queries) !== null && _l !== void 0 ? _l : (partialCache.queries = {})
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
- ;(_m = partialCache.storeHooks) !== null && _m !== void 0
53
- ? _m
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
- if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) {
7
- t[p] = s[p]
8
- }
9
- }
10
- if (s != null && typeof Object.getOwnPropertySymbols === 'function') {
11
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
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 {applyEntityChanges, EMPTY_OBJECT, isEmptyObject, logDebug, optionalUtils} from './utilsAndConstants'
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 initialState = Object.freeze({
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
- initialState,
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 = initialState, action) => {
65
+ return (state = mutable ? getMutableInitialState() : immutableInitialState, action) => {
42
66
  switch (action.type) {
43
- case actions.updateQueryStateAndEntities.type: {
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
- newState.queries = Object.assign(Object.assign({}, state.queries), {
88
- [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), {
89
- [queryCacheKey]: newQueryState,
90
- }),
91
- })
92
- } else if (oldQueryState !== undefined) {
93
- const _a = state.queries[queryKey],
94
- _b = queryCacheKey,
95
- _ = _a[_b],
96
- withoutCacheKey = __rest(_a, [typeof _b === 'symbol' ? _b : _b + ''])
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
- newState.queries = Object.assign(Object.assign({}, state.queries), {[queryKey]: withoutCacheKey})
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 actions.updateMutationStateAndEntities.type: {
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 !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
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
- newState.mutations = Object.assign(Object.assign({}, state.mutations), {
149
- [mutationKey]: newMutationState,
150
- })
151
- } else if (oldMutationState !== undefined) {
152
- const _c = state.mutations,
153
- _d = mutationKey,
154
- _ = _c[_d],
155
- withoutMutationKey = __rest(_c, [typeof _d === 'symbol' ? _d : _d + ''])
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
- newState.mutations = withoutMutationKey
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 actions.mergeEntityChanges.type: {
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 actions.invalidateQuery.type: {
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 newQueries = undefined
218
+ let newStatesByQueryKey
219
+ const copiedQueryKeys = mutable ? undefined : new Set()
174
220
  for (const {query: queryKey, cacheKey, expiresAt = now} of queriesToInvalidate) {
175
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
176
- queryKey
177
- ]
178
- if (cacheKey != null) {
179
- if (queryStates[cacheKey]) {
180
- const queryState = queryStates[cacheKey]
181
- if (queryState && queryState.expiresAt !== expiresAt) {
182
- newQueries !== null && newQueries !== void 0
183
- ? newQueries
184
- : (newQueries = Object.assign({}, state.queries))
185
- if (state.queries[queryKey] === newQueries[queryKey]) {
186
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
187
- }
188
- newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
189
- if (expiresAt === undefined) {
190
- delete newQueries[queryKey][cacheKey].expiresAt
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
- } else {
195
- for (const cacheKey in queryStates) {
196
- const queryState = queryStates[cacheKey]
197
- if (queryState && queryState.expiresAt !== expiresAt) {
198
- newQueries !== null && newQueries !== void 0
199
- ? newQueries
200
- : (newQueries = Object.assign({}, state.queries))
201
- if (state.queries[queryKey] === newQueries[queryKey]) {
202
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
203
- }
204
- newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
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
- return newQueries === undefined
213
- ? state
214
- : Object.assign(Object.assign({}, state), {queries: newQueries})
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 actions.clearQueryState.type: {
269
+ case clearQueryState.type: {
217
270
  const {queries: queriesToClear} = action
218
271
  if (queriesToClear.length === 0) {
219
272
  return state
220
273
  }
221
- let newQueries = undefined
274
+ let newStatesByQueryKey
275
+ const copiedQueryKeys = mutable ? undefined : new Set()
222
276
  for (const {query: queryKey, cacheKey} of queriesToClear) {
223
- const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
224
- queryKey
225
- ]
277
+ const statesByCacheKey = (
278
+ newStatesByQueryKey !== null && newStatesByQueryKey !== void 0
279
+ ? newStatesByQueryKey
280
+ : state.queries
281
+ )[queryKey]
226
282
  if (cacheKey != null) {
227
- if (queryStates[cacheKey]) {
228
- newQueries !== null && newQueries !== void 0
229
- ? newQueries
230
- : (newQueries = Object.assign({}, state.queries))
231
- if (state.queries[queryKey] === newQueries[queryKey]) {
232
- newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
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
- } else if (queryStates !== EMPTY_OBJECT) {
237
- newQueries !== null && newQueries !== void 0
238
- ? newQueries
239
- : (newQueries = Object.assign({}, state.queries))
240
- newQueries[queryKey] = EMPTY_OBJECT
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
- return newQueries === undefined
244
- ? state
245
- : Object.assign(Object.assign({}, state), {queries: newQueries})
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 actions.clearMutationState.type: {
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
- return newMutations === undefined
262
- ? state
263
- : Object.assign(Object.assign({}, state), {mutations: newMutations})
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 actions.clearCache.type: {
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
  },