react-redux-cache 0.19.2 → 0.19.4-rc.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.
Files changed (57) hide show
  1. package/README.md +25 -7
  2. package/dist/cjs/createActions.js +65 -0
  3. package/dist/cjs/createCache.js +208 -0
  4. package/dist/cjs/createCacheReducer.js +285 -0
  5. package/dist/cjs/createSelectors.js +68 -0
  6. package/dist/cjs/index.js +83 -0
  7. package/dist/cjs/mutate.js +161 -0
  8. package/dist/cjs/query.js +180 -0
  9. package/dist/cjs/types.js +2 -0
  10. package/dist/cjs/useMutation.js +82 -0
  11. package/dist/cjs/useQuery.js +121 -0
  12. package/dist/cjs/utilsAndConstants.js +189 -0
  13. package/dist/esm/createActions.js +61 -0
  14. package/dist/esm/createCache.js +203 -0
  15. package/dist/esm/createCacheReducer.js +271 -0
  16. package/dist/esm/createSelectors.js +64 -0
  17. package/dist/esm/index.js +3 -0
  18. package/dist/esm/mutate.js +157 -0
  19. package/dist/esm/query.js +169 -0
  20. package/dist/esm/types.js +1 -0
  21. package/dist/esm/useMutation.js +88 -0
  22. package/dist/esm/useQuery.js +125 -0
  23. package/dist/esm/utilsAndConstants.js +168 -0
  24. package/dist/types/createActions.d.ts +106 -0
  25. package/dist/types/createCache.d.ts +712 -0
  26. package/dist/types/createCacheReducer.d.ts +11 -0
  27. package/dist/types/createSelectors.d.ts +79 -0
  28. package/dist/types/index.d.ts +3 -0
  29. package/dist/types/mutate.d.ts +94 -0
  30. package/dist/types/query.d.ts +122 -0
  31. package/dist/types/types.d.ts +322 -0
  32. package/dist/types/useMutation.d.ts +39 -0
  33. package/dist/types/useQuery.d.ts +40 -0
  34. package/dist/types/utilsAndConstants.d.ts +39 -0
  35. package/package.json +22 -10
  36. package/dist/createActions.d.ts +0 -83
  37. package/dist/createActions.js +0 -64
  38. package/dist/createCache.d.ts +0 -378
  39. package/dist/createCache.js +0 -185
  40. package/dist/createCacheReducer.d.ts +0 -3
  41. package/dist/createCacheReducer.js +0 -243
  42. package/dist/createSelectors.d.ts +0 -18
  43. package/dist/createSelectors.js +0 -61
  44. package/dist/index.d.ts +0 -3
  45. package/dist/index.js +0 -53
  46. package/dist/mutate.d.ts +0 -4
  47. package/dist/mutate.js +0 -98
  48. package/dist/query.d.ts +0 -4
  49. package/dist/query.js +0 -107
  50. package/dist/types.d.ts +0 -187
  51. package/dist/types.js +0 -3
  52. package/dist/useMutation.d.ts +0 -4
  53. package/dist/useMutation.js +0 -61
  54. package/dist/useQuery.d.ts +0 -4
  55. package/dist/useQuery.js +0 -77
  56. package/dist/utilsAndConstants.d.ts +0 -20
  57. package/dist/utilsAndConstants.js +0 -161
package/README.md CHANGED
@@ -11,13 +11,31 @@
11
11
 
12
12
  ### Supports both `Redux` and `Zustand` (check /example)
13
13
 
14
- **Powerful**, **performant** yet **lightweight** data fetching and caching library that supports **normalization** unlike `React Query` and `RTK-Query`, while having similar but not over-engineered, simple interface. Another advantage over `RTK-Query` is that it **doesn't use Immer** ([perf issue](https://github.com/reduxjs/redux-toolkit/issues/4793)). Covered with tests, fully typed and written on Typescript.
15
-
16
- **Normalization** is the best way to keep the state of the app **consistent** between different views, reduces the number of fetches and allows to show cached data when navigating, which greatly improves **user experience**.
17
-
18
- Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with **full control** over its storage - redux or zustand store, with ability to write custom selectors, actions and reducers to manage cached state.
19
-
20
- Examples of states, generated by cache reducer from `/example` project:
14
+ **Powerful**, **performant** yet **lightweight** data fetching and caching library that supports **normalization** unlike `React Query` and `RTK-Query`, while having similar but not over-engineered, simple interface. Covered with tests, fully typed and written on Typescript.
15
+
16
+ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with **full control** over its store - redux or zustand.
17
+
18
+ |Principle|Description|
19
+ |--|--|
20
+ |Full access to the store|You choose the store (redux / zustand) and have full access to state, reducer, actions, hooks, selectors and utils, used by this library, and can create your own.|
21
+ |Supports all kinds of queries / mutations|REST, GraphQL, databases - any async operations can be cached.|
22
+ |Fully typed|Written on TypeScript, everything is checked by compiler.|
23
+ |Not overengineered|Simplicity is the main goal.|
24
+ |Performance|Every function is heavily optimized, Immer is not used ([RTK [Query] issue](https://github.com/reduxjs/redux-toolkit/issues/4793)).|
25
+ |Reliability|High test coverage, zero issue policy.|
26
+ |Lightweight|`npx minified-size dist/esm/*.js`<br/>minified: 18.3 kB<br/>gzipped: 7.91 kB<br/>brotlied: 7.02 kB|
27
+
28
+ |Feature|Description|
29
+ |--|--|
30
+ |De-duplication of queries / mutations|Similar parallel queries are combined into one, mutations - aborted.|
31
+ |Time to live & Invalidation|Choose how long query result can be used before expired, or clear / invalidate it manually.|
32
+ |Deep comparison|Rendering is much heavier than deep comparison of incoming data, so it is enabled by default to prevent excess renders.|
33
+ |Infinite pagination|Easily implemented - one or two direction.|
34
+ |Error handling|No need to use try / catch, errors are returned by fuctions and / or can be handled globally from single place.|
35
+ |Fetch policies|Decide if data is full enough or need to be fetched.|
36
+ |Normalization|Consistent state accross the app - better UX, minimum loading states and lower traffic consumption.|
37
+
38
+ #### Examples of states, generated by cache reducer from `/example` project:
21
39
  <details>
22
40
  <summary>
23
41
  Normalized
@@ -0,0 +1,65 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createActions = void 0
4
+ const utilsAndConstants_1 = require('./utilsAndConstants')
5
+
6
+ const createActions = (name) => {
7
+ const actionPrefix = `@${utilsAndConstants_1.PACKAGE_SHORT_NAME}/${name}/`
8
+ const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`
9
+ const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
10
+ type: updateQueryStateAndEntitiesType,
11
+ queryKey,
12
+ queryCacheKey,
13
+ state,
14
+ entityChanges,
15
+ })
16
+ updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType
17
+ const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`
18
+ const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
19
+ type: updateMutationStateAndEntitiesType,
20
+ mutationKey,
21
+ state,
22
+ entityChanges,
23
+ })
24
+ updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType
25
+ const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`
26
+ const mergeEntityChanges = (changes) => ({
27
+ type: mergeEntityChangesType,
28
+ changes,
29
+ })
30
+ mergeEntityChanges.type = mergeEntityChangesType
31
+ const invalidateQueryType = `${actionPrefix}invalidateQuery`
32
+ const invalidateQuery = (queries) => ({
33
+ type: invalidateQueryType,
34
+ queries,
35
+ })
36
+ invalidateQuery.type = invalidateQueryType
37
+ const clearQueryStateType = `${actionPrefix}clearQueryState`
38
+ const clearQueryState = (queries) => ({
39
+ type: clearQueryStateType,
40
+ queries,
41
+ })
42
+ clearQueryState.type = clearQueryStateType
43
+ const clearMutationStateType = `${actionPrefix}clearMutationState`
44
+ const clearMutationState = (mutationKeys) => ({
45
+ type: clearMutationStateType,
46
+ mutationKeys,
47
+ })
48
+ clearMutationState.type = clearMutationStateType
49
+ const clearCacheType = `${actionPrefix}clearCache`
50
+ const clearCache = (stateToKeep) => ({
51
+ type: clearCacheType,
52
+ stateToKeep,
53
+ })
54
+ clearCache.type = clearCacheType
55
+ return {
56
+ updateQueryStateAndEntities,
57
+ updateMutationStateAndEntities,
58
+ mergeEntityChanges,
59
+ invalidateQuery,
60
+ clearQueryState,
61
+ clearMutationState,
62
+ clearCache,
63
+ }
64
+ }
65
+ exports.createActions = createActions
@@ -0,0 +1,208 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createCache = exports.withTypenames = void 0
4
+ const react_1 = require('react')
5
+ const createActions_1 = require('./createActions')
6
+ const createCacheReducer_1 = require('./createCacheReducer')
7
+ const createSelectors_1 = require('./createSelectors')
8
+ const mutate_1 = require('./mutate')
9
+ const query_1 = require('./query')
10
+ const useMutation_1 = require('./useMutation')
11
+ const useQuery_1 = require('./useQuery')
12
+ const utilsAndConstants_1 = require('./utilsAndConstants')
13
+
14
+ const withTypenames = () => {
15
+ return {
16
+ createCache: (partialCache) => {
17
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m
18
+ var _o, _p, _q, _r, _s, _t
19
+ const abortControllers = new WeakMap()
20
+ ;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
21
+ ;(_b = (_o = partialCache.options).logsEnabled) !== null && _b !== void 0
22
+ ? _b
23
+ : (_o.logsEnabled = false)
24
+ ;(_c = (_p = partialCache.options).additionalValidation) !== null && _c !== void 0
25
+ ? _c
26
+ : (_p.additionalValidation = utilsAndConstants_1.IS_DEV)
27
+ ;(_d = (_q = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0
28
+ ? _d
29
+ : (_q.deepComparisonEnabled = true)
30
+ ;(_e = partialCache.globals) !== null && _e !== void 0 ? _e : (partialCache.globals = {})
31
+ ;(_f = (_r = partialCache.globals).queries) !== null && _f !== void 0 ? _f : (_r.queries = {})
32
+ ;(_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0
33
+ ? _g
34
+ : (_s.fetchPolicy = utilsAndConstants_1.FetchPolicy.NoCacheOrExpired)
35
+ ;(_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0
36
+ ? _h
37
+ : (_t.skipFetch = false)
38
+ ;(_j = partialCache.cacheStateSelector) !== null && _j !== void 0
39
+ ? _j
40
+ : (partialCache.cacheStateSelector = (state) => state[cache.name])
41
+ ;(_k = partialCache.mutations) !== null && _k !== void 0 ? _k : (partialCache.mutations = {})
42
+ ;(_l = partialCache.queries) !== null && _l !== void 0 ? _l : (partialCache.queries = {})
43
+ partialCache.abortControllers = abortControllers
44
+ try {
45
+ ;(_m = partialCache.storeHooks) !== null && _m !== void 0
46
+ ? _m
47
+ : (partialCache.storeHooks = {
48
+ useStore: require('react-redux').useStore,
49
+ useSelector: require('react-redux').useSelector,
50
+ })
51
+ } catch (e) {
52
+ throw e
53
+ }
54
+ const cache = partialCache
55
+ if (cache.options.deepComparisonEnabled && !utilsAndConstants_1.optionalUtils.deepEqual) {
56
+ console.warn(
57
+ 'react-redux-cache: optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true'
58
+ )
59
+ }
60
+ const setDefaultComparer = (target) => {
61
+ if (
62
+ (target === null || target === void 0 ? void 0 : target.selectorComparer) != null &&
63
+ typeof target.selectorComparer === 'object'
64
+ ) {
65
+ target.selectorComparer = (0, utilsAndConstants_1.createStateComparer)(target.selectorComparer)
66
+ }
67
+ }
68
+ setDefaultComparer(cache.globals.queries)
69
+ for (const queryKey in partialCache.queries) {
70
+ setDefaultComparer(partialCache.queries[queryKey])
71
+ }
72
+ const selectors = Object.assign(
73
+ {selectCacheState: cache.cacheStateSelector},
74
+ (0, createSelectors_1.createSelectors)(cache)
75
+ )
76
+ const {
77
+ selectCacheState,
78
+ selectQueryState,
79
+ selectQueryResult,
80
+ selectQueryLoading,
81
+ selectQueryError,
82
+ selectQueryParams,
83
+ selectQueryExpiresAt,
84
+ selectMutationState,
85
+ selectMutationResult,
86
+ selectMutationLoading,
87
+ selectMutationError,
88
+ selectMutationParams,
89
+ selectEntityById,
90
+ selectEntities,
91
+ selectEntitiesByTypename,
92
+ } = selectors
93
+ const actions = (0, createActions_1.createActions)(cache.name)
94
+ const {
95
+ updateQueryStateAndEntities,
96
+ updateMutationStateAndEntities,
97
+ mergeEntityChanges,
98
+ invalidateQuery,
99
+ clearQueryState,
100
+ clearMutationState,
101
+ clearCache,
102
+ } = actions
103
+ const reducer = (0, createCacheReducer_1.createCacheReducer)(
104
+ actions,
105
+ Object.keys(cache.queries),
106
+ cache.options
107
+ )
108
+ const createClient = (store) => {
109
+ const client = {
110
+ query: (options) => {
111
+ var _a
112
+ const {query: queryKey, params} = options
113
+ const getCacheKey =
114
+ (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0
115
+ ? _a
116
+ : utilsAndConstants_1.defaultGetCacheKey
117
+ const cacheKey = getCacheKey(params)
118
+ return (0, query_1.query)(
119
+ 'query',
120
+ store,
121
+ cache,
122
+ actions,
123
+ selectors,
124
+ queryKey,
125
+ cacheKey,
126
+ params,
127
+ options.secondsToLive,
128
+ options.onlyIfExpired,
129
+ options.mergeResults,
130
+ options.onCompleted,
131
+ options.onSuccess,
132
+ options.onError
133
+ )
134
+ },
135
+ mutate: (options) => {
136
+ return (0, mutate_1.mutate)(
137
+ 'mutate',
138
+ store,
139
+ cache,
140
+ actions,
141
+ selectors,
142
+ options.mutation,
143
+ options.params,
144
+ abortControllers,
145
+ options.onCompleted,
146
+ options.onSuccess,
147
+ options.onError
148
+ )
149
+ },
150
+ }
151
+ return client
152
+ }
153
+ return {
154
+ cache,
155
+ reducer,
156
+ actions: {
157
+ updateQueryStateAndEntities,
158
+ updateMutationStateAndEntities,
159
+ mergeEntityChanges,
160
+ invalidateQuery,
161
+ clearQueryState,
162
+ clearMutationState,
163
+ clearCache,
164
+ },
165
+ selectors: {
166
+ selectCacheState,
167
+ selectQueryState,
168
+ selectQueryResult,
169
+ selectQueryLoading,
170
+ selectQueryError,
171
+ selectQueryParams,
172
+ selectQueryExpiresAt,
173
+ selectMutationState,
174
+ selectMutationResult,
175
+ selectMutationLoading,
176
+ selectMutationError,
177
+ selectMutationParams,
178
+ selectEntityById,
179
+ selectEntities,
180
+ selectEntitiesByTypename,
181
+ },
182
+ hooks: {
183
+ useClient: () => {
184
+ const store = cache.storeHooks.useStore()
185
+ return (0, react_1.useMemo)(() => createClient(store), [store])
186
+ },
187
+ useQuery: (options) => (0, useQuery_1.useQuery)(cache, actions, selectors, options),
188
+ useMutation: (options) =>
189
+ (0, useMutation_1.useMutation)(cache, actions, selectors, options, abortControllers),
190
+ useSelectEntityById: (id, typename) => {
191
+ return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
192
+ },
193
+ },
194
+ utils: {
195
+ createClient,
196
+ getInitialState: () => {
197
+ return reducer(undefined, utilsAndConstants_1.EMPTY_OBJECT)
198
+ },
199
+ applyEntityChanges: (entities, changes) => {
200
+ return (0, utilsAndConstants_1.applyEntityChanges)(entities, changes, cache.options)
201
+ },
202
+ },
203
+ }
204
+ },
205
+ }
206
+ }
207
+ exports.withTypenames = withTypenames
208
+ exports.createCache = (0, exports.withTypenames)().createCache
@@ -0,0 +1,285 @@
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]]
15
+ }
16
+ }
17
+ }
18
+ return t
19
+ }
20
+ Object.defineProperty(exports, '__esModule', {value: true})
21
+ exports.createCacheReducer = void 0
22
+ const utilsAndConstants_1 = require('./utilsAndConstants')
23
+
24
+ const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
25
+ const optionalMutationKeys = ['error', 'result', 'params', 'loading']
26
+ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
27
+ const initialState = Object.freeze({
28
+ entities: Object.freeze({}),
29
+ queries: Object.freeze(
30
+ queryKeys.reduce((result, x) => {
31
+ result[x] = Object.freeze({})
32
+ return result
33
+ }, {})
34
+ ),
35
+ mutations: Object.freeze({}),
36
+ })
37
+ cacheOptions.logsEnabled &&
38
+ (0, utilsAndConstants_1.log)('createCacheReducer', {
39
+ queryKeys,
40
+ initialState,
41
+ })
42
+ const deepEqual = cacheOptions.deepComparisonEnabled
43
+ ? utilsAndConstants_1.optionalUtils.deepEqual
44
+ : undefined
45
+ return (state = initialState, action) => {
46
+ switch (action.type) {
47
+ case actions.updateQueryStateAndEntities.type: {
48
+ const {queryKey, queryCacheKey, state: queryState, entityChanges} = action
49
+ const oldQueryState = state.queries[queryKey][queryCacheKey]
50
+ let newQueryState = queryState && Object.assign(Object.assign({}, oldQueryState), queryState)
51
+ if (newQueryState) {
52
+ if (oldQueryState && deepEqual) {
53
+ if (
54
+ newQueryState.params !== oldQueryState.params &&
55
+ deepEqual(newQueryState.params, oldQueryState.params)
56
+ ) {
57
+ newQueryState.params = oldQueryState.params
58
+ }
59
+ if (
60
+ newQueryState.result !== oldQueryState.result &&
61
+ deepEqual(newQueryState.result, oldQueryState.result)
62
+ ) {
63
+ newQueryState.result = oldQueryState.result
64
+ }
65
+ }
66
+ for (const key of optionalQueryKeys) {
67
+ if (key in newQueryState && newQueryState[key] === undefined) {
68
+ delete newQueryState[key]
69
+ }
70
+ }
71
+ if (
72
+ deepEqual === null || deepEqual === void 0
73
+ ? void 0
74
+ : deepEqual(
75
+ oldQueryState !== null && oldQueryState !== void 0
76
+ ? oldQueryState
77
+ : utilsAndConstants_1.EMPTY_OBJECT,
78
+ newQueryState
79
+ )
80
+ ) {
81
+ newQueryState = undefined
82
+ }
83
+ }
84
+ const newEntities =
85
+ entityChanges &&
86
+ (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
+ newState.queries = Object.assign(Object.assign({}, state.queries), {
96
+ [queryKey]: Object.assign(Object.assign({}, state.queries[queryKey]), {
97
+ [queryCacheKey]: newQueryState,
98
+ }),
99
+ })
100
+ } else if (oldQueryState !== undefined) {
101
+ const _a = state.queries[queryKey],
102
+ _b = queryCacheKey,
103
+ _ = _a[_b],
104
+ withoutCacheKey = __rest(_a, [typeof _b === 'symbol' ? _b : _b + ''])
105
+ newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
106
+ newState.queries = Object.assign(Object.assign({}, state.queries), {[queryKey]: withoutCacheKey})
107
+ }
108
+ }
109
+ return newState !== null && newState !== void 0 ? newState : state
110
+ }
111
+ case actions.updateMutationStateAndEntities.type: {
112
+ const {mutationKey, state: mutationState, entityChanges} = action
113
+ const oldMutationState = state.mutations[mutationKey]
114
+ let newMutationState =
115
+ mutationState && Object.assign(Object.assign({}, oldMutationState), mutationState)
116
+ if (newMutationState) {
117
+ if (oldMutationState && deepEqual) {
118
+ if (
119
+ newMutationState.params !== oldMutationState.params &&
120
+ deepEqual(newMutationState.params, oldMutationState.params)
121
+ ) {
122
+ newMutationState.params = oldMutationState.params
123
+ }
124
+ if (
125
+ newMutationState.result !== oldMutationState.result &&
126
+ deepEqual(newMutationState.result, oldMutationState.result)
127
+ ) {
128
+ newMutationState.result = oldMutationState.result
129
+ }
130
+ }
131
+ for (const key of optionalMutationKeys) {
132
+ if (key in newMutationState && newMutationState[key] === undefined) {
133
+ delete newMutationState[key]
134
+ }
135
+ }
136
+ if (
137
+ deepEqual === null || deepEqual === void 0
138
+ ? void 0
139
+ : deepEqual(
140
+ oldMutationState !== null && oldMutationState !== void 0
141
+ ? oldMutationState
142
+ : utilsAndConstants_1.EMPTY_OBJECT,
143
+ newMutationState
144
+ )
145
+ ) {
146
+ newMutationState = undefined
147
+ }
148
+ }
149
+ const newEntities =
150
+ entityChanges &&
151
+ (0, utilsAndConstants_1.applyEntityChanges)(state.entities, entityChanges, cacheOptions)
152
+ let newState
153
+ if (newEntities) {
154
+ newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
155
+ newState.entities = newEntities
156
+ }
157
+ if (newMutationState) {
158
+ if (!(0, utilsAndConstants_1.isEmptyObject)(newMutationState)) {
159
+ newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
160
+ newState.mutations = Object.assign(Object.assign({}, state.mutations), {
161
+ [mutationKey]: newMutationState,
162
+ })
163
+ } else if (oldMutationState !== undefined) {
164
+ const _c = state.mutations,
165
+ _d = mutationKey,
166
+ _ = _c[_d],
167
+ withoutMutationKey = __rest(_c, [typeof _d === 'symbol' ? _d : _d + ''])
168
+ newState !== null && newState !== void 0 ? newState : (newState = Object.assign({}, state))
169
+ newState.mutations = withoutMutationKey
170
+ }
171
+ }
172
+ return newState !== null && newState !== void 0 ? newState : state
173
+ }
174
+ case actions.mergeEntityChanges.type: {
175
+ const {changes} = action
176
+ const newEntities = (0, utilsAndConstants_1.applyEntityChanges)(state.entities, changes, cacheOptions)
177
+ return newEntities ? Object.assign(Object.assign({}, state), {entities: newEntities}) : state
178
+ }
179
+ case actions.invalidateQuery.type: {
180
+ const {queries: queriesToInvalidate} = action
181
+ if (queriesToInvalidate.length === 0) {
182
+ return state
183
+ }
184
+ const now = Date.now()
185
+ let newQueries = undefined
186
+ for (const {query: queryKey, cacheKey, expiresAt = now} of queriesToInvalidate) {
187
+ const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
188
+ queryKey
189
+ ]
190
+ if (cacheKey != null) {
191
+ if (queryStates[cacheKey]) {
192
+ const queryState = queryStates[cacheKey]
193
+ if (queryState && queryState.expiresAt !== expiresAt) {
194
+ newQueries !== null && newQueries !== void 0
195
+ ? newQueries
196
+ : (newQueries = Object.assign({}, state.queries))
197
+ if (state.queries[queryKey] === newQueries[queryKey]) {
198
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
199
+ }
200
+ newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
201
+ if (expiresAt === undefined) {
202
+ delete newQueries[queryKey][cacheKey].expiresAt
203
+ }
204
+ }
205
+ }
206
+ } else {
207
+ for (const cacheKey in queryStates) {
208
+ const queryState = queryStates[cacheKey]
209
+ if (queryState && queryState.expiresAt !== expiresAt) {
210
+ newQueries !== null && newQueries !== void 0
211
+ ? newQueries
212
+ : (newQueries = Object.assign({}, state.queries))
213
+ if (state.queries[queryKey] === newQueries[queryKey]) {
214
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
215
+ }
216
+ newQueries[queryKey][cacheKey] = Object.assign(Object.assign({}, queryState), {expiresAt})
217
+ if (expiresAt === undefined) {
218
+ delete newQueries[queryKey][cacheKey].expiresAt
219
+ }
220
+ }
221
+ }
222
+ }
223
+ }
224
+ return newQueries === undefined
225
+ ? state
226
+ : Object.assign(Object.assign({}, state), {queries: newQueries})
227
+ }
228
+ case actions.clearQueryState.type: {
229
+ const {queries: queriesToClear} = action
230
+ if (queriesToClear.length === 0) {
231
+ return state
232
+ }
233
+ let newQueries = undefined
234
+ for (const {query: queryKey, cacheKey} of queriesToClear) {
235
+ const queryStates = (newQueries !== null && newQueries !== void 0 ? newQueries : state.queries)[
236
+ queryKey
237
+ ]
238
+ if (cacheKey != null) {
239
+ if (queryStates[cacheKey]) {
240
+ newQueries !== null && newQueries !== void 0
241
+ ? newQueries
242
+ : (newQueries = Object.assign({}, state.queries))
243
+ if (state.queries[queryKey] === newQueries[queryKey]) {
244
+ newQueries[queryKey] = Object.assign({}, newQueries[queryKey])
245
+ }
246
+ delete newQueries[queryKey][cacheKey]
247
+ }
248
+ } else if (queryStates !== utilsAndConstants_1.EMPTY_OBJECT) {
249
+ newQueries !== null && newQueries !== void 0
250
+ ? newQueries
251
+ : (newQueries = Object.assign({}, state.queries))
252
+ newQueries[queryKey] = utilsAndConstants_1.EMPTY_OBJECT
253
+ }
254
+ }
255
+ return newQueries === undefined
256
+ ? state
257
+ : Object.assign(Object.assign({}, state), {queries: newQueries})
258
+ }
259
+ case actions.clearMutationState.type: {
260
+ const {mutationKeys} = action
261
+ if (mutationKeys.length === 0) {
262
+ return state
263
+ }
264
+ let newMutations = undefined
265
+ for (const mutation of mutationKeys) {
266
+ if (state.mutations[mutation]) {
267
+ newMutations !== null && newMutations !== void 0
268
+ ? newMutations
269
+ : (newMutations = Object.assign({}, state.mutations))
270
+ delete newMutations[mutation]
271
+ }
272
+ }
273
+ return newMutations === undefined
274
+ ? state
275
+ : Object.assign(Object.assign({}, state), {mutations: newMutations})
276
+ }
277
+ case actions.clearCache.type: {
278
+ const {stateToKeep} = action
279
+ return stateToKeep ? Object.assign(Object.assign({}, initialState), stateToKeep) : initialState
280
+ }
281
+ }
282
+ return state
283
+ }
284
+ }
285
+ exports.createCacheReducer = createCacheReducer
@@ -0,0 +1,68 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createSelectors = void 0
4
+ const utilsAndConstants_1 = require('./utilsAndConstants')
5
+
6
+ const createSelectors = (cache) => {
7
+ const selectEntityById = (state, id, typename) => {
8
+ var _a
9
+ return id == null
10
+ ? undefined
11
+ : (_a = cache.cacheStateSelector(state).entities[typename]) === null || _a === void 0
12
+ ? void 0
13
+ : _a[id]
14
+ }
15
+ const selectQueryState = (state, query, cacheKey) => {
16
+ var _a
17
+ return (_a = cache.cacheStateSelector(state).queries[query][cacheKey]) !== null && _a !== void 0
18
+ ? _a
19
+ : utilsAndConstants_1.EMPTY_OBJECT
20
+ }
21
+ const selectMutationState = (state, mutation) => {
22
+ var _a
23
+ return (_a = cache.cacheStateSelector(state).mutations[mutation]) !== null && _a !== void 0
24
+ ? _a
25
+ : utilsAndConstants_1.EMPTY_OBJECT
26
+ }
27
+ return {
28
+ selectQueryState,
29
+ selectQueryResult: (state, query, cacheKey) => {
30
+ return selectQueryState(state, query, cacheKey).result
31
+ },
32
+ selectQueryLoading: (state, query, cacheKey) => {
33
+ var _a
34
+ return (_a = selectQueryState(state, query, cacheKey).loading) !== null && _a !== void 0 ? _a : false
35
+ },
36
+ selectQueryError: (state, query, cacheKey) => {
37
+ return selectQueryState(state, query, cacheKey).error
38
+ },
39
+ selectQueryParams: (state, query, cacheKey) => {
40
+ return selectQueryState(state, query, cacheKey).params
41
+ },
42
+ selectQueryExpiresAt: (state, query, cacheKey) => {
43
+ return selectQueryState(state, query, cacheKey).expiresAt
44
+ },
45
+ selectMutationState,
46
+ selectMutationResult: (state, mutation) => {
47
+ return selectMutationState(state, mutation).result
48
+ },
49
+ selectMutationLoading: (state, mutation) => {
50
+ var _a
51
+ return (_a = selectMutationState(state, mutation).loading) !== null && _a !== void 0 ? _a : false
52
+ },
53
+ selectMutationError: (state, mutation) => {
54
+ return selectMutationState(state, mutation).error
55
+ },
56
+ selectMutationParams: (state, mutation) => {
57
+ return selectMutationState(state, mutation).params
58
+ },
59
+ selectEntityById,
60
+ selectEntities: (state) => {
61
+ return cache.cacheStateSelector(state).entities
62
+ },
63
+ selectEntitiesByTypename: (state, typename) => {
64
+ return cache.cacheStateSelector(state).entities[typename]
65
+ },
66
+ }
67
+ }
68
+ exports.createSelectors = createSelectors