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