react-redux-cache 0.19.6 → 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 +1 -1
- package/dist/cjs/createCache.js +3 -2
- package/dist/cjs/createReducer.js +1 -1
- 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 +1 -5
- package/dist/cjs/useQuery.js +3 -8
- package/dist/cjs/utilsAndConstants.js +24 -21
- package/dist/esm/createCache.js +4 -2
- package/dist/esm/createReducer.js +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/mutate.js +3 -11
- package/dist/esm/query.js +13 -29
- package/dist/esm/useMutation.js +2 -6
- package/dist/esm/useQuery.js +4 -9
- package/dist/esm/utilsAndConstants.js +18 -19
- package/dist/types/createCache.d.ts +34 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types.d.ts +1 -0
- package/dist/types/utilsAndConstants.d.ts +6 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with
|
|
|
23
23
|
|Not overengineered|Simplicity is the main goal.|
|
|
24
24
|
|Performance|Every function is heavily optimized, Immer is not used ([RTK [Query] issue](https://github.com/reduxjs/redux-toolkit/issues/4793)).|
|
|
25
25
|
|Reliability|High test coverage, zero issue policy.|
|
|
26
|
-
|Lightweight|`npx minified-size dist/esm/*.js`<br/>minified: 18.2 kB<br/>gzipped: 7.91 kB<br/>brotlied: 7.03 kB
|
|
26
|
+
|Lightweight|`npx minified-size dist/esm/*.js`<br/>minified: 18.2 kB<br/>gzipped: 7.91 kB<br/>brotlied: 7.03 kB
|
|
27
27
|
|
|
28
28
|
|Feature|Description|
|
|
29
29
|
|--|--|
|
package/dist/cjs/createCache.js
CHANGED
|
@@ -53,8 +53,9 @@ const withTypenames = () => {
|
|
|
53
53
|
}
|
|
54
54
|
const cache = partialCache
|
|
55
55
|
if (cache.options.deepComparisonEnabled && !utilsAndConstants_1.optionalUtils.deepEqual) {
|
|
56
|
-
|
|
57
|
-
'
|
|
56
|
+
;(0, utilsAndConstants_1.logWarn)(
|
|
57
|
+
'createCache',
|
|
58
|
+
'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true'
|
|
58
59
|
)
|
|
59
60
|
}
|
|
60
61
|
const setDefaultComparer = (target) => {
|
|
@@ -35,7 +35,7 @@ const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
35
35
|
mutations: Object.freeze({}),
|
|
36
36
|
})
|
|
37
37
|
cacheOptions.logsEnabled &&
|
|
38
|
-
(0, utilsAndConstants_1.
|
|
38
|
+
(0, utilsAndConstants_1.logDebug)('createCacheReducer', {
|
|
39
39
|
queryKeys,
|
|
40
40
|
initialState,
|
|
41
41
|
})
|
package/dist/cjs/index.js
CHANGED
|
@@ -33,7 +33,8 @@ var __exportStar =
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
Object.defineProperty(exports, '__esModule', {value: true})
|
|
36
|
-
exports.
|
|
36
|
+
exports.noop =
|
|
37
|
+
exports.isEmptyObject =
|
|
37
38
|
exports.FetchPolicy =
|
|
38
39
|
exports.defaultGetCacheKey =
|
|
39
40
|
exports.createStateComparer =
|
|
@@ -81,3 +82,9 @@ Object.defineProperty(exports, 'isEmptyObject', {
|
|
|
81
82
|
return utilsAndConstants_1.isEmptyObject
|
|
82
83
|
},
|
|
83
84
|
})
|
|
85
|
+
Object.defineProperty(exports, 'noop', {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
get() {
|
|
88
|
+
return utilsAndConstants_1.noop
|
|
89
|
+
},
|
|
90
|
+
})
|
package/dist/cjs/mutate.js
CHANGED
|
@@ -82,7 +82,7 @@ const mutate = (
|
|
|
82
82
|
{
|
|
83
83
|
const abortController = abortControllersOfStore[mutationKey]
|
|
84
84
|
cache.options.logsEnabled &&
|
|
85
|
-
(0, utilsAndConstants_1.
|
|
85
|
+
(0, utilsAndConstants_1.logDebug)(logTag, {
|
|
86
86
|
mutationKey,
|
|
87
87
|
params,
|
|
88
88
|
previousAborted: abortController !== undefined,
|
|
@@ -109,7 +109,7 @@ const mutate = (
|
|
|
109
109
|
error = e
|
|
110
110
|
}
|
|
111
111
|
cache.options.logsEnabled &&
|
|
112
|
-
(0, utilsAndConstants_1.
|
|
112
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} finished`, {
|
|
113
113
|
response,
|
|
114
114
|
error,
|
|
115
115
|
aborted: abortController.signal.aborted,
|
package/dist/cjs/query.js
CHANGED
|
@@ -75,27 +75,19 @@ const query = (
|
|
|
75
75
|
const logsEnabled = cache.options.logsEnabled
|
|
76
76
|
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
|
|
77
77
|
if (skipFetch) {
|
|
78
|
-
return {
|
|
79
|
-
result: queryStateOnStart.result,
|
|
80
|
-
}
|
|
78
|
+
return {result: queryStateOnStart.result}
|
|
81
79
|
}
|
|
82
80
|
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
83
81
|
logsEnabled &&
|
|
84
|
-
(0, utilsAndConstants_1.
|
|
82
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} fetch cancelled: already loading`, {
|
|
85
83
|
queryStateOnStart,
|
|
86
84
|
params,
|
|
87
85
|
cacheKey,
|
|
88
86
|
})
|
|
89
|
-
const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
error,
|
|
94
|
-
}
|
|
95
|
-
: {
|
|
96
|
-
cancelled: 'loading',
|
|
97
|
-
result: selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
98
|
-
}
|
|
87
|
+
const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.noop).catch(catchAndReturn)
|
|
88
|
+
const result = selectQueryResult(store.getState(), queryKey, cacheKey)
|
|
89
|
+
const cancelled = 'loading'
|
|
90
|
+
return error ? {cancelled, result, error} : {cancelled, result}
|
|
99
91
|
}
|
|
100
92
|
if (
|
|
101
93
|
onlyIfExpired &&
|
|
@@ -104,16 +96,13 @@ const query = (
|
|
|
104
96
|
queryStateOnStart.expiresAt > Date.now()
|
|
105
97
|
) {
|
|
106
98
|
logsEnabled &&
|
|
107
|
-
(0, utilsAndConstants_1.
|
|
99
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} fetch cancelled: not expired yet`, {
|
|
108
100
|
queryStateOnStart,
|
|
109
101
|
params,
|
|
110
102
|
cacheKey,
|
|
111
103
|
onlyIfExpired,
|
|
112
104
|
})
|
|
113
|
-
return {
|
|
114
|
-
cancelled: 'not-expired',
|
|
115
|
-
result: queryStateOnStart.result,
|
|
116
|
-
}
|
|
105
|
+
return {cancelled: 'not-expired', result: queryStateOnStart.result}
|
|
117
106
|
}
|
|
118
107
|
const {updateQueryStateAndEntities} = actions
|
|
119
108
|
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
@@ -124,7 +113,7 @@ const query = (
|
|
|
124
113
|
})
|
|
125
114
|
)
|
|
126
115
|
logsEnabled &&
|
|
127
|
-
(0, utilsAndConstants_1.
|
|
116
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} started`, {
|
|
128
117
|
queryKey,
|
|
129
118
|
params,
|
|
130
119
|
cacheKey,
|
|
@@ -149,7 +138,7 @@ const query = (
|
|
|
149
138
|
onCompleted === null || onCompleted === void 0
|
|
150
139
|
? void 0
|
|
151
140
|
: onCompleted(undefined, error, params, store, actions, selectors)
|
|
152
|
-
return {error}
|
|
141
|
+
return {error, result: selectQueryResult(store.getState(), queryKey, cacheKey)}
|
|
153
142
|
}
|
|
154
143
|
const newState = {
|
|
155
144
|
error: undefined,
|
|
@@ -178,9 +167,7 @@ const query = (
|
|
|
178
167
|
onCompleted === null || onCompleted === void 0
|
|
179
168
|
? void 0
|
|
180
169
|
: onCompleted(response, undefined, params, store, actions, selectors)
|
|
181
|
-
return {
|
|
182
|
-
result: newState === null || newState === void 0 ? void 0 : newState.result,
|
|
183
|
-
}
|
|
170
|
+
return {result: newState === null || newState === void 0 ? void 0 : newState.result}
|
|
184
171
|
})
|
|
185
172
|
exports.query = query
|
|
186
173
|
const catchAndReturn = (x) => x
|
package/dist/cjs/useMutation.js
CHANGED
|
@@ -67,11 +67,7 @@ const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
|
67
67
|
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0
|
|
68
68
|
? _a
|
|
69
69
|
: utilsAndConstants_1.EMPTY_OBJECT
|
|
70
|
-
cache.options.logsEnabled &&
|
|
71
|
-
(0, utilsAndConstants_1.log)('useMutation', {
|
|
72
|
-
options,
|
|
73
|
-
mutationState,
|
|
74
|
-
})
|
|
70
|
+
cache.options.logsEnabled && (0, utilsAndConstants_1.logDebug)('useMutation', {options, mutationState})
|
|
75
71
|
return [mutate, mutationState, abort]
|
|
76
72
|
}
|
|
77
73
|
exports.useMutation = useMutation
|
package/dist/cjs/useQuery.js
CHANGED
|
@@ -88,13 +88,13 @@ const useQuery = (cache, actions, selectors, options) => {
|
|
|
88
88
|
;(0, react_1.useEffect)(() => {
|
|
89
89
|
if (skipFetch) {
|
|
90
90
|
logsEnabled &&
|
|
91
|
-
(0, utilsAndConstants_1.
|
|
91
|
+
(0, utilsAndConstants_1.logDebug)('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
|
|
92
92
|
return
|
|
93
93
|
}
|
|
94
94
|
const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now()
|
|
95
95
|
if (!fetchPolicy(expired, params, queryState, store, selectors)) {
|
|
96
96
|
logsEnabled &&
|
|
97
|
-
(0, utilsAndConstants_1.
|
|
97
|
+
(0, utilsAndConstants_1.logDebug)('useQuery.useEffect skip fetch due to fetch policy', {
|
|
98
98
|
queryState,
|
|
99
99
|
expired,
|
|
100
100
|
queryKey,
|
|
@@ -104,12 +104,7 @@ const useQuery = (cache, actions, selectors, options) => {
|
|
|
104
104
|
}
|
|
105
105
|
performFetch()
|
|
106
106
|
}, [cacheKey, skipFetch])
|
|
107
|
-
logsEnabled &&
|
|
108
|
-
(0, utilsAndConstants_1.log)('useQuery', {
|
|
109
|
-
cacheKey,
|
|
110
|
-
options,
|
|
111
|
-
queryState,
|
|
112
|
-
})
|
|
107
|
+
logsEnabled && (0, utilsAndConstants_1.logDebug)('useQuery', {cacheKey, options, queryState})
|
|
113
108
|
return [queryState, performFetch]
|
|
114
109
|
}
|
|
115
110
|
exports.useQuery = useQuery
|
|
@@ -4,12 +4,13 @@ exports.FetchPolicy =
|
|
|
4
4
|
exports.createStateComparer =
|
|
5
5
|
exports.isEmptyObject =
|
|
6
6
|
exports.applyEntityChanges =
|
|
7
|
-
exports.log =
|
|
8
7
|
exports.defaultGetCacheKey =
|
|
9
|
-
exports.
|
|
8
|
+
exports.noop =
|
|
10
9
|
exports.EMPTY_ARRAY =
|
|
11
10
|
exports.EMPTY_OBJECT =
|
|
12
11
|
exports.IS_DEV =
|
|
12
|
+
exports.logWarn =
|
|
13
|
+
exports.logDebug =
|
|
13
14
|
exports.optionalUtils =
|
|
14
15
|
exports.PACKAGE_SHORT_NAME =
|
|
15
16
|
void 0
|
|
@@ -17,10 +18,18 @@ exports.PACKAGE_SHORT_NAME = 'rrc'
|
|
|
17
18
|
exports.optionalUtils = {
|
|
18
19
|
deepEqual: undefined,
|
|
19
20
|
}
|
|
21
|
+
const logDebug = (tag, data) => {
|
|
22
|
+
console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
23
|
+
}
|
|
24
|
+
exports.logDebug = logDebug
|
|
25
|
+
const logWarn = (tag, data) => {
|
|
26
|
+
console.warn(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
27
|
+
}
|
|
28
|
+
exports.logWarn = logWarn
|
|
20
29
|
try {
|
|
21
30
|
exports.optionalUtils.deepEqual = require('fast-deep-equal/es6')
|
|
22
31
|
} catch (_a) {
|
|
23
|
-
|
|
32
|
+
;(0, exports.logDebug)('deepEqual', 'fast-deep-equal optional dependency was not installed')
|
|
24
33
|
}
|
|
25
34
|
exports.IS_DEV = (() => {
|
|
26
35
|
try {
|
|
@@ -31,8 +40,8 @@ exports.IS_DEV = (() => {
|
|
|
31
40
|
})()
|
|
32
41
|
exports.EMPTY_OBJECT = Object.freeze({})
|
|
33
42
|
exports.EMPTY_ARRAY = Object.freeze([])
|
|
34
|
-
const
|
|
35
|
-
exports.
|
|
43
|
+
const noop = () => {}
|
|
44
|
+
exports.noop = noop
|
|
36
45
|
const defaultGetCacheKey = (params) => {
|
|
37
46
|
switch (typeof params) {
|
|
38
47
|
case 'string':
|
|
@@ -45,14 +54,10 @@ const defaultGetCacheKey = (params) => {
|
|
|
45
54
|
}
|
|
46
55
|
}
|
|
47
56
|
exports.defaultGetCacheKey = defaultGetCacheKey
|
|
48
|
-
const log = (tag, data) => {
|
|
49
|
-
console.debug(`@${exports.PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
50
|
-
}
|
|
51
|
-
exports.log = log
|
|
52
57
|
const applyEntityChanges = (entities, changes, options) => {
|
|
53
58
|
var _a, _b, _c, _d
|
|
54
59
|
if (changes.merge && changes.entities) {
|
|
55
|
-
|
|
60
|
+
;(0, exports.logWarn)('applyEntityChanges', 'merge and entities should not be both set')
|
|
56
61
|
}
|
|
57
62
|
const {merge = changes.entities, replace, remove} = changes
|
|
58
63
|
if (!merge && !replace && !remove) {
|
|
@@ -60,13 +65,11 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
60
65
|
}
|
|
61
66
|
const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined
|
|
62
67
|
let result
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
])
|
|
69
|
-
for (const typename of typenames) {
|
|
68
|
+
const objectWithAllTypenames = Object.assign(
|
|
69
|
+
Object.assign(Object.assign({}, changes.merge), changes.remove),
|
|
70
|
+
changes.replace
|
|
71
|
+
)
|
|
72
|
+
for (const typename in objectWithAllTypenames) {
|
|
70
73
|
const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
|
|
71
74
|
const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
|
|
72
75
|
const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
|
|
@@ -99,9 +102,9 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
99
102
|
? _c
|
|
100
103
|
: 0)
|
|
101
104
|
if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
|
|
102
|
-
|
|
103
|
-
'
|
|
104
|
-
|
|
105
|
+
;(0, exports.logWarn)(
|
|
106
|
+
'applyEntityChanges',
|
|
107
|
+
'merge, replace and remove changes have intersections for: ' + typename
|
|
105
108
|
)
|
|
106
109
|
}
|
|
107
110
|
}
|
|
@@ -147,7 +150,7 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
147
150
|
result[typename] = newEntities
|
|
148
151
|
}
|
|
149
152
|
options.logsEnabled &&
|
|
150
|
-
(0, exports.
|
|
153
|
+
(0, exports.logDebug)('applyEntityChanges', {
|
|
151
154
|
entities,
|
|
152
155
|
changes,
|
|
153
156
|
options,
|
package/dist/esm/createCache.js
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
EMPTY_OBJECT,
|
|
15
15
|
FetchPolicy,
|
|
16
16
|
IS_DEV,
|
|
17
|
+
logWarn,
|
|
17
18
|
optionalUtils,
|
|
18
19
|
} from './utilsAndConstants'
|
|
19
20
|
|
|
@@ -59,8 +60,9 @@ export const withTypenames = () => {
|
|
|
59
60
|
}
|
|
60
61
|
const cache = partialCache
|
|
61
62
|
if (cache.options.deepComparisonEnabled && !optionalUtils.deepEqual) {
|
|
62
|
-
|
|
63
|
-
'
|
|
63
|
+
logWarn(
|
|
64
|
+
'createCache',
|
|
65
|
+
'optional dependency for fast-deep-equal was not provided, while deepComparisonEnabled option is true'
|
|
64
66
|
)
|
|
65
67
|
}
|
|
66
68
|
const setDefaultComparer = (target) => {
|
|
@@ -16,7 +16,7 @@ var __rest =
|
|
|
16
16
|
}
|
|
17
17
|
return t
|
|
18
18
|
}
|
|
19
|
-
import {applyEntityChanges, EMPTY_OBJECT, isEmptyObject,
|
|
19
|
+
import {applyEntityChanges, EMPTY_OBJECT, isEmptyObject, logDebug, optionalUtils} from './utilsAndConstants'
|
|
20
20
|
|
|
21
21
|
const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
|
|
22
22
|
const optionalMutationKeys = ['error', 'result', 'params', 'loading']
|
|
@@ -33,7 +33,7 @@ export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
33
33
|
mutations: Object.freeze({}),
|
|
34
34
|
})
|
|
35
35
|
cacheOptions.logsEnabled &&
|
|
36
|
-
|
|
36
|
+
logDebug('createCacheReducer', {
|
|
37
37
|
queryKeys,
|
|
38
38
|
initialState,
|
|
39
39
|
})
|
package/dist/esm/index.js
CHANGED
|
@@ -2,4 +2,4 @@ export {createCache, withTypenames} from './createCache'
|
|
|
2
2
|
|
|
3
3
|
export * from './types'
|
|
4
4
|
|
|
5
|
-
export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject} from './utilsAndConstants'
|
|
5
|
+
export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject, noop} from './utilsAndConstants'
|
package/dist/esm/mutate.js
CHANGED
|
@@ -29,7 +29,7 @@ var __awaiter =
|
|
|
29
29
|
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
30
30
|
})
|
|
31
31
|
}
|
|
32
|
-
import {
|
|
32
|
+
import {logDebug} from './utilsAndConstants'
|
|
33
33
|
|
|
34
34
|
export const mutate = (
|
|
35
35
|
logTag_1,
|
|
@@ -79,11 +79,7 @@ export const mutate = (
|
|
|
79
79
|
{
|
|
80
80
|
const abortController = abortControllersOfStore[mutationKey]
|
|
81
81
|
cache.options.logsEnabled &&
|
|
82
|
-
|
|
83
|
-
mutationKey,
|
|
84
|
-
params,
|
|
85
|
-
previousAborted: abortController !== undefined,
|
|
86
|
-
})
|
|
82
|
+
logDebug(logTag, {mutationKey, params, previousAborted: abortController !== undefined})
|
|
87
83
|
if (abortController !== undefined) {
|
|
88
84
|
abortController.abort()
|
|
89
85
|
}
|
|
@@ -106,11 +102,7 @@ export const mutate = (
|
|
|
106
102
|
error = e
|
|
107
103
|
}
|
|
108
104
|
cache.options.logsEnabled &&
|
|
109
|
-
|
|
110
|
-
response,
|
|
111
|
-
error,
|
|
112
|
-
aborted: abortController.signal.aborted,
|
|
113
|
-
})
|
|
105
|
+
logDebug(`${logTag} finished`, {response, error, aborted: abortController.signal.aborted})
|
|
114
106
|
if (abortController.signal.aborted) {
|
|
115
107
|
return ABORTED_RESULT
|
|
116
108
|
}
|
package/dist/esm/query.js
CHANGED
|
@@ -29,7 +29,7 @@ var __awaiter =
|
|
|
29
29
|
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
30
30
|
})
|
|
31
31
|
}
|
|
32
|
-
import {
|
|
32
|
+
import {logDebug, noop} from './utilsAndConstants'
|
|
33
33
|
|
|
34
34
|
export const query = (
|
|
35
35
|
logTag,
|
|
@@ -72,27 +72,15 @@ export const query = (
|
|
|
72
72
|
const logsEnabled = cache.options.logsEnabled
|
|
73
73
|
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
|
|
74
74
|
if (skipFetch) {
|
|
75
|
-
return {
|
|
76
|
-
result: queryStateOnStart.result,
|
|
77
|
-
}
|
|
75
|
+
return {result: queryStateOnStart.result}
|
|
78
76
|
}
|
|
79
77
|
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
80
78
|
logsEnabled &&
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const error = yield queryStateOnStart.loading.then(NOOP).catch(catchAndReturn)
|
|
87
|
-
return error
|
|
88
|
-
? {
|
|
89
|
-
cancelled: 'loading',
|
|
90
|
-
error,
|
|
91
|
-
}
|
|
92
|
-
: {
|
|
93
|
-
cancelled: 'loading',
|
|
94
|
-
result: selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
95
|
-
}
|
|
79
|
+
logDebug(`${logTag} fetch cancelled: already loading`, {queryStateOnStart, params, cacheKey})
|
|
80
|
+
const error = yield queryStateOnStart.loading.then(noop).catch(catchAndReturn)
|
|
81
|
+
const result = selectQueryResult(store.getState(), queryKey, cacheKey)
|
|
82
|
+
const cancelled = 'loading'
|
|
83
|
+
return error ? {cancelled, result, error} : {cancelled, result}
|
|
96
84
|
}
|
|
97
85
|
if (
|
|
98
86
|
onlyIfExpired &&
|
|
@@ -101,16 +89,13 @@ export const query = (
|
|
|
101
89
|
queryStateOnStart.expiresAt > Date.now()
|
|
102
90
|
) {
|
|
103
91
|
logsEnabled &&
|
|
104
|
-
|
|
92
|
+
logDebug(`${logTag} fetch cancelled: not expired yet`, {
|
|
105
93
|
queryStateOnStart,
|
|
106
94
|
params,
|
|
107
95
|
cacheKey,
|
|
108
96
|
onlyIfExpired,
|
|
109
97
|
})
|
|
110
|
-
return {
|
|
111
|
-
cancelled: 'not-expired',
|
|
112
|
-
result: queryStateOnStart.result,
|
|
113
|
-
}
|
|
98
|
+
return {cancelled: 'not-expired', result: queryStateOnStart.result}
|
|
114
99
|
}
|
|
115
100
|
const {updateQueryStateAndEntities} = actions
|
|
116
101
|
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
@@ -120,7 +105,8 @@ export const query = (
|
|
|
120
105
|
params,
|
|
121
106
|
})
|
|
122
107
|
)
|
|
123
|
-
logsEnabled &&
|
|
108
|
+
logsEnabled &&
|
|
109
|
+
logDebug(`${logTag} started`, {queryKey, params, cacheKey, queryStateOnStart, onlyIfExpired})
|
|
124
110
|
let response
|
|
125
111
|
try {
|
|
126
112
|
response = yield fetchPromise
|
|
@@ -139,7 +125,7 @@ export const query = (
|
|
|
139
125
|
onCompleted === null || onCompleted === void 0
|
|
140
126
|
? void 0
|
|
141
127
|
: onCompleted(undefined, error, params, store, actions, selectors)
|
|
142
|
-
return {error}
|
|
128
|
+
return {error, result: selectQueryResult(store.getState(), queryKey, cacheKey)}
|
|
143
129
|
}
|
|
144
130
|
const newState = {
|
|
145
131
|
error: undefined,
|
|
@@ -168,9 +154,7 @@ export const query = (
|
|
|
168
154
|
onCompleted === null || onCompleted === void 0
|
|
169
155
|
? void 0
|
|
170
156
|
: onCompleted(response, undefined, params, store, actions, selectors)
|
|
171
|
-
return {
|
|
172
|
-
result: newState === null || newState === void 0 ? void 0 : newState.result,
|
|
173
|
-
}
|
|
157
|
+
return {result: newState === null || newState === void 0 ? void 0 : newState.result}
|
|
174
158
|
})
|
|
175
159
|
|
|
176
160
|
const catchAndReturn = (x) => x
|
package/dist/esm/useMutation.js
CHANGED
|
@@ -32,7 +32,7 @@ var __awaiter =
|
|
|
32
32
|
import {useMemo} from 'react'
|
|
33
33
|
|
|
34
34
|
import {mutate as mutateImpl} from './mutate'
|
|
35
|
-
import {EMPTY_OBJECT,
|
|
35
|
+
import {EMPTY_OBJECT, logDebug} from './utilsAndConstants'
|
|
36
36
|
|
|
37
37
|
export const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
38
38
|
var _a
|
|
@@ -74,10 +74,6 @@ export const useMutation = (cache, actions, selectors, options, abortControllers
|
|
|
74
74
|
}, [mutationKey, store])
|
|
75
75
|
const mutationState =
|
|
76
76
|
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0 ? _a : EMPTY_OBJECT
|
|
77
|
-
cache.options.logsEnabled &&
|
|
78
|
-
log('useMutation', {
|
|
79
|
-
options,
|
|
80
|
-
mutationState,
|
|
81
|
-
})
|
|
77
|
+
cache.options.logsEnabled && logDebug('useMutation', {options, mutationState})
|
|
82
78
|
return [mutate, mutationState, abort]
|
|
83
79
|
}
|
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,7 @@ 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
|
}
|
|
126
121
|
|
|
@@ -4,10 +4,18 @@ export const optionalUtils = {
|
|
|
4
4
|
deepEqual: undefined,
|
|
5
5
|
}
|
|
6
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
|
+
|
|
7
15
|
try {
|
|
8
16
|
optionalUtils.deepEqual = require('fast-deep-equal/es6')
|
|
9
17
|
} catch (_a) {
|
|
10
|
-
|
|
18
|
+
logDebug('deepEqual', 'fast-deep-equal optional dependency was not installed')
|
|
11
19
|
}
|
|
12
20
|
|
|
13
21
|
export const IS_DEV = (() => {
|
|
@@ -22,7 +30,7 @@ export const EMPTY_OBJECT = Object.freeze({})
|
|
|
22
30
|
|
|
23
31
|
export const EMPTY_ARRAY = Object.freeze([])
|
|
24
32
|
|
|
25
|
-
export const
|
|
33
|
+
export const noop = () => {}
|
|
26
34
|
|
|
27
35
|
export const defaultGetCacheKey = (params) => {
|
|
28
36
|
switch (typeof params) {
|
|
@@ -36,14 +44,10 @@ export const defaultGetCacheKey = (params) => {
|
|
|
36
44
|
}
|
|
37
45
|
}
|
|
38
46
|
|
|
39
|
-
export const log = (tag, data) => {
|
|
40
|
-
console.debug(`@${PACKAGE_SHORT_NAME} [${tag}]`, data)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
47
|
export const applyEntityChanges = (entities, changes, options) => {
|
|
44
48
|
var _a, _b, _c, _d
|
|
45
49
|
if (changes.merge && changes.entities) {
|
|
46
|
-
|
|
50
|
+
logWarn('applyEntityChanges', 'merge and entities should not be both set')
|
|
47
51
|
}
|
|
48
52
|
const {merge = changes.entities, replace, remove} = changes
|
|
49
53
|
if (!merge && !replace && !remove) {
|
|
@@ -51,13 +55,11 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
51
55
|
}
|
|
52
56
|
const deepEqual = options.deepComparisonEnabled ? optionalUtils.deepEqual : undefined
|
|
53
57
|
let result
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
])
|
|
60
|
-
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) {
|
|
61
63
|
const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
|
|
62
64
|
const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
|
|
63
65
|
const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
|
|
@@ -90,10 +92,7 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
90
92
|
? _c
|
|
91
93
|
: 0)
|
|
92
94
|
if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
|
|
93
|
-
|
|
94
|
-
'react-redux-cache.applyEntityChanges: merge, replace and remove changes have intersections for: ' +
|
|
95
|
-
typename
|
|
96
|
-
)
|
|
95
|
+
logWarn('applyEntityChanges', 'merge, replace and remove changes have intersections for: ' + typename)
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
const oldEntities = (_d = entities[typename]) !== null && _d !== void 0 ? _d : EMPTY_OBJECT
|
|
@@ -138,7 +137,7 @@ export const applyEntityChanges = (entities, changes, options) => {
|
|
|
138
137
|
result[typename] = newEntities
|
|
139
138
|
}
|
|
140
139
|
options.logsEnabled &&
|
|
141
|
-
|
|
140
|
+
logDebug('applyEntityChanges', {
|
|
142
141
|
entities,
|
|
143
142
|
changes,
|
|
144
143
|
options,
|
|
@@ -262,9 +262,17 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
262
262
|
hooks: {
|
|
263
263
|
/** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
|
|
264
264
|
useClient: () => {
|
|
265
|
+
/**
|
|
266
|
+
* Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
|
|
267
|
+
* @param onlyIfExpired When true, cancels fetch if result is not yet expired.
|
|
268
|
+
* @param skipFetch Fetch is cancelled and current cached result is returned.
|
|
269
|
+
*/
|
|
265
270
|
query: <QK extends keyof (QP & QR)>(
|
|
266
271
|
options: QueryOptions<N, T, QP, QR, QK, MP, MR>
|
|
267
272
|
) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>
|
|
273
|
+
/**
|
|
274
|
+
* Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
|
|
275
|
+
*/
|
|
268
276
|
mutate: <MK extends keyof (MP & MR)>(
|
|
269
277
|
options: MutateOptions<N, T, QP, QR, MP, MR, MK>
|
|
270
278
|
) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
|
|
@@ -325,11 +333,19 @@ export declare const withTypenames: <T extends Typenames = Typenames>() => {
|
|
|
325
333
|
useSelectEntityById: <TN extends keyof T>(id: Key | null | undefined, typename: TN) => T[TN] | undefined
|
|
326
334
|
}
|
|
327
335
|
utils: {
|
|
328
|
-
/** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
|
|
336
|
+
/** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
|
|
329
337
|
createClient: (store: Store) => {
|
|
338
|
+
/**
|
|
339
|
+
* Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
|
|
340
|
+
* @param onlyIfExpired When true, cancels fetch if result is not yet expired.
|
|
341
|
+
* @param skipFetch Fetch is cancelled and current cached result is returned.
|
|
342
|
+
*/
|
|
330
343
|
query: <QK extends keyof (QP & QR)>(
|
|
331
344
|
options: QueryOptions<N, T, QP, QR, QK, MP, MR>
|
|
332
345
|
) => Promise<QueryResult<QK extends keyof QP & keyof QR ? QR[QK] : never>>
|
|
346
|
+
/**
|
|
347
|
+
* Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
|
|
348
|
+
*/
|
|
333
349
|
mutate: <MK extends keyof (MP & MR)>(
|
|
334
350
|
options: MutateOptions<N, T, QP, QR, MP, MR, MK>
|
|
335
351
|
) => Promise<MutationResult<MK extends keyof MP & keyof MR ? MR[MK] : never>>
|
|
@@ -605,9 +621,17 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
|
605
621
|
hooks: {
|
|
606
622
|
/** Returns memoized object with query and mutate functions. Memoization dependency is the store. */
|
|
607
623
|
useClient: () => {
|
|
624
|
+
/**
|
|
625
|
+
* Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
|
|
626
|
+
* @param onlyIfExpired When true, cancels fetch if result is not yet expired.
|
|
627
|
+
* @param skipFetch Fetch is cancelled and current cached result is returned.
|
|
628
|
+
*/
|
|
608
629
|
query: <QK_1 extends keyof QP | keyof QR>(
|
|
609
630
|
options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>
|
|
610
631
|
) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
|
|
632
|
+
/**
|
|
633
|
+
* Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
|
|
634
|
+
*/
|
|
611
635
|
mutate: <MK_1 extends keyof MP | keyof MR>(
|
|
612
636
|
options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>
|
|
613
637
|
) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
|
|
@@ -668,11 +692,19 @@ export declare const createCache: <N extends string, QP, QR, MP, MR>(
|
|
|
668
692
|
useSelectEntityById: <TN extends string>(id: Key | null | undefined, typename: TN) => object | undefined
|
|
669
693
|
}
|
|
670
694
|
utils: {
|
|
671
|
-
/** Creates client by providing the store. Can be used when the store is a singleton - to not use a hook for getting the client, but import it directly. */
|
|
695
|
+
/** Creates client by providing the store. Can be used when the store is a singleton - to not use a useClient hook for getting the client, but import it directly. */
|
|
672
696
|
createClient: (store: Store) => {
|
|
697
|
+
/**
|
|
698
|
+
* Performs a query using provided options. Deduplicates calls with the same cache key. Always returns current cached result, even when query is cancelled or finished with error.
|
|
699
|
+
* @param onlyIfExpired When true, cancels fetch if result is not yet expired.
|
|
700
|
+
* @param skipFetch Fetch is cancelled and current cached result is returned.
|
|
701
|
+
*/
|
|
673
702
|
query: <QK_1 extends keyof QP | keyof QR>(
|
|
674
703
|
options: QueryOptions<N, Typenames, QP, QR, QK_1, MP, MR>
|
|
675
704
|
) => Promise<QueryResult<QK_1 extends keyof QP & keyof QR ? QR[QK_1] : never>>
|
|
705
|
+
/**
|
|
706
|
+
* Performs a mutation, aborting previous one with the same mutation key. Returns result only if finished succesfully.
|
|
707
|
+
*/
|
|
676
708
|
mutate: <MK_1 extends keyof MP | keyof MR>(
|
|
677
709
|
options: MutateOptions<N, Typenames, QP, QR, MP, MR, MK_1>
|
|
678
710
|
) => Promise<MutationResult<MK_1 extends keyof MP & keyof MR ? MR[MK_1] : never>>
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export {createCache, withTypenames} from './createCache'
|
|
|
2
2
|
|
|
3
3
|
export * from './types'
|
|
4
4
|
|
|
5
|
-
export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject} from './utilsAndConstants'
|
|
5
|
+
export {createStateComparer, defaultGetCacheKey, FetchPolicy, isEmptyObject, noop} from './utilsAndConstants'
|
package/dist/types/types.d.ts
CHANGED
|
@@ -279,6 +279,7 @@ export type QueryResponse<R = unknown> = {
|
|
|
279
279
|
export type NormalizedQueryResponse<T extends Typenames = Typenames, R = unknown> = EntityChanges<T> &
|
|
280
280
|
QueryResponse<R>
|
|
281
281
|
|
|
282
|
+
/** Current result is always returned, even if cancelled or finished with error. */
|
|
282
283
|
export type QueryResult<R = unknown> = {
|
|
283
284
|
error?: unknown
|
|
284
285
|
/** Fetch cancelled reason. */
|
|
@@ -14,19 +14,22 @@ export declare const optionalUtils: {
|
|
|
14
14
|
deepEqual?: (a: any, b: any) => boolean
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
export declare const logDebug: (tag: string, data?: unknown) => void
|
|
18
|
+
|
|
19
|
+
export declare const logWarn: (tag: string, data?: unknown) => void
|
|
20
|
+
|
|
17
21
|
export declare const IS_DEV: boolean
|
|
18
22
|
|
|
19
23
|
export declare const EMPTY_OBJECT: Readonly<{}>
|
|
20
24
|
|
|
21
25
|
export declare const EMPTY_ARRAY: readonly never[]
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
/** Empty function. */
|
|
28
|
+
export declare const noop: () => void
|
|
24
29
|
|
|
25
30
|
/** Default getCacheKey implementation. */
|
|
26
31
|
export declare const defaultGetCacheKey: <P = unknown>(params: P) => Key
|
|
27
32
|
|
|
28
|
-
export declare const log: (tag: string, data?: unknown) => void
|
|
29
|
-
|
|
30
33
|
export declare const applyEntityChanges: <T extends Typenames>(
|
|
31
34
|
entities: EntitiesMap<T>,
|
|
32
35
|
changes: EntityChanges<T>,
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "react-redux-cache",
|
|
3
3
|
"author": "Alexander Danilov",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.20.0",
|
|
6
6
|
"description": "Powerful data fetching and caching library for Redux and Zustand that supports normalization.",
|
|
7
7
|
"main": "./dist/cjs/index.js",
|
|
8
8
|
"module": "./dist/esm/index.js",
|