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
@@ -0,0 +1,189 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', {value: true})
3
+ exports.createStateComparer =
4
+ exports.isEmptyObject =
5
+ exports.applyEntityChanges =
6
+ exports.FetchPolicy =
7
+ exports.log =
8
+ exports.defaultGetCacheKey =
9
+ exports.NOOP =
10
+ exports.EMPTY_ARRAY =
11
+ exports.EMPTY_OBJECT =
12
+ exports.IS_DEV =
13
+ exports.optionalUtils =
14
+ exports.PACKAGE_SHORT_NAME =
15
+ void 0
16
+ exports.PACKAGE_SHORT_NAME = 'rrc'
17
+ exports.optionalUtils = {
18
+ deepEqual: undefined,
19
+ }
20
+ try {
21
+ exports.optionalUtils.deepEqual = require('fast-deep-equal/es6')
22
+ } catch (_a) {
23
+ console.debug(exports.PACKAGE_SHORT_NAME + ': fast-deep-equal optional dependency was not installed')
24
+ }
25
+ exports.IS_DEV = (() => {
26
+ try {
27
+ return __DEV__
28
+ } catch (e) {
29
+ return process.env.NODE_ENV === 'development'
30
+ }
31
+ })()
32
+ exports.EMPTY_OBJECT = Object.freeze({})
33
+ exports.EMPTY_ARRAY = Object.freeze([])
34
+ const NOOP = () => {}
35
+ exports.NOOP = NOOP
36
+ const defaultGetCacheKey = (params) => {
37
+ switch (typeof params) {
38
+ case 'string':
39
+ case 'symbol':
40
+ return params
41
+ case 'object':
42
+ return JSON.stringify(params)
43
+ default:
44
+ return String(params)
45
+ }
46
+ }
47
+ exports.defaultGetCacheKey = defaultGetCacheKey
48
+ const log = (tag, data) => {
49
+ console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
50
+ }
51
+ exports.log = log
52
+ exports.FetchPolicy = {
53
+ NoCacheOrExpired: (expired, _params, state) => {
54
+ return expired || state.result === undefined
55
+ },
56
+ Always: () => true,
57
+ }
58
+ const applyEntityChanges = (entities, changes, options) => {
59
+ var _a, _b, _c, _d
60
+ if (changes.merge && changes.entities) {
61
+ console.warn('react-redux-cache.applyEntityChanges: merge and entities should not be both set')
62
+ }
63
+ const {merge = changes.entities, replace, remove} = changes
64
+ if (!merge && !replace && !remove) {
65
+ return undefined
66
+ }
67
+ const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined
68
+ let result
69
+ const typenames = new Set([
70
+ ...(changes.entities ? Object.keys(changes.entities) : exports.EMPTY_ARRAY),
71
+ ...(changes.merge ? Object.keys(changes.merge) : exports.EMPTY_ARRAY),
72
+ ...(changes.remove ? Object.keys(changes.remove) : exports.EMPTY_ARRAY),
73
+ ...(changes.replace ? Object.keys(changes.replace) : exports.EMPTY_ARRAY),
74
+ ])
75
+ for (const typename of typenames) {
76
+ const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
77
+ const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
78
+ const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
79
+ if (
80
+ !entitiesToMerge &&
81
+ !entitiesToReplace &&
82
+ !(entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length)
83
+ ) {
84
+ continue
85
+ }
86
+ if (options.additionalValidation) {
87
+ const mergeIds = entitiesToMerge && Object.keys(entitiesToMerge)
88
+ const replaceIds = entitiesToReplace && Object.keys(entitiesToReplace)
89
+ const idsSet = new Set(mergeIds)
90
+ replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.forEach((id) => idsSet.add(id))
91
+ entitiesToRemove === null || entitiesToRemove === void 0
92
+ ? void 0
93
+ : entitiesToRemove.forEach((id) => idsSet.add(String(id)))
94
+ const totalKeysInResponse =
95
+ ((_a = mergeIds === null || mergeIds === void 0 ? void 0 : mergeIds.length) !== null && _a !== void 0
96
+ ? _a
97
+ : 0) +
98
+ ((_b = replaceIds === null || replaceIds === void 0 ? void 0 : replaceIds.length) !== null &&
99
+ _b !== void 0
100
+ ? _b
101
+ : 0) +
102
+ ((_c =
103
+ entitiesToRemove === null || entitiesToRemove === void 0 ? void 0 : entitiesToRemove.length) !==
104
+ null && _c !== void 0
105
+ ? _c
106
+ : 0)
107
+ if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
108
+ console.warn(
109
+ 'react-redux-cache.applyEntityChanges: merge, replace and remove changes have intersections for: ' +
110
+ typename
111
+ )
112
+ }
113
+ }
114
+ const oldEntities = (_d = entities[typename]) !== null && _d !== void 0 ? _d : exports.EMPTY_OBJECT
115
+ let newEntities
116
+ entitiesToRemove === null || entitiesToRemove === void 0
117
+ ? void 0
118
+ : entitiesToRemove.forEach((id) => {
119
+ if (oldEntities[id]) {
120
+ newEntities !== null && newEntities !== void 0
121
+ ? newEntities
122
+ : (newEntities = Object.assign({}, oldEntities))
123
+ delete newEntities[id]
124
+ }
125
+ })
126
+ if (entitiesToReplace) {
127
+ for (const id in entitiesToReplace) {
128
+ const newEntity = entitiesToReplace[id]
129
+ if (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntities[id], newEntity))) {
130
+ newEntities !== null && newEntities !== void 0
131
+ ? newEntities
132
+ : (newEntities = Object.assign({}, oldEntities))
133
+ newEntities[id] = newEntity
134
+ }
135
+ }
136
+ }
137
+ if (entitiesToMerge) {
138
+ for (const id in entitiesToMerge) {
139
+ const oldEntity = oldEntities[id]
140
+ const newEntity = Object.assign(Object.assign({}, oldEntity), entitiesToMerge[id])
141
+ if (!(deepEqual === null || deepEqual === void 0 ? void 0 : deepEqual(oldEntity, newEntity))) {
142
+ newEntities !== null && newEntities !== void 0
143
+ ? newEntities
144
+ : (newEntities = Object.assign({}, oldEntities))
145
+ newEntities[id] = newEntity
146
+ }
147
+ }
148
+ }
149
+ if (!newEntities) {
150
+ continue
151
+ }
152
+ result !== null && result !== void 0 ? result : (result = Object.assign({}, entities))
153
+ result[typename] = newEntities
154
+ }
155
+ options.logsEnabled &&
156
+ (0, exports.log)('applyEntityChanges', {
157
+ entities,
158
+ changes,
159
+ options,
160
+ result,
161
+ })
162
+ return result
163
+ }
164
+ exports.applyEntityChanges = applyEntityChanges
165
+ const isEmptyObject = (o) => {
166
+ for (const _ in o) {
167
+ return false
168
+ }
169
+ return true
170
+ }
171
+ exports.isEmptyObject = isEmptyObject
172
+ const createStateComparer = (fields) => {
173
+ return (x, y) => {
174
+ if (x === y) {
175
+ return true
176
+ }
177
+ if (x === undefined || y === undefined) {
178
+ return false
179
+ }
180
+ for (let i = 0; i < fields.length; i += 1) {
181
+ const key = fields[i]
182
+ if (x[key] !== y[key]) {
183
+ return false
184
+ }
185
+ }
186
+ return true
187
+ }
188
+ }
189
+ exports.createStateComparer = createStateComparer
@@ -0,0 +1,61 @@
1
+ import {PACKAGE_SHORT_NAME} from './utilsAndConstants'
2
+
3
+ export const createActions = (name) => {
4
+ const actionPrefix = `@${PACKAGE_SHORT_NAME}/${name}/`
5
+ const updateQueryStateAndEntitiesType = `${actionPrefix}updateQueryStateAndEntities`
6
+ const updateQueryStateAndEntities = (queryKey, queryCacheKey, state, entityChanges) => ({
7
+ type: updateQueryStateAndEntitiesType,
8
+ queryKey,
9
+ queryCacheKey,
10
+ state,
11
+ entityChanges,
12
+ })
13
+ updateQueryStateAndEntities.type = updateQueryStateAndEntitiesType
14
+ const updateMutationStateAndEntitiesType = `${actionPrefix}updateMutationStateAndEntities`
15
+ const updateMutationStateAndEntities = (mutationKey, state, entityChanges) => ({
16
+ type: updateMutationStateAndEntitiesType,
17
+ mutationKey,
18
+ state,
19
+ entityChanges,
20
+ })
21
+ updateMutationStateAndEntities.type = updateMutationStateAndEntitiesType
22
+ const mergeEntityChangesType = `${actionPrefix}mergeEntityChanges`
23
+ const mergeEntityChanges = (changes) => ({
24
+ type: mergeEntityChangesType,
25
+ changes,
26
+ })
27
+ mergeEntityChanges.type = mergeEntityChangesType
28
+ const invalidateQueryType = `${actionPrefix}invalidateQuery`
29
+ const invalidateQuery = (queries) => ({
30
+ type: invalidateQueryType,
31
+ queries,
32
+ })
33
+ invalidateQuery.type = invalidateQueryType
34
+ const clearQueryStateType = `${actionPrefix}clearQueryState`
35
+ const clearQueryState = (queries) => ({
36
+ type: clearQueryStateType,
37
+ queries,
38
+ })
39
+ clearQueryState.type = clearQueryStateType
40
+ const clearMutationStateType = `${actionPrefix}clearMutationState`
41
+ const clearMutationState = (mutationKeys) => ({
42
+ type: clearMutationStateType,
43
+ mutationKeys,
44
+ })
45
+ clearMutationState.type = clearMutationStateType
46
+ const clearCacheType = `${actionPrefix}clearCache`
47
+ const clearCache = (stateToKeep) => ({
48
+ type: clearCacheType,
49
+ stateToKeep,
50
+ })
51
+ clearCache.type = clearCacheType
52
+ return {
53
+ updateQueryStateAndEntities,
54
+ updateMutationStateAndEntities,
55
+ mergeEntityChanges,
56
+ invalidateQuery,
57
+ clearQueryState,
58
+ clearMutationState,
59
+ clearCache,
60
+ }
61
+ }
@@ -0,0 +1,203 @@
1
+ import {useMemo} from 'react'
2
+
3
+ import {createActions} from './createActions'
4
+ import {createCacheReducer} from './createCacheReducer'
5
+ import {createSelectors} from './createSelectors'
6
+ import {mutate as mutateImpl} from './mutate'
7
+ import {query as queryImpl} from './query'
8
+ import {useMutation} from './useMutation'
9
+ import {useQuery} from './useQuery'
10
+ import {
11
+ applyEntityChanges,
12
+ createStateComparer,
13
+ defaultGetCacheKey,
14
+ EMPTY_OBJECT,
15
+ FetchPolicy,
16
+ IS_DEV,
17
+ optionalUtils,
18
+ } from './utilsAndConstants'
19
+
20
+ export const withTypenames = () => {
21
+ return {
22
+ createCache: (partialCache) => {
23
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m
24
+ var _o, _p, _q, _r, _s, _t
25
+ const abortControllers = new WeakMap()
26
+ ;(_a = partialCache.options) !== null && _a !== void 0 ? _a : (partialCache.options = {})
27
+ ;(_b = (_o = partialCache.options).logsEnabled) !== null && _b !== void 0
28
+ ? _b
29
+ : (_o.logsEnabled = false)
30
+ ;(_c = (_p = partialCache.options).additionalValidation) !== null && _c !== void 0
31
+ ? _c
32
+ : (_p.additionalValidation = IS_DEV)
33
+ ;(_d = (_q = partialCache.options).deepComparisonEnabled) !== null && _d !== void 0
34
+ ? _d
35
+ : (_q.deepComparisonEnabled = true)
36
+ ;(_e = partialCache.globals) !== null && _e !== void 0 ? _e : (partialCache.globals = {})
37
+ ;(_f = (_r = partialCache.globals).queries) !== null && _f !== void 0 ? _f : (_r.queries = {})
38
+ ;(_g = (_s = partialCache.globals.queries).fetchPolicy) !== null && _g !== void 0
39
+ ? _g
40
+ : (_s.fetchPolicy = FetchPolicy.NoCacheOrExpired)
41
+ ;(_h = (_t = partialCache.globals.queries).skipFetch) !== null && _h !== void 0
42
+ ? _h
43
+ : (_t.skipFetch = false)
44
+ ;(_j = partialCache.cacheStateSelector) !== null && _j !== void 0
45
+ ? _j
46
+ : (partialCache.cacheStateSelector = (state) => state[cache.name])
47
+ ;(_k = partialCache.mutations) !== null && _k !== void 0 ? _k : (partialCache.mutations = {})
48
+ ;(_l = partialCache.queries) !== null && _l !== void 0 ? _l : (partialCache.queries = {})
49
+ partialCache.abortControllers = abortControllers
50
+ try {
51
+ ;(_m = partialCache.storeHooks) !== null && _m !== void 0
52
+ ? _m
53
+ : (partialCache.storeHooks = {
54
+ useStore: require('react-redux').useStore,
55
+ useSelector: require('react-redux').useSelector,
56
+ })
57
+ } catch (e) {
58
+ throw e
59
+ }
60
+ const cache = partialCache
61
+ if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
62
+ console.warn(
63
+ 'react-redux-cache: optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true'
64
+ )
65
+ }
66
+ const setDefaultComparer = (target) => {
67
+ if (
68
+ (target === null || target === void 0 ? void 0 : target.selectorComparer) != null &&
69
+ typeof target.selectorComparer === 'object'
70
+ ) {
71
+ target.selectorComparer = createStateComparer(target.selectorComparer)
72
+ }
73
+ }
74
+ setDefaultComparer(cache.globals.queries)
75
+ for (const queryKey in partialCache.queries) {
76
+ setDefaultComparer(partialCache.queries[queryKey])
77
+ }
78
+ const selectors = Object.assign({selectCacheState: cache.cacheStateSelector}, createSelectors(cache))
79
+ const {
80
+ selectCacheState,
81
+ selectQueryState,
82
+ selectQueryResult,
83
+ selectQueryLoading,
84
+ selectQueryError,
85
+ selectQueryParams,
86
+ selectQueryExpiresAt,
87
+ selectMutationState,
88
+ selectMutationResult,
89
+ selectMutationLoading,
90
+ selectMutationError,
91
+ selectMutationParams,
92
+ selectEntityById,
93
+ selectEntities,
94
+ selectEntitiesByTypename,
95
+ } = selectors
96
+ const actions = createActions(cache.name)
97
+ const {
98
+ updateQueryStateAndEntities,
99
+ updateMutationStateAndEntities,
100
+ mergeEntityChanges,
101
+ invalidateQuery,
102
+ clearQueryState,
103
+ clearMutationState,
104
+ clearCache,
105
+ } = actions
106
+ const reducer = createCacheReducer(actions, Object.keys(cache.queries), cache.options)
107
+ const createClient = (store) => {
108
+ const client = {
109
+ query: (options) => {
110
+ var _a
111
+ const {query: queryKey, params} = options
112
+ const getCacheKey =
113
+ (_a = cache.queries[queryKey].getCacheKey) !== null && _a !== void 0 ? _a : defaultGetCacheKey
114
+ const cacheKey = getCacheKey(params)
115
+ return queryImpl(
116
+ 'query',
117
+ store,
118
+ cache,
119
+ actions,
120
+ selectors,
121
+ queryKey,
122
+ cacheKey,
123
+ params,
124
+ options.secondsToLive,
125
+ options.onlyIfExpired,
126
+ options.mergeResults,
127
+ options.onCompleted,
128
+ options.onSuccess,
129
+ options.onError
130
+ )
131
+ },
132
+ mutate: (options) => {
133
+ return mutateImpl(
134
+ 'mutate',
135
+ store,
136
+ cache,
137
+ actions,
138
+ selectors,
139
+ options.mutation,
140
+ options.params,
141
+ abortControllers,
142
+ options.onCompleted,
143
+ options.onSuccess,
144
+ options.onError
145
+ )
146
+ },
147
+ }
148
+ return client
149
+ }
150
+ return {
151
+ cache,
152
+ reducer,
153
+ actions: {
154
+ updateQueryStateAndEntities,
155
+ updateMutationStateAndEntities,
156
+ mergeEntityChanges,
157
+ invalidateQuery,
158
+ clearQueryState,
159
+ clearMutationState,
160
+ clearCache,
161
+ },
162
+ selectors: {
163
+ selectCacheState,
164
+ selectQueryState,
165
+ selectQueryResult,
166
+ selectQueryLoading,
167
+ selectQueryError,
168
+ selectQueryParams,
169
+ selectQueryExpiresAt,
170
+ selectMutationState,
171
+ selectMutationResult,
172
+ selectMutationLoading,
173
+ selectMutationError,
174
+ selectMutationParams,
175
+ selectEntityById,
176
+ selectEntities,
177
+ selectEntitiesByTypename,
178
+ },
179
+ hooks: {
180
+ useClient: () => {
181
+ const store = cache.storeHooks.useStore()
182
+ return useMemo(() => createClient(store), [store])
183
+ },
184
+ useQuery: (options) => useQuery(cache, actions, selectors, options),
185
+ useMutation: (options) => useMutation(cache, actions, selectors, options, abortControllers),
186
+ useSelectEntityById: (id, typename) => {
187
+ return cache.storeHooks.useSelector((state) => selectEntityById(state, id, typename))
188
+ },
189
+ },
190
+ utils: {
191
+ createClient,
192
+ getInitialState: () => {
193
+ return reducer(undefined, EMPTY_OBJECT)
194
+ },
195
+ applyEntityChanges: (entities, changes) => {
196
+ return applyEntityChanges(entities, changes, cache.options)
197
+ },
198
+ },
199
+ }
200
+ },
201
+ }
202
+ }
203
+ export const createCache = withTypenames().createCache