react-redux-cache 0.22.1 → 0.22.2
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 +2 -2
- package/dist/cjs/createActions.js +63 -63
- package/dist/cjs/createCache.js +216 -131
- package/dist/cjs/createReducer.js +355 -290
- package/dist/cjs/createSelectors.js +66 -58
- package/dist/cjs/index.js +87 -26
- package/dist/cjs/mutate.js +153 -70
- package/dist/cjs/query.js +161 -70
- package/dist/cjs/types.js +2 -2
- package/dist/cjs/useMutation.js +81 -42
- package/dist/cjs/useQuery.js +127 -56
- package/dist/cjs/utilsAndConstants.js +204 -151
- package/dist/esm/createActions.js +60 -59
- package/dist/esm/createCache.js +218 -127
- package/dist/esm/createReducer.js +349 -286
- package/dist/esm/createSelectors.js +63 -54
- package/dist/esm/index.js +3 -3
- package/dist/esm/mutate.js +143 -66
- package/dist/esm/query.js +149 -66
- package/dist/esm/types.js +1 -1
- package/dist/esm/useMutation.js +77 -38
- package/dist/esm/useQuery.js +119 -52
- package/dist/esm/utilsAndConstants.js +190 -140
- package/dist/types/createActions.d.ts +109 -83
- package/dist/types/createCache.d.ts +734 -409
- package/dist/types/createReducer.d.ts +11 -3
- package/dist/types/createSelectors.d.ts +80 -18
- package/dist/types/index.d.ts +3 -3
- package/dist/types/mutate.d.ts +94 -4
- package/dist/types/query.d.ts +123 -4
- package/dist/types/types.d.ts +360 -184
- package/dist/types/useMutation.d.ts +39 -4
- package/dist/types/useQuery.d.ts +40 -4
- package/dist/types/utilsAndConstants.d.ts +56 -23
- package/package.json +4 -5
package/dist/cjs/query.js
CHANGED
|
@@ -1,81 +1,172 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter =
|
|
3
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
4
12
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.query = void 0
|
|
35
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
36
|
+
const query = (
|
|
37
|
+
logTag,
|
|
38
|
+
store,
|
|
39
|
+
cache,
|
|
40
|
+
actions,
|
|
41
|
+
selectors,
|
|
42
|
+
queryKey,
|
|
43
|
+
cacheKey,
|
|
44
|
+
params,
|
|
45
|
+
secondsToLive,
|
|
46
|
+
onlyIfExpired,
|
|
47
|
+
skipFetch,
|
|
48
|
+
mergeResults,
|
|
49
|
+
onCompleted,
|
|
50
|
+
onSuccess,
|
|
51
|
+
onError,
|
|
52
|
+
) =>
|
|
53
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
54
|
+
var _a, _b, _c, _d
|
|
55
|
+
if (secondsToLive === void 0) {
|
|
56
|
+
secondsToLive =
|
|
57
|
+
(_a = cache.queries[queryKey].secondsToLive) !== null && _a !== void 0
|
|
58
|
+
? _a
|
|
59
|
+
: cache.globals.queries.secondsToLive
|
|
60
|
+
}
|
|
61
|
+
if (mergeResults === void 0) {
|
|
62
|
+
mergeResults = cache.queries[queryKey].mergeResults
|
|
63
|
+
}
|
|
64
|
+
if (onCompleted === void 0) {
|
|
65
|
+
onCompleted = cache.queries[queryKey].onCompleted
|
|
66
|
+
}
|
|
67
|
+
if (onSuccess === void 0) {
|
|
68
|
+
onSuccess = cache.queries[queryKey].onSuccess
|
|
69
|
+
}
|
|
70
|
+
if (onError === void 0) {
|
|
71
|
+
onError = cache.queries[queryKey].onError
|
|
72
|
+
}
|
|
73
|
+
const {selectQueryResult, selectQueryState} = selectors
|
|
74
|
+
const logsEnabled = cache.options.logsEnabled
|
|
75
|
+
const queryStateOnStart = selectQueryState(store.getState(), queryKey, cacheKey)
|
|
24
76
|
if (skipFetch) {
|
|
25
|
-
|
|
77
|
+
return {result: queryStateOnStart.result}
|
|
26
78
|
}
|
|
27
79
|
if (queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.loading) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
80
|
+
logsEnabled &&
|
|
81
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} fetch cancelled: already loading`, {
|
|
82
|
+
queryStateOnStart,
|
|
83
|
+
params,
|
|
84
|
+
cacheKey,
|
|
85
|
+
})
|
|
86
|
+
const error = yield queryStateOnStart.loading.then(utilsAndConstants_1.noop).catch(catchAndReturn)
|
|
87
|
+
const result = selectQueryResult(store.getState(), queryKey, cacheKey)
|
|
88
|
+
const cancelled = 'loading'
|
|
89
|
+
return error ? {cancelled, result, error} : {cancelled, result}
|
|
34
90
|
}
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
91
|
+
if (
|
|
92
|
+
onlyIfExpired &&
|
|
93
|
+
(queryStateOnStart === null || queryStateOnStart === void 0 ? void 0 : queryStateOnStart.expiresAt) !=
|
|
94
|
+
null &&
|
|
95
|
+
queryStateOnStart.expiresAt > Date.now()
|
|
96
|
+
) {
|
|
97
|
+
logsEnabled &&
|
|
98
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} fetch cancelled: not expired yet`, {
|
|
99
|
+
queryStateOnStart,
|
|
100
|
+
params,
|
|
101
|
+
cacheKey,
|
|
102
|
+
onlyIfExpired,
|
|
103
|
+
})
|
|
104
|
+
return {cancelled: 'not-expired', result: queryStateOnStart.result}
|
|
44
105
|
}
|
|
45
|
-
const {
|
|
46
|
-
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
47
|
-
store.dispatch(
|
|
106
|
+
const {updateQueryStateAndEntities} = actions
|
|
107
|
+
const fetchPromise = cache.queries[queryKey].query(params, store)
|
|
108
|
+
store.dispatch(
|
|
109
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
48
110
|
loading: fetchPromise,
|
|
49
111
|
params,
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
112
|
+
}),
|
|
113
|
+
)
|
|
114
|
+
logsEnabled &&
|
|
115
|
+
(0, utilsAndConstants_1.logDebug)(`${logTag} started`, {
|
|
116
|
+
queryKey,
|
|
117
|
+
params,
|
|
118
|
+
cacheKey,
|
|
119
|
+
queryStateOnStart,
|
|
120
|
+
onlyIfExpired,
|
|
121
|
+
})
|
|
122
|
+
let response
|
|
53
123
|
try {
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
})
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
124
|
+
response = yield fetchPromise
|
|
125
|
+
} catch (error) {
|
|
126
|
+
store.dispatch(
|
|
127
|
+
updateQueryStateAndEntities(queryKey, cacheKey, {
|
|
128
|
+
error,
|
|
129
|
+
loading: undefined,
|
|
130
|
+
}),
|
|
131
|
+
)
|
|
132
|
+
if (!(onError === null || onError === void 0 ? void 0 : onError(error, params, store))) {
|
|
133
|
+
;(_c = (_b = cache.globals).onError) === null || _c === void 0
|
|
134
|
+
? void 0
|
|
135
|
+
: _c.call(_b, error, queryKey, params, store, actions, selectors)
|
|
136
|
+
}
|
|
137
|
+
onCompleted === null || onCompleted === void 0
|
|
138
|
+
? void 0
|
|
139
|
+
: onCompleted(undefined, error, params, store, actions, selectors)
|
|
140
|
+
return {error, result: selectQueryResult(store.getState(), queryKey, cacheKey)}
|
|
66
141
|
}
|
|
67
142
|
const newState = {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
143
|
+
error: undefined,
|
|
144
|
+
loading: undefined,
|
|
145
|
+
expiresAt:
|
|
146
|
+
(_d = response.expiresAt) !== null && _d !== void 0
|
|
147
|
+
? _d
|
|
148
|
+
: secondsToLive != null
|
|
149
|
+
? Date.now() + secondsToLive * 1000
|
|
150
|
+
: undefined,
|
|
151
|
+
result: mergeResults
|
|
152
|
+
? mergeResults(
|
|
153
|
+
selectQueryResult(store.getState(), queryKey, cacheKey),
|
|
154
|
+
response,
|
|
155
|
+
params,
|
|
156
|
+
store,
|
|
157
|
+
actions,
|
|
158
|
+
selectors,
|
|
159
|
+
)
|
|
160
|
+
: response.result,
|
|
161
|
+
}
|
|
162
|
+
store.dispatch(updateQueryStateAndEntities(queryKey, cacheKey, newState, response))
|
|
163
|
+
onSuccess === null || onSuccess === void 0
|
|
164
|
+
? void 0
|
|
165
|
+
: onSuccess(response, params, store, actions, selectors)
|
|
166
|
+
onCompleted === null || onCompleted === void 0
|
|
167
|
+
? void 0
|
|
168
|
+
: onCompleted(response, undefined, params, store, actions, selectors)
|
|
169
|
+
return {result: newState === null || newState === void 0 ? void 0 : newState.result}
|
|
170
|
+
})
|
|
171
|
+
exports.query = query
|
|
172
|
+
const catchAndReturn = (x) => x
|
package/dist/cjs/types.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
package/dist/cjs/useMutation.js
CHANGED
|
@@ -1,44 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter =
|
|
3
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
4
12
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.useMutation = void 0
|
|
35
|
+
const react_1 = require('react')
|
|
36
|
+
const mutate_1 = require('./mutate')
|
|
37
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
16
38
|
const useMutation = (cache, actions, selectors, options, abortControllers) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
var _a
|
|
40
|
+
const {selectMutationState} = selectors
|
|
41
|
+
const {updateMutationStateAndEntities} = actions
|
|
42
|
+
const {mutation: mutationKey, onCompleted, onSuccess, onError} = options
|
|
43
|
+
const store = cache.storeHooks.useStore()
|
|
44
|
+
const [mutationStateSelector, mutate, abort] = (0, react_1.useMemo)(() => {
|
|
45
|
+
return [
|
|
46
|
+
(state) => selectMutationState(state, mutationKey),
|
|
47
|
+
(params) =>
|
|
48
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
49
|
+
return yield (0, mutate_1.mutate)(
|
|
50
|
+
'useMutation.mutate',
|
|
51
|
+
store,
|
|
52
|
+
cache,
|
|
53
|
+
actions,
|
|
54
|
+
selectors,
|
|
55
|
+
mutationKey,
|
|
56
|
+
params,
|
|
57
|
+
abortControllers,
|
|
58
|
+
onCompleted,
|
|
59
|
+
onSuccess,
|
|
60
|
+
onError,
|
|
61
|
+
)
|
|
62
|
+
}),
|
|
63
|
+
() => {
|
|
64
|
+
var _a
|
|
65
|
+
const abortController =
|
|
66
|
+
(_a = abortControllers.get(store)) === null || _a === void 0 ? void 0 : _a[mutationKey]
|
|
67
|
+
if (abortController === undefined || abortController.signal.aborted) {
|
|
68
|
+
return false
|
|
69
|
+
}
|
|
70
|
+
abortController.abort()
|
|
71
|
+
store.dispatch(updateMutationStateAndEntities(mutationKey, {loading: undefined}))
|
|
72
|
+
return true
|
|
73
|
+
},
|
|
74
|
+
]
|
|
75
|
+
}, [mutationKey, store])
|
|
76
|
+
const mutationState =
|
|
77
|
+
(_a = cache.storeHooks.useSelector(mutationStateSelector)) !== null && _a !== void 0
|
|
78
|
+
? _a
|
|
79
|
+
: utilsAndConstants_1.EMPTY_OBJECT
|
|
80
|
+
cache.options.logsEnabled && (0, utilsAndConstants_1.logDebug)('useMutation', {options, mutationState})
|
|
81
|
+
return [mutate, mutationState, abort]
|
|
82
|
+
}
|
|
83
|
+
exports.useMutation = useMutation
|
package/dist/cjs/useQuery.js
CHANGED
|
@@ -1,59 +1,130 @@
|
|
|
1
|
-
|
|
2
|
-
var __awaiter =
|
|
3
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
var __awaiter =
|
|
3
|
+
(this && this.__awaiter) ||
|
|
4
|
+
function (thisArg, _arguments, P, generator) {
|
|
5
|
+
function adopt(value) {
|
|
6
|
+
return value instanceof P
|
|
7
|
+
? value
|
|
8
|
+
: new P(function (resolve) {
|
|
9
|
+
resolve(value)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
4
12
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.useQuery = void 0;
|
|
13
|
-
const react_1 = require("react");
|
|
14
|
-
const query_1 = require("./query");
|
|
15
|
-
const utilsAndConstants_1 = require("./utilsAndConstants");
|
|
16
|
-
const useQuery = (cache, actions, selectors, options) => {
|
|
17
|
-
var _a, _b, _c, _d, _e;
|
|
18
|
-
const { query: queryKey, skipFetch = false, params, secondsToLive, selectorComparer, fetchPolicy = (_a = cache.queries[queryKey].fetchPolicy) !== null && _a !== void 0 ? _a : cache.globals.queries.fetchPolicy, mergeResults, onCompleted, onSuccess, onError, } = options;
|
|
19
|
-
const { selectQueryState } = selectors;
|
|
20
|
-
const queryInfo = cache.queries[queryKey];
|
|
21
|
-
const logsEnabled = cache.options.logsEnabled;
|
|
22
|
-
const getCacheKey = (_b = queryInfo.getCacheKey) !== null && _b !== void 0 ? _b : (utilsAndConstants_1.defaultGetCacheKey);
|
|
23
|
-
const comparer = selectorComparer === undefined
|
|
24
|
-
? ((_d = (_c = queryInfo.selectorComparer) !== null && _c !== void 0 ? _c : cache.globals.queries.selectorComparer) !== null && _d !== void 0 ? _d : defaultStateComparer)
|
|
25
|
-
: typeof selectorComparer === 'function'
|
|
26
|
-
? selectorComparer
|
|
27
|
-
: (0, utilsAndConstants_1.createStateComparer)(selectorComparer);
|
|
28
|
-
const store = cache.storeHooks.useStore();
|
|
29
|
-
const cacheKey = getCacheKey(params);
|
|
30
|
-
const performFetch = (0, react_1.useCallback)((options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
-
const paramsPassed = options && 'params' in options;
|
|
32
|
-
return yield (0, query_1.query)('useQuery.fetch', store, cache, actions, selectors, queryKey, paramsPassed ? getCacheKey(options.params) : cacheKey, paramsPassed ? options.params : params, secondsToLive, options === null || options === void 0 ? void 0 : options.onlyIfExpired, false, mergeResults, onCompleted, onSuccess, onError);
|
|
33
|
-
}), [store, queryKey, cacheKey]);
|
|
34
|
-
const queryState = (_e = cache.storeHooks.useSelector((state) => {
|
|
35
|
-
return selectQueryState(state, queryKey, cacheKey);
|
|
36
|
-
}, comparer)) !== null && _e !== void 0 ? _e : utilsAndConstants_1.EMPTY_OBJECT;
|
|
37
|
-
(0, react_1.useEffect)(() => {
|
|
38
|
-
if (skipFetch) {
|
|
39
|
-
logsEnabled && (0, utilsAndConstants_1.logDebug)('useQuery.useEffect skip fetch', { skipFetch, queryKey, cacheKey });
|
|
40
|
-
return;
|
|
13
|
+
function fulfilled(value) {
|
|
14
|
+
try {
|
|
15
|
+
step(generator.next(value))
|
|
16
|
+
} catch (e) {
|
|
17
|
+
reject(e)
|
|
41
18
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
queryKey,
|
|
49
|
-
cacheKey,
|
|
50
|
-
});
|
|
51
|
-
return;
|
|
19
|
+
}
|
|
20
|
+
function rejected(value) {
|
|
21
|
+
try {
|
|
22
|
+
step(generator['throw'](value))
|
|
23
|
+
} catch (e) {
|
|
24
|
+
reject(e)
|
|
52
25
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
26
|
+
}
|
|
27
|
+
function step(result) {
|
|
28
|
+
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected)
|
|
29
|
+
}
|
|
30
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next())
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
Object.defineProperty(exports, '__esModule', {value: true})
|
|
34
|
+
exports.useQuery = void 0
|
|
35
|
+
const react_1 = require('react')
|
|
36
|
+
const query_1 = require('./query')
|
|
37
|
+
const utilsAndConstants_1 = require('./utilsAndConstants')
|
|
38
|
+
const useQuery = (cache, actions, selectors, options) => {
|
|
39
|
+
var _a, _b, _c, _d, _e
|
|
40
|
+
const {
|
|
41
|
+
query: queryKey,
|
|
42
|
+
skipFetch = false,
|
|
43
|
+
params,
|
|
44
|
+
secondsToLive,
|
|
45
|
+
selectorComparer,
|
|
46
|
+
fetchPolicy = (_a = cache.queries[queryKey].fetchPolicy) !== null && _a !== void 0
|
|
47
|
+
? _a
|
|
48
|
+
: cache.globals.queries.fetchPolicy,
|
|
49
|
+
mergeResults,
|
|
50
|
+
onCompleted,
|
|
51
|
+
onSuccess,
|
|
52
|
+
onError,
|
|
53
|
+
} = options
|
|
54
|
+
const {selectQueryState} = selectors
|
|
55
|
+
const queryInfo = cache.queries[queryKey]
|
|
56
|
+
const logsEnabled = cache.options.logsEnabled
|
|
57
|
+
const getCacheKey =
|
|
58
|
+
(_b = queryInfo.getCacheKey) !== null && _b !== void 0 ? _b : utilsAndConstants_1.defaultGetCacheKey
|
|
59
|
+
const comparer =
|
|
60
|
+
selectorComparer === undefined
|
|
61
|
+
? (_d =
|
|
62
|
+
(_c = queryInfo.selectorComparer) !== null && _c !== void 0
|
|
63
|
+
? _c
|
|
64
|
+
: cache.globals.queries.selectorComparer) !== null && _d !== void 0
|
|
65
|
+
? _d
|
|
66
|
+
: defaultStateComparer
|
|
67
|
+
: typeof selectorComparer === 'function'
|
|
68
|
+
? selectorComparer
|
|
69
|
+
: (0, utilsAndConstants_1.createStateComparer)(selectorComparer)
|
|
70
|
+
const store = cache.storeHooks.useStore()
|
|
71
|
+
const cacheKey = getCacheKey(params)
|
|
72
|
+
const performFetch = (0, react_1.useCallback)(
|
|
73
|
+
(options) =>
|
|
74
|
+
__awaiter(void 0, void 0, void 0, function* () {
|
|
75
|
+
const paramsPassed = options && 'params' in options
|
|
76
|
+
return yield (0, query_1.query)(
|
|
77
|
+
'useQuery.fetch',
|
|
78
|
+
store,
|
|
79
|
+
cache,
|
|
80
|
+
actions,
|
|
81
|
+
selectors,
|
|
82
|
+
queryKey,
|
|
83
|
+
paramsPassed ? getCacheKey(options.params) : cacheKey,
|
|
84
|
+
paramsPassed ? options.params : params,
|
|
85
|
+
secondsToLive,
|
|
86
|
+
options === null || options === void 0 ? void 0 : options.onlyIfExpired,
|
|
87
|
+
false,
|
|
88
|
+
mergeResults,
|
|
89
|
+
onCompleted,
|
|
90
|
+
onSuccess,
|
|
91
|
+
onError,
|
|
92
|
+
)
|
|
93
|
+
}),
|
|
94
|
+
[store, queryKey, cacheKey],
|
|
95
|
+
)
|
|
96
|
+
const queryState =
|
|
97
|
+
(_e = cache.storeHooks.useSelector((state) => {
|
|
98
|
+
return selectQueryState(state, queryKey, cacheKey)
|
|
99
|
+
}, comparer)) !== null && _e !== void 0
|
|
100
|
+
? _e
|
|
101
|
+
: utilsAndConstants_1.EMPTY_OBJECT
|
|
102
|
+
;(0, react_1.useEffect)(() => {
|
|
103
|
+
if (skipFetch) {
|
|
104
|
+
logsEnabled &&
|
|
105
|
+
(0, utilsAndConstants_1.logDebug)('useQuery.useEffect skip fetch', {skipFetch, queryKey, cacheKey})
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
const expired = queryState.expiresAt != null && queryState.expiresAt <= Date.now()
|
|
109
|
+
if (!fetchPolicy(expired, params, queryState, store, selectors)) {
|
|
110
|
+
logsEnabled &&
|
|
111
|
+
(0, utilsAndConstants_1.logDebug)('useQuery.useEffect skip fetch due to fetch policy', {
|
|
112
|
+
queryState,
|
|
113
|
+
expired,
|
|
114
|
+
queryKey,
|
|
115
|
+
cacheKey,
|
|
116
|
+
})
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
performFetch()
|
|
120
|
+
}, [cacheKey, skipFetch])
|
|
121
|
+
logsEnabled && (0, utilsAndConstants_1.logDebug)('useQuery', {cacheKey, options, queryState})
|
|
122
|
+
return [queryState, performFetch]
|
|
123
|
+
}
|
|
124
|
+
exports.useQuery = useQuery
|
|
125
|
+
const defaultStateComparer = (0, utilsAndConstants_1.createStateComparer)([
|
|
126
|
+
'result',
|
|
127
|
+
'loading',
|
|
128
|
+
'params',
|
|
129
|
+
'error',
|
|
130
|
+
])
|