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/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.
|
|
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
|
|--|--|
|
|
@@ -127,6 +127,8 @@ Can be considered as `ApolloClient` for protocols other than `GraphQL`, but with
|
|
|
127
127
|
}
|
|
128
128
|
```
|
|
129
129
|
</details>
|
|
130
|
+
|
|
131
|
+
[API Reference](DOCUMENTATION.md)
|
|
130
132
|
|
|
131
133
|
### Table of contents
|
|
132
134
|
|
package/dist/cjs/createCache.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, '__esModule', {value: true})
|
|
|
3
3
|
exports.createCache = exports.withTypenames = void 0
|
|
4
4
|
const react_1 = require('react')
|
|
5
5
|
const createActions_1 = require('./createActions')
|
|
6
|
-
const
|
|
6
|
+
const createReducer_1 = require('./createReducer')
|
|
7
7
|
const createSelectors_1 = require('./createSelectors')
|
|
8
8
|
const mutate_1 = require('./mutate')
|
|
9
9
|
const query_1 = require('./query')
|
|
@@ -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) => {
|
|
@@ -100,11 +101,7 @@ const withTypenames = () => {
|
|
|
100
101
|
clearMutationState,
|
|
101
102
|
clearCache,
|
|
102
103
|
} = actions
|
|
103
|
-
const reducer = (0,
|
|
104
|
-
actions,
|
|
105
|
-
Object.keys(cache.queries),
|
|
106
|
-
cache.options
|
|
107
|
-
)
|
|
104
|
+
const reducer = (0, createReducer_1.createReducer)(actions, Object.keys(cache.queries), cache.options)
|
|
108
105
|
const createClient = (store) => {
|
|
109
106
|
const client = {
|
|
110
107
|
query: (options) => {
|
|
@@ -18,12 +18,12 @@ var __rest =
|
|
|
18
18
|
return t
|
|
19
19
|
}
|
|
20
20
|
Object.defineProperty(exports, '__esModule', {value: true})
|
|
21
|
-
exports.
|
|
21
|
+
exports.createReducer = void 0
|
|
22
22
|
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
23
23
|
|
|
24
24
|
const optionalQueryKeys = ['error', 'expiresAt', 'result', 'params', 'loading']
|
|
25
25
|
const optionalMutationKeys = ['error', 'result', 'params', 'loading']
|
|
26
|
-
const
|
|
26
|
+
const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
27
27
|
const initialState = Object.freeze({
|
|
28
28
|
entities: Object.freeze({}),
|
|
29
29
|
queries: Object.freeze(
|
|
@@ -35,7 +35,7 @@ const createCacheReducer = (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
|
})
|
|
@@ -282,4 +282,4 @@ const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
282
282
|
return state
|
|
283
283
|
}
|
|
284
284
|
}
|
|
285
|
-
exports.
|
|
285
|
+
exports.createReducer = createReducer
|
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
|
@@ -38,18 +38,13 @@ const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
|
38
38
|
|
|
39
39
|
const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
40
40
|
var _a
|
|
41
|
+
const {selectMutationState} = selectors
|
|
42
|
+
const {updateMutationStateAndEntities} = actions
|
|
41
43
|
const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
|
|
42
44
|
const store = cache.storeHooks.useStore()
|
|
43
45
|
const [mutationStateSelector, mutate, abort] = (0, react_1.useMemo)(() => {
|
|
44
46
|
return [
|
|
45
|
-
(state) =>
|
|
46
|
-
cache.options.logsEnabled &&
|
|
47
|
-
(0, utilsAndConstants_1.log)('mutationStateSelector', {
|
|
48
|
-
state,
|
|
49
|
-
cacheState: cache.cacheStateSelector(state),
|
|
50
|
-
})
|
|
51
|
-
return cache.cacheStateSelector(state).mutations[mutationKey]
|
|
52
|
-
},
|
|
47
|
+
(state) => selectMutationState(state, mutationKey),
|
|
53
48
|
(params) =>
|
|
54
49
|
__awaiter(void 0, void 0, void 0, function* () {
|
|
55
50
|
return yield (0,
|
|
@@ -63,7 +58,7 @@ const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
|
63
58
|
return false
|
|
64
59
|
}
|
|
65
60
|
abortController.abort()
|
|
66
|
-
store.dispatch(
|
|
61
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, {loading: undefined}))
|
|
67
62
|
return true
|
|
68
63
|
},
|
|
69
64
|
]
|
|
@@ -72,11 +67,7 @@ const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
|
72
67
|
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0
|
|
73
68
|
? _a
|
|
74
69
|
: utilsAndConstants_1.EMPTY_OBJECT
|
|
75
|
-
cache.options.logsEnabled &&
|
|
76
|
-
(0, utilsAndConstants_1.log)('useMutation', {
|
|
77
|
-
options,
|
|
78
|
-
mutationState,
|
|
79
|
-
})
|
|
70
|
+
cache.options.logsEnabled && (0, utilsAndConstants_1.logDebug)('useMutation', {options, mutationState})
|
|
80
71
|
return [mutate, mutationState, abort]
|
|
81
72
|
}
|
|
82
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
|
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
Object.defineProperty(exports, '__esModule', {value: true})
|
|
3
|
-
exports.
|
|
3
|
+
exports.FetchPolicy =
|
|
4
|
+
exports.createStateComparer =
|
|
4
5
|
exports.isEmptyObject =
|
|
5
6
|
exports.applyEntityChanges =
|
|
6
|
-
exports.FetchPolicy =
|
|
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,20 +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
|
-
exports.FetchPolicy = {
|
|
53
|
-
NoCacheOrExpired: (expired, _params, state) => {
|
|
54
|
-
return expired || state.result === undefined
|
|
55
|
-
},
|
|
56
|
-
Always: () => true,
|
|
57
|
-
}
|
|
58
57
|
const applyEntityChanges = (entities, changes, options) => {
|
|
59
58
|
var _a, _b, _c, _d
|
|
60
59
|
if (changes.merge && changes.entities) {
|
|
61
|
-
|
|
60
|
+
;(0, exports.logWarn)('applyEntityChanges', 'merge and entities should not be both set')
|
|
62
61
|
}
|
|
63
62
|
const {merge = changes.entities, replace, remove} = changes
|
|
64
63
|
if (!merge && !replace && !remove) {
|
|
@@ -66,13 +65,11 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
66
65
|
}
|
|
67
66
|
const deepEqual = options.deepComparisonEnabled ? exports.optionalUtils.deepEqual : undefined
|
|
68
67
|
let result
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
])
|
|
75
|
-
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) {
|
|
76
73
|
const entitiesToMerge = merge === null || merge === void 0 ? void 0 : merge[typename]
|
|
77
74
|
const entitiesToReplace = replace === null || replace === void 0 ? void 0 : replace[typename]
|
|
78
75
|
const entitiesToRemove = remove === null || remove === void 0 ? void 0 : remove[typename]
|
|
@@ -105,9 +102,9 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
105
102
|
? _c
|
|
106
103
|
: 0)
|
|
107
104
|
if (totalKeysInResponse !== 0 && idsSet.size !== totalKeysInResponse) {
|
|
108
|
-
|
|
109
|
-
'
|
|
110
|
-
|
|
105
|
+
;(0, exports.logWarn)(
|
|
106
|
+
'applyEntityChanges',
|
|
107
|
+
'merge, replace and remove changes have intersections for: ' + typename
|
|
111
108
|
)
|
|
112
109
|
}
|
|
113
110
|
}
|
|
@@ -153,7 +150,7 @@ const applyEntityChanges = (entities, changes, options) => {
|
|
|
153
150
|
result[typename] = newEntities
|
|
154
151
|
}
|
|
155
152
|
options.logsEnabled &&
|
|
156
|
-
(0, exports.
|
|
153
|
+
(0, exports.logDebug)('applyEntityChanges', {
|
|
157
154
|
entities,
|
|
158
155
|
changes,
|
|
159
156
|
options,
|
|
@@ -187,3 +184,9 @@ const createStateComparer = (fields) => {
|
|
|
187
184
|
}
|
|
188
185
|
}
|
|
189
186
|
exports.createStateComparer = createStateComparer
|
|
187
|
+
exports.FetchPolicy = {
|
|
188
|
+
NoCacheOrExpired: (expired, _params, state) => {
|
|
189
|
+
return expired || state.result === undefined
|
|
190
|
+
},
|
|
191
|
+
Always: () => true,
|
|
192
|
+
}
|
package/dist/esm/createCache.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {useMemo} from 'react'
|
|
2
2
|
|
|
3
3
|
import {createActions} from './createActions'
|
|
4
|
-
import {
|
|
4
|
+
import {createReducer} from './createReducer'
|
|
5
5
|
import {createSelectors} from './createSelectors'
|
|
6
6
|
import {mutate as mutateImpl} from './mutate'
|
|
7
7
|
import {query as queryImpl} from './query'
|
|
@@ -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) => {
|
|
@@ -103,7 +105,7 @@ export const withTypenames = () => {
|
|
|
103
105
|
clearMutationState,
|
|
104
106
|
clearCache,
|
|
105
107
|
} = actions
|
|
106
|
-
const reducer =
|
|
108
|
+
const reducer = createReducer(actions, Object.keys(cache.queries), cache.options)
|
|
107
109
|
const createClient = (store) => {
|
|
108
110
|
const client = {
|
|
109
111
|
query: (options) => {
|
|
@@ -201,4 +203,5 @@ export const withTypenames = () => {
|
|
|
201
203
|
},
|
|
202
204
|
}
|
|
203
205
|
}
|
|
206
|
+
|
|
204
207
|
export const createCache = withTypenames().createCache
|
|
@@ -16,11 +16,12 @@ 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']
|
|
23
|
-
|
|
23
|
+
|
|
24
|
+
export const createReducer = (actions, queryKeys, cacheOptions) => {
|
|
24
25
|
const initialState = Object.freeze({
|
|
25
26
|
entities: Object.freeze({}),
|
|
26
27
|
queries: Object.freeze(
|
|
@@ -32,7 +33,7 @@ export const createCacheReducer = (actions, queryKeys, cacheOptions) => {
|
|
|
32
33
|
mutations: Object.freeze({}),
|
|
33
34
|
})
|
|
34
35
|
cacheOptions.logsEnabled &&
|
|
35
|
-
|
|
36
|
+
logDebug('createCacheReducer', {
|
|
36
37
|
queryKeys,
|
|
37
38
|
initialState,
|
|
38
39
|
})
|
package/dist/esm/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export {createCache, withTypenames} from './createCache'
|
|
2
|
+
|
|
2
3
|
export * from './types'
|
|
3
|
-
|
|
4
|
+
|
|
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
|
}
|
|
@@ -154,4 +146,5 @@ export const mutate = (
|
|
|
154
146
|
throw new Error(`${logTag}: both error and response are not defined`)
|
|
155
147
|
}
|
|
156
148
|
)
|
|
149
|
+
|
|
157
150
|
const ABORTED_RESULT = Object.freeze({aborted: true})
|
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,8 +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
|
})
|
|
159
|
+
|
|
175
160
|
const catchAndReturn = (x) => x
|
package/dist/esm/useMutation.js
CHANGED
|
@@ -32,22 +32,17 @@ 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
|
|
39
|
+
const {selectMutationState} = selectors
|
|
40
|
+
const {updateMutationStateAndEntities} = actions
|
|
39
41
|
const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
|
|
40
42
|
const store = cache.storeHooks.useStore()
|
|
41
43
|
const [mutationStateSelector, mutate, abort] = useMemo(() => {
|
|
42
44
|
return [
|
|
43
|
-
(state) =>
|
|
44
|
-
cache.options.logsEnabled &&
|
|
45
|
-
log('mutationStateSelector', {
|
|
46
|
-
state,
|
|
47
|
-
cacheState: cache.cacheStateSelector(state),
|
|
48
|
-
})
|
|
49
|
-
return cache.cacheStateSelector(state).mutations[mutationKey]
|
|
50
|
-
},
|
|
45
|
+
(state) => selectMutationState(state, mutationKey),
|
|
51
46
|
(params) =>
|
|
52
47
|
__awaiter(void 0, void 0, void 0, function* () {
|
|
53
48
|
return yield mutateImpl(
|
|
@@ -72,17 +67,13 @@ export const useMutation = (cache, actions, selectors, options, abortControllers
|
|
|
72
67
|
return false
|
|
73
68
|
}
|
|
74
69
|
abortController.abort()
|
|
75
|
-
store.dispatch(
|
|
70
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, {loading: undefined}))
|
|
76
71
|
return true
|
|
77
72
|
},
|
|
78
73
|
]
|
|
79
74
|
}, [mutationKey, store])
|
|
80
75
|
const mutationState =
|
|
81
76
|
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0 ? _a : EMPTY_OBJECT
|
|
82
|
-
cache.options.logsEnabled &&
|
|
83
|
-
log('useMutation', {
|
|
84
|
-
options,
|
|
85
|
-
mutationState,
|
|
86
|
-
})
|
|
77
|
+
cache.options.logsEnabled && logDebug('useMutation', {options, mutationState})
|
|
87
78
|
return [mutate, mutationState, abort]
|
|
88
79
|
}
|