react-redux-cache 0.19.5 → 0.20.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.
- package/README.md +3 -1
- package/dist/cjs/createCache.js +5 -8
- package/dist/cjs/{createCacheReducer.js → createReducer.js} +4 -4
- package/dist/cjs/index.js +8 -1
- package/dist/cjs/mutate.js +2 -2
- package/dist/cjs/query.js +11 -24
- package/dist/cjs/useMutation.js +5 -14
- package/dist/cjs/useQuery.js +3 -8
- package/dist/cjs/utilsAndConstants.js +32 -29
- package/dist/esm/createCache.js +7 -4
- package/dist/esm/{createCacheReducer.js → createReducer.js} +4 -3
- package/dist/esm/index.js +3 -1
- package/dist/esm/mutate.js +4 -11
- package/dist/esm/query.js +14 -29
- package/dist/esm/useMutation.js +6 -15
- package/dist/esm/useQuery.js +5 -9
- package/dist/esm/utilsAndConstants.js +35 -24
- package/dist/types/createActions.d.ts +1 -0
- package/dist/types/createCache.d.ts +120 -111
- package/dist/types/{createCacheReducer.d.ts → createReducer.d.ts} +1 -1
- package/dist/types/createSelectors.d.ts +1 -0
- package/dist/types/index.d.ts +3 -1
- package/dist/types/mutate.d.ts +1 -1
- package/dist/types/query.d.ts +1 -1
- package/dist/types/types.d.ts +31 -0
- package/dist/types/useMutation.d.ts +1 -1
- package/dist/types/useQuery.d.ts +1 -1
- package/dist/types/utilsAndConstants.d.ts +29 -12
- package/package.json +4 -3
package/dist/esm/useQuery.js
CHANGED
|
@@ -32,7 +32,7 @@ var __awaiter =
|
|
|
32
32
|
import {useCallback, useEffect} from 'react'
|
|
33
33
|
|
|
34
34
|
import {query as queryImpl} from './query'
|
|
35
|
-
import {createStateComparer, defaultGetCacheKey, EMPTY_OBJECT,
|
|
35
|
+
import {createStateComparer, defaultGetCacheKey, EMPTY_OBJECT, logDebug} from './utilsAndConstants'
|
|
36
36
|
|
|
37
37
|
export const useQuery = (cache, actions, selectors, options) => {
|
|
38
38
|
var _a, _b, _c, _d, _e
|
|
@@ -99,13 +99,13 @@ export const useQuery = (cache, actions, selectors, options) => {
|
|
|
99
99
|
: EMPTY_OBJECT
|
|
100
100
|
useEffect(() => {
|
|
101
101
|
if (skipFetch) {
|
|
102
|
-
logsEnabled &&
|
|
102
|
+
logsEnabled && logDebug('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
|
|
103
103
|
return
|
|
104
104
|
}
|
|
105
105
|
const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now()
|
|
106
106
|
if (!fetchPolicy(expired, params, queryState, store, selectors)) {
|
|
107
107
|
logsEnabled &&
|
|
108
|
-
|
|
108
|
+
logDebug('useQuery.useEffect skip fetch due to fetch policy', {
|
|
109
109
|
queryState,
|
|
110
110
|
expired,
|
|
111
111
|
queryKey,
|
|
@@ -115,12 +115,8 @@ export const useQuery = (cache, actions, selectors, options) => {
|
|
|
115
115
|
}
|
|
116
116
|
performFetch()
|
|
117
117
|
}, [cacheKey, skipFetch])
|
|
118
|
-
logsEnabled &&
|
|
119
|
-
log('useQuery', {
|
|
120
|
-
cacheKey,
|
|
121
|
-
options,
|
|
122
|
-
queryState,
|
|
123
|
-
})
|
|
118
|
+
logsEnabled && logDebug('useQuery', {cacheKey, options, queryState})
|
|
124
119
|
return [queryState, performFetch]
|
|
125
120
|
}
|
|
121
|
+
|
|
126
122
|
const defaultStateComparer = createStateComparer(['result', 'loading', 'params', 'error'])
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
export const PACKAGE_SHORT_NAME = 'rrc'
|
|
2
|
+
|
|
2
3
|
export const optionalUtils = {
|
|
3
4
|
deepEqual: undefined,
|
|
4
5
|
}
|
|
6
|
+
|
|
7
|
+
export const logDebug = (tag, data) => {
|
|
8
|
+
console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const logWarn = (tag, data) => {
|
|
12
|
+
console.warn(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
try {
|
|
6
16
|
optionalUtils.deepEqual = require('fast-deep-equal/es6')
|
|
7
17
|
} catch (_a) {
|
|
8
|
-
|
|
18
|
+
logDebug('deepEqual', 'fast-deep-equal optional dependency was not installed')
|
|
9
19
|
}
|
|
20
|
+
|
|
10
21
|
export const IS_DEV = (() => {
|
|
11
22
|
try {
|
|
12
23
|
return __DEV__
|
|
@@ -14,9 +25,13 @@ export const IS_DEV = (() => {
|
|
|
14
25
|
return process.env.NODE_ENV === 'development'
|
|
15
26
|
}
|
|
16
27
|
})()
|
|
28
|
+
|
|
17
29
|
export const EMPTY_OBJECT = Object.freeze({})
|
|
30
|
+
|
|
18
31
|
export const EMPTY_ARRAY = Object.freeze([])
|
|
19
|
-
|
|
32
|
+
|
|
33
|
+
export const noop = () => {}
|
|
34
|
+
|
|
20
35
|
export const defaultGetCacheKey = (params) => {
|
|
21
36
|
switch (typeof params) {
|
|
22
37
|
case 'string':
|
|
@@ -28,19 +43,11 @@ export const defaultGetCacheKey = (params) => {
|
|
|
28
43
|
return String(params)
|
|
29
44
|
}
|
|
30
45
|
}
|
|
31
|
-
|
|
32
|
-
console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
33
|
-
}
|
|
34
|
-
export const FetchPolicy = {
|
|
35
|
-
NoCacheOrExpired: (expired, _params, state) => {
|
|
36
|
-
return expired || state.result === undefined
|
|
37
|
-
},
|
|
38
|
-
Always: () => true,
|
|
39
|
-
}
|
|
46
|
+
|
|
40
47
|
export const applyEntityChanges = (entities, changes, options) => {
|
|
41
48
|
var _a, _b, _c, _d
|
|
42
49
|
if (changes.merge && changes.entities) {
|
|
43
|
-
|
|
50
|
+
logWarn('applyEntityChanges', 'merge and entities should not be both set')
|
|
44
51
|
}
|
|
45
52
|
const {merge = changes.entities, replace, remove} = changes
|
|
46
53
|
if (!merge && !replace && !remove) {
|
|
@@ -48,13 +55,11 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
48
55
|
}
|
|
49
56
|
const deepEqual = options.deepComparisonEnabled ? optionalUtils.deepEqual : undefined
|
|
50
57
|
let result
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
])
|
|
57
|
-
for (const typename of typenames) {
|
|
58
|
+
const objectWithAllTypenames = Object.assign(
|
|
59
|
+
Object.assign(Object.assign({}, changes.merge), changes.remove),
|
|
60
|
+
changes.replace
|
|
61
|
+
)
|
|
62
|
+
for (const typename in objectWithAllTypenames) {
|
|
58
63
|
const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
|
|
59
64
|
const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
|
|
60
65
|
const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
|
|
@@ -87,10 +92,7 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
87
92
|
? _c
|
|
88
93
|
: 0)
|
|
89
94
|
if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
|
|
90
|
-
|
|
91
|
-
'react-redux-cache.applyEntityChanges: merge, replace and remove changes have intersections for: ' +
|
|
92
|
-
typename
|
|
93
|
-
)
|
|
95
|
+
logWarn('applyEntityChanges', 'merge, replace and remove changes have intersections for: ' + typename)
|
|
94
96
|
}
|
|
95
97
|
}
|
|
96
98
|
const oldEntities = (_d = entities[typename]) !== null && _d !== void 0 ? _d : EMPTY_OBJECT
|
|
@@ -135,7 +137,7 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
135
137
|
result[typename] = newEntities
|
|
136
138
|
}
|
|
137
139
|
options.logsEnabled &&
|
|
138
|
-
|
|
140
|
+
logDebug('applyEntityChanges', {
|
|
139
141
|
entities,
|
|
140
142
|
changes,
|
|
141
143
|
options,
|
|
@@ -143,12 +145,14 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
143
145
|
})
|
|
144
146
|
return result
|
|
145
147
|
}
|
|
148
|
+
|
|
146
149
|
export const isEmptyObject = (o) => {
|
|
147
150
|
for (const _ in o) {
|
|
148
151
|
return false
|
|
149
152
|
}
|
|
150
153
|
return true
|
|
151
154
|
}
|
|
155
|
+
|
|
152
156
|
export const createStateComparer = (fields) => {
|
|
153
157
|
return (x, y) => {
|
|
154
158
|
if (x === y) {
|
|
@@ -166,3 +170,10 @@ export const createStateComparer = (fields) => {
|
|
|
166
170
|
return true
|
|
167
171
|
}
|
|
168
172
|
}
|
|
173
|
+
|
|
174
|
+
export const FetchPolicy = {
|
|
175
|
+
NoCacheOrExpired: (expired, _params, state) => {
|
|
176
|
+
return expired || state.result === undefined
|
|
177
|
+
},
|
|
178
|
+
Always: () => true,
|
|
179
|
+
}
|