| 1 |
var __defProp = Object.defineProperty; |
| 2 |
var __defProps = Object.defineProperties; |
| 3 |
var __getOwnPropDescs = Object.getOwnPropertyDescriptors; |
| 4 |
var __getOwnPropSymbols = Object.getOwnPropertySymbols; |
| 5 |
var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 6 |
var __propIsEnum = Object.prototype.propertyIsEnumerable; |
| 7 |
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; |
| 8 |
var __spreadValues = (a, b) => { |
| 9 |
for (var prop in b || (b = {})) |
| 10 |
if (__hasOwnProp.call(b, prop)) |
| 11 |
__defNormalProp(a, prop, b[prop]); |
| 12 |
if (__getOwnPropSymbols) |
| 13 |
for (var prop of __getOwnPropSymbols(b)) { |
| 14 |
if (__propIsEnum.call(b, prop)) |
| 15 |
__defNormalProp(a, prop, b[prop]); |
| 16 |
} |
| 17 |
return a; |
| 18 |
}; |
| 19 |
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); |
| 20 |
var __objRest = (source, exclude) => { |
| 21 |
var target = {}; |
| 22 |
for (var prop in source) |
| 23 |
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) |
| 24 |
target[prop] = source[prop]; |
| 25 |
if (source != null && __getOwnPropSymbols) |
| 26 |
for (var prop of __getOwnPropSymbols(source)) { |
| 27 |
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) |
| 28 |
target[prop] = source[prop]; |
| 29 |
} |
| 30 |
return target; |
| 31 |
}; |
| 32 |
|
| 33 |
// src/query/react/index.ts |
| 34 |
import { buildCreateApi, coreModule } from "@reduxjs/toolkit/query"; |
| 35 |
|
| 36 |
// src/query/react/module.ts |
| 37 |
import { formatProdErrorMessage as _formatProdErrorMessage3 } from "@reduxjs/toolkit"; |
| 38 |
import { batch as rrBatch, useDispatch as rrUseDispatch, useSelector as rrUseSelector, useStore as rrUseStore } from "react-redux"; |
| 39 |
import { createSelector as _createSelector } from "reselect"; |
| 40 |
|
| 41 |
// src/query/utils/capitalize.ts |
| 42 |
function capitalize(str) { |
| 43 |
return str.replace(str[0], str[0].toUpperCase()); |
| 44 |
} |
| 45 |
|
| 46 |
// src/query/utils/countObjectKeys.ts |
| 47 |
function countObjectKeys(obj) { |
| 48 |
let count = 0; |
| 49 |
for (const _key in obj) { |
| 50 |
count++; |
| 51 |
} |
| 52 |
return count; |
| 53 |
} |
| 54 |
|
| 55 |
// src/query/endpointDefinitions.ts |
| 56 |
function isQueryDefinition(e) { |
| 57 |
return e.type === "query" /* query */; |
| 58 |
} |
| 59 |
function isMutationDefinition(e) { |
| 60 |
return e.type === "mutation" /* mutation */; |
| 61 |
} |
| 62 |
function isInfiniteQueryDefinition(e) { |
| 63 |
return e.type === "infinitequery" /* infinitequery */; |
| 64 |
} |
| 65 |
|
| 66 |
// src/query/tsHelpers.ts |
| 67 |
function safeAssign(target, ...args) { |
| 68 |
return Object.assign(target, ...args); |
| 69 |
} |
| 70 |
|
| 71 |
// src/query/react/buildHooks.ts |
| 72 |
import { formatProdErrorMessage as _formatProdErrorMessage, formatProdErrorMessage as _formatProdErrorMessage2 } from "@reduxjs/toolkit"; |
| 73 |
import { defaultSerializeQueryArgs, QueryStatus, skipToken } from "@reduxjs/toolkit/query"; |
| 74 |
import { useCallback, useDebugValue, useEffect as useEffect3, useLayoutEffect, useMemo as useMemo2, useRef as useRef3, useState } from "react"; |
| 75 |
import { shallowEqual as shallowEqual2 } from "react-redux"; |
| 76 |
|
| 77 |
// src/query/react/constants.ts |
| 78 |
var UNINITIALIZED_VALUE = Symbol(); |
| 79 |
|
| 80 |
// src/query/react/useSerializedStableValue.ts |
| 81 |
import { useEffect, useRef, useMemo } from "react"; |
| 82 |
function useStableQueryArgs(queryArgs, serialize, endpointDefinition, endpointName) { |
| 83 |
const incoming = useMemo(() => ({ |
| 84 |
queryArgs, |
| 85 |
serialized: typeof queryArgs == "object" ? serialize({ |
| 86 |
queryArgs, |
| 87 |
endpointDefinition, |
| 88 |
endpointName |
| 89 |
}) : queryArgs |
| 90 |
}), [queryArgs, serialize, endpointDefinition, endpointName]); |
| 91 |
const cache = useRef(incoming); |
| 92 |
useEffect(() => { |
| 93 |
if (cache.current.serialized !== incoming.serialized) { |
| 94 |
cache.current = incoming; |
| 95 |
} |
| 96 |
}, [incoming]); |
| 97 |
return cache.current.serialized === incoming.serialized ? cache.current.queryArgs : queryArgs; |
| 98 |
} |
| 99 |
|
| 100 |
// src/query/react/useShallowStableValue.ts |
| 101 |
import { useEffect as useEffect2, useRef as useRef2 } from "react"; |
| 102 |
import { shallowEqual } from "react-redux"; |
| 103 |
function useShallowStableValue(value) { |
| 104 |
const cache = useRef2(value); |
| 105 |
useEffect2(() => { |
| 106 |
if (!shallowEqual(cache.current, value)) { |
| 107 |
cache.current = value; |
| 108 |
} |
| 109 |
}, [value]); |
| 110 |
return shallowEqual(cache.current, value) ? cache.current : value; |
| 111 |
} |
| 112 |
|
| 113 |
// src/query/react/buildHooks.ts |
| 114 |
var canUseDOM = () => !!(typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined"); |
| 115 |
var isDOM = /* @__PURE__ */ canUseDOM(); |
| 116 |
var isRunningInReactNative = () => typeof navigator !== "undefined" && navigator.product === "ReactNative"; |
| 117 |
var isReactNative = /* @__PURE__ */ isRunningInReactNative(); |
| 118 |
var getUseIsomorphicLayoutEffect = () => isDOM || isReactNative ? useLayoutEffect : useEffect3; |
| 119 |
var useIsomorphicLayoutEffect = /* @__PURE__ */ getUseIsomorphicLayoutEffect(); |
| 120 |
var noPendingQueryStateSelector = (selected) => { |
| 121 |
if (selected.isUninitialized) { |
| 122 |
return __spreadProps(__spreadValues({}, selected), { |
| 123 |
isUninitialized: false, |
| 124 |
isFetching: true, |
| 125 |
isLoading: selected.data !== void 0 ? false : true, |
| 126 |
status: QueryStatus.pending |
| 127 |
}); |
| 128 |
} |
| 129 |
return selected; |
| 130 |
}; |
| 131 |
function pick(obj, ...keys) { |
| 132 |
const ret = {}; |
| 133 |
keys.forEach((key) => { |
| 134 |
ret[key] = obj[key]; |
| 135 |
}); |
| 136 |
return ret; |
| 137 |
} |
| 138 |
var COMMON_HOOK_DEBUG_FIELDS = ["data", "status", "isLoading", "isSuccess", "isError", "error"]; |
| 139 |
function buildHooks({ |
| 140 |
api, |
| 141 |
moduleOptions: { |
| 142 |
batch, |
| 143 |
hooks: { |
| 144 |
useDispatch, |
| 145 |
useSelector, |
| 146 |
useStore |
| 147 |
}, |
| 148 |
unstable__sideEffectsInRender, |
| 149 |
createSelector |
| 150 |
}, |
| 151 |
serializeQueryArgs, |
| 152 |
context |
| 153 |
}) { |
| 154 |
const usePossiblyImmediateEffect = unstable__sideEffectsInRender ? (cb) => cb() : useEffect3; |
| 155 |
return { |
| 156 |
buildQueryHooks, |
| 157 |
buildInfiniteQueryHooks, |
| 158 |
buildMutationHook, |
| 159 |
usePrefetch |
| 160 |
}; |
| 161 |
function queryStatePreSelector(currentState, lastResult, queryArgs) { |
| 162 |
if ((lastResult == null ? void 0 : lastResult.endpointName) && currentState.isUninitialized) { |
| 163 |
const { |
| 164 |
endpointName |
| 165 |
} = lastResult; |
| 166 |
const endpointDefinition = context.endpointDefinitions[endpointName]; |
| 167 |
if (queryArgs !== skipToken && serializeQueryArgs({ |
| 168 |
queryArgs: lastResult.originalArgs, |
| 169 |
endpointDefinition, |
| 170 |
endpointName |
| 171 |
}) === serializeQueryArgs({ |
| 172 |
queryArgs, |
| 173 |
endpointDefinition, |
| 174 |
endpointName |
| 175 |
})) lastResult = void 0; |
| 176 |
} |
| 177 |
let data = currentState.isSuccess ? currentState.data : lastResult == null ? void 0 : lastResult.data; |
| 178 |
if (data === void 0) data = currentState.data; |
| 179 |
const hasData = data !== void 0; |
| 180 |
const isFetching = currentState.isLoading; |
| 181 |
const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching; |
| 182 |
const isSuccess = currentState.isSuccess || hasData && (isFetching && !(lastResult == null ? void 0 : lastResult.isError) || currentState.isUninitialized); |
| 183 |
return __spreadProps(__spreadValues({}, currentState), { |
| 184 |
data, |
| 185 |
currentData: currentState.data, |
| 186 |
isFetching, |
| 187 |
isLoading, |
| 188 |
isSuccess |
| 189 |
}); |
| 190 |
} |
| 191 |
function infiniteQueryStatePreSelector(currentState, lastResult, queryArgs) { |
| 192 |
if ((lastResult == null ? void 0 : lastResult.endpointName) && currentState.isUninitialized) { |
| 193 |
const { |
| 194 |
endpointName |
| 195 |
} = lastResult; |
| 196 |
const endpointDefinition = context.endpointDefinitions[endpointName]; |
| 197 |
if (serializeQueryArgs({ |
| 198 |
queryArgs: lastResult.originalArgs, |
| 199 |
endpointDefinition, |
| 200 |
endpointName |
| 201 |
}) === serializeQueryArgs({ |
| 202 |
queryArgs, |
| 203 |
endpointDefinition, |
| 204 |
endpointName |
| 205 |
})) lastResult = void 0; |
| 206 |
} |
| 207 |
let data = currentState.isSuccess ? currentState.data : lastResult == null ? void 0 : lastResult.data; |
| 208 |
if (data === void 0) data = currentState.data; |
| 209 |
const hasData = data !== void 0; |
| 210 |
const isFetching = currentState.isLoading; |
| 211 |
const isLoading = (!lastResult || lastResult.isLoading || lastResult.isUninitialized) && !hasData && isFetching; |
| 212 |
const isSuccess = currentState.isSuccess || isFetching && hasData; |
| 213 |
return __spreadProps(__spreadValues({}, currentState), { |
| 214 |
data, |
| 215 |
currentData: currentState.data, |
| 216 |
isFetching, |
| 217 |
isLoading, |
| 218 |
isSuccess |
| 219 |
}); |
| 220 |
} |
| 221 |
function usePrefetch(endpointName, defaultOptions) { |
| 222 |
const dispatch = useDispatch(); |
| 223 |
const stableDefaultOptions = useShallowStableValue(defaultOptions); |
| 224 |
return useCallback((arg, options) => dispatch(api.util.prefetch(endpointName, arg, __spreadValues(__spreadValues({}, stableDefaultOptions), options))), [endpointName, dispatch, stableDefaultOptions]); |
| 225 |
} |
| 226 |
function useQuerySubscriptionCommonImpl(endpointName, arg, _a = {}) { |
| 227 |
var _b = _a, { |
| 228 |
refetchOnReconnect, |
| 229 |
refetchOnFocus, |
| 230 |
refetchOnMountOrArgChange, |
| 231 |
skip = false, |
| 232 |
pollingInterval = 0, |
| 233 |
skipPollingIfUnfocused = false |
| 234 |
} = _b, rest = __objRest(_b, [ |
| 235 |
"refetchOnReconnect", |
| 236 |
"refetchOnFocus", |
| 237 |
"refetchOnMountOrArgChange", |
| 238 |
"skip", |
| 239 |
"pollingInterval", |
| 240 |
"skipPollingIfUnfocused" |
| 241 |
]); |
| 242 |
const { |
| 243 |
initiate |
| 244 |
} = api.endpoints[endpointName]; |
| 245 |
const dispatch = useDispatch(); |
| 246 |
const subscriptionSelectorsRef = useRef3(void 0); |
| 247 |
if (!subscriptionSelectorsRef.current) { |
| 248 |
const returnedValue = dispatch(api.internalActions.internal_getRTKQSubscriptions()); |
| 249 |
if (process.env.NODE_ENV !== "production") { |
| 250 |
if (typeof returnedValue !== "object" || typeof (returnedValue == null ? void 0 : returnedValue.type) === "string") { |
| 251 |
throw new Error(process.env.NODE_ENV === "production" ? _formatProdErrorMessage(37) : `Warning: Middleware for RTK-Query API at reducerPath "${api.reducerPath}" has not been added to the store. |
| 252 |
You must add the middleware for RTK-Query to function correctly!`); |
| 253 |
} |
| 254 |
} |
| 255 |
subscriptionSelectorsRef.current = returnedValue; |
| 256 |
} |
| 257 |
const stableArg = useStableQueryArgs( |
| 258 |
skip ? skipToken : arg, |
| 259 |
// Even if the user provided a per-endpoint `serializeQueryArgs` with |
| 260 |
// a consistent return value, _here_ we want to use the default behavior |
| 261 |
// so we can tell if _anything_ actually changed. Otherwise, we can end up |
| 262 |
// with a case where the query args did change but the serialization doesn't, |
| 263 |
// and then we never try to initiate a refetch. |
| 264 |
defaultSerializeQueryArgs, |
| 265 |
context.endpointDefinitions[endpointName], |
| 266 |
endpointName |
| 267 |
); |
| 268 |
const stableSubscriptionOptions = useShallowStableValue({ |
| 269 |
refetchOnReconnect, |
| 270 |
refetchOnFocus, |
| 271 |
pollingInterval, |
| 272 |
skipPollingIfUnfocused |
| 273 |
}); |
| 274 |
const lastRenderHadSubscription = useRef3(false); |
| 275 |
const initialPageParam = rest.initialPageParam; |
| 276 |
const stableInitialPageParam = useShallowStableValue(initialPageParam); |
| 277 |
const promiseRef = useRef3(void 0); |
| 278 |
let { |
| 279 |
queryCacheKey, |
| 280 |
requestId |
| 281 |
} = promiseRef.current || {}; |
| 282 |
let currentRenderHasSubscription = false; |
| 283 |
if (queryCacheKey && requestId) { |
| 284 |
currentRenderHasSubscription = subscriptionSelectorsRef.current.isRequestSubscribed(queryCacheKey, requestId); |
| 285 |
} |
| 286 |
const subscriptionRemoved = !currentRenderHasSubscription && lastRenderHadSubscription.current; |
| 287 |
usePossiblyImmediateEffect(() => { |
| 288 |
lastRenderHadSubscription.current = currentRenderHasSubscription; |
| 289 |
}); |
| 290 |
usePossiblyImmediateEffect(() => { |
| 291 |
if (subscriptionRemoved) { |
| 292 |
promiseRef.current = void 0; |
| 293 |
} |
| 294 |
}, [subscriptionRemoved]); |
| 295 |
usePossiblyImmediateEffect(() => { |
| 296 |
var _a2; |
| 297 |
const lastPromise = promiseRef.current; |
| 298 |
if (typeof process !== "undefined" && process.env.NODE_ENV === "removeMeOnCompilation") { |
| 299 |
console.log(subscriptionRemoved); |
| 300 |
} |
| 301 |
if (stableArg === skipToken) { |
| 302 |
lastPromise == null ? void 0 : lastPromise.unsubscribe(); |
| 303 |
promiseRef.current = void 0; |
| 304 |
return; |
| 305 |
} |
| 306 |
const lastSubscriptionOptions = (_a2 = promiseRef.current) == null ? void 0 : _a2.subscriptionOptions; |
| 307 |
if (!lastPromise || lastPromise.arg !== stableArg) { |
| 308 |
lastPromise == null ? void 0 : lastPromise.unsubscribe(); |
| 309 |
const promise = dispatch(initiate(stableArg, __spreadValues({ |
| 310 |
subscriptionOptions: stableSubscriptionOptions, |
| 311 |
forceRefetch: refetchOnMountOrArgChange |
| 312 |
}, isInfiniteQueryDefinition(context.endpointDefinitions[endpointName]) ? { |
| 313 |
initialPageParam: stableInitialPageParam |
| 314 |
} : {}))); |
| 315 |
promiseRef.current = promise; |
| 316 |
} else if (stableSubscriptionOptions !== lastSubscriptionOptions) { |
| 317 |
lastPromise.updateSubscriptionOptions(stableSubscriptionOptions); |
| 318 |
} |
| 319 |
}, [dispatch, initiate, refetchOnMountOrArgChange, stableArg, stableSubscriptionOptions, subscriptionRemoved, stableInitialPageParam, endpointName]); |
| 320 |
return [promiseRef, dispatch, initiate, stableSubscriptionOptions]; |
| 321 |
} |
| 322 |
function buildUseQueryState(endpointName, preSelector) { |
| 323 |
const useQueryState = (arg, { |
| 324 |
skip = false, |
| 325 |
selectFromResult |
| 326 |
} = {}) => { |
| 327 |
const { |
| 328 |
select |
| 329 |
} = api.endpoints[endpointName]; |
| 330 |
const stableArg = useStableQueryArgs(skip ? skipToken : arg, serializeQueryArgs, context.endpointDefinitions[endpointName], endpointName); |
| 331 |
const lastValue = useRef3(void 0); |
| 332 |
const selectDefaultResult = useMemo2(() => ( |
| 333 |
// Normally ts-ignores are bad and should be avoided, but we're |
| 334 |
// already casting this selector to be `Selector<any>` anyway, |
| 335 |
// so the inconsistencies don't matter here |
| 336 |
// @ts-ignore |
| 337 |
createSelector([ |
| 338 |
// @ts-ignore |
| 339 |
select(stableArg), |
| 340 |
(_, lastResult) => lastResult, |
| 341 |
(_) => stableArg |
| 342 |
], preSelector, { |
| 343 |
memoizeOptions: { |
| 344 |
resultEqualityCheck: shallowEqual2 |
| 345 |
} |
| 346 |
}) |
| 347 |
), [select, stableArg]); |
| 348 |
const querySelector = useMemo2(() => selectFromResult ? createSelector([selectDefaultResult], selectFromResult, { |
| 349 |
devModeChecks: { |
| 350 |
identityFunctionCheck: "never" |
| 351 |
} |
| 352 |
}) : selectDefaultResult, [selectDefaultResult, selectFromResult]); |
| 353 |
const currentState = useSelector((state) => querySelector(state, lastValue.current), shallowEqual2); |
| 354 |
const store = useStore(); |
| 355 |
const newLastValue = selectDefaultResult(store.getState(), lastValue.current); |
| 356 |
useIsomorphicLayoutEffect(() => { |
| 357 |
lastValue.current = newLastValue; |
| 358 |
}, [newLastValue]); |
| 359 |
return currentState; |
| 360 |
}; |
| 361 |
return useQueryState; |
| 362 |
} |
| 363 |
function usePromiseRefUnsubscribeOnUnmount(promiseRef) { |
| 364 |
useEffect3(() => { |
| 365 |
return () => { |
| 366 |
var _a, _b; |
| 367 |
(_b = (_a = promiseRef.current) == null ? void 0 : _a.unsubscribe) == null ? void 0 : _b.call(_a); |
| 368 |
promiseRef.current = void 0; |
| 369 |
}; |
| 370 |
}, [promiseRef]); |
| 371 |
} |
| 372 |
function refetchOrErrorIfUnmounted(promiseRef) { |
| 373 |
if (!promiseRef.current) throw new Error(process.env.NODE_ENV === "production" ? _formatProdErrorMessage2(38) : "Cannot refetch a query that has not been started yet."); |
| 374 |
return promiseRef.current.refetch(); |
| 375 |
} |
| 376 |
function buildQueryHooks(endpointName) { |
| 377 |
const useQuerySubscription = (arg, options = {}) => { |
| 378 |
const [promiseRef] = useQuerySubscriptionCommonImpl(endpointName, arg, options); |
| 379 |
usePromiseRefUnsubscribeOnUnmount(promiseRef); |
| 380 |
return useMemo2(() => ({ |
| 381 |
/** |
| 382 |
* A method to manually refetch data for the query |
| 383 |
*/ |
| 384 |
refetch: () => refetchOrErrorIfUnmounted(promiseRef) |
| 385 |
}), [promiseRef]); |
| 386 |
}; |
| 387 |
const useLazyQuerySubscription = ({ |
| 388 |
refetchOnReconnect, |
| 389 |
refetchOnFocus, |
| 390 |
pollingInterval = 0, |
| 391 |
skipPollingIfUnfocused = false |
| 392 |
} = {}) => { |
| 393 |
const { |
| 394 |
initiate |
| 395 |
} = api.endpoints[endpointName]; |
| 396 |
const dispatch = useDispatch(); |
| 397 |
const [arg, setArg] = useState(UNINITIALIZED_VALUE); |
| 398 |
const promiseRef = useRef3(void 0); |
| 399 |
const stableSubscriptionOptions = useShallowStableValue({ |
| 400 |
refetchOnReconnect, |
| 401 |
refetchOnFocus, |
| 402 |
pollingInterval, |
| 403 |
skipPollingIfUnfocused |
| 404 |
}); |
| 405 |
usePossiblyImmediateEffect(() => { |
| 406 |
var _a, _b; |
| 407 |
const lastSubscriptionOptions = (_a = promiseRef.current) == null ? void 0 : _a.subscriptionOptions; |
| 408 |
if (stableSubscriptionOptions !== lastSubscriptionOptions) { |
| 409 |
(_b = promiseRef.current) == null ? void 0 : _b.updateSubscriptionOptions(stableSubscriptionOptions); |
| 410 |
} |
| 411 |
}, [stableSubscriptionOptions]); |
| 412 |
const subscriptionOptionsRef = useRef3(stableSubscriptionOptions); |
| 413 |
usePossiblyImmediateEffect(() => { |
| 414 |
subscriptionOptionsRef.current = stableSubscriptionOptions; |
| 415 |
}, [stableSubscriptionOptions]); |
| 416 |
const trigger = useCallback(function(arg2, preferCacheValue = false) { |
| 417 |
let promise; |
| 418 |
batch(() => { |
| 419 |
var _a; |
| 420 |
(_a = promiseRef.current) == null ? void 0 : _a.unsubscribe(); |
| 421 |
promiseRef.current = promise = dispatch(initiate(arg2, { |
| 422 |
subscriptionOptions: subscriptionOptionsRef.current, |
| 423 |
forceRefetch: !preferCacheValue |
| 424 |
})); |
| 425 |
setArg(arg2); |
| 426 |
}); |
| 427 |
return promise; |
| 428 |
}, [dispatch, initiate]); |
| 429 |
const reset = useCallback(() => { |
| 430 |
var _a, _b; |
| 431 |
if ((_a = promiseRef.current) == null ? void 0 : _a.queryCacheKey) { |
| 432 |
dispatch(api.internalActions.removeQueryResult({ |
| 433 |
queryCacheKey: (_b = promiseRef.current) == null ? void 0 : _b.queryCacheKey |
| 434 |
})); |
| 435 |
} |
| 436 |
}, [dispatch]); |
| 437 |
useEffect3(() => { |
| 438 |
return () => { |
| 439 |
var _a; |
| 440 |
(_a = promiseRef == null ? void 0 : promiseRef.current) == null ? void 0 : _a.unsubscribe(); |
| 441 |
}; |
| 442 |
}, []); |
| 443 |
useEffect3(() => { |
| 444 |
if (arg !== UNINITIALIZED_VALUE && !promiseRef.current) { |
| 445 |
trigger(arg, true); |
| 446 |
} |
| 447 |
}, [arg, trigger]); |
| 448 |
return useMemo2(() => [trigger, arg, { |
| 449 |
reset |
| 450 |
}], [trigger, arg, reset]); |
| 451 |
}; |
| 452 |
const useQueryState = buildUseQueryState(endpointName, queryStatePreSelector); |
| 453 |
return { |
| 454 |
useQueryState, |
| 455 |
useQuerySubscription, |
| 456 |
useLazyQuerySubscription, |
| 457 |
useLazyQuery(options) { |
| 458 |
const [trigger, arg, { |
| 459 |
reset |
| 460 |
}] = useLazyQuerySubscription(options); |
| 461 |
const queryStateResults = useQueryState(arg, __spreadProps(__spreadValues({}, options), { |
| 462 |
skip: arg === UNINITIALIZED_VALUE |
| 463 |
})); |
| 464 |
const info = useMemo2(() => ({ |
| 465 |
lastArg: arg |
| 466 |
}), [arg]); |
| 467 |
return useMemo2(() => [trigger, __spreadProps(__spreadValues({}, queryStateResults), { |
| 468 |
reset |
| 469 |
}), info], [trigger, queryStateResults, reset, info]); |
| 470 |
}, |
| 471 |
useQuery(arg, options) { |
| 472 |
const querySubscriptionResults = useQuerySubscription(arg, options); |
| 473 |
const queryStateResults = useQueryState(arg, __spreadValues({ |
| 474 |
selectFromResult: arg === skipToken || (options == null ? void 0 : options.skip) ? void 0 : noPendingQueryStateSelector |
| 475 |
}, options)); |
| 476 |
const debugValue = pick(queryStateResults, ...COMMON_HOOK_DEBUG_FIELDS); |
| 477 |
useDebugValue(debugValue); |
| 478 |
return useMemo2(() => __spreadValues(__spreadValues({}, queryStateResults), querySubscriptionResults), [queryStateResults, querySubscriptionResults]); |
| 479 |
} |
| 480 |
}; |
| 481 |
} |
| 482 |
function buildInfiniteQueryHooks(endpointName) { |
| 483 |
const useInfiniteQuerySubscription = (arg, options = {}) => { |
| 484 |
const [promiseRef, dispatch, initiate, stableSubscriptionOptions] = useQuerySubscriptionCommonImpl(endpointName, arg, options); |
| 485 |
const subscriptionOptionsRef = useRef3(stableSubscriptionOptions); |
| 486 |
usePossiblyImmediateEffect(() => { |
| 487 |
subscriptionOptionsRef.current = stableSubscriptionOptions; |
| 488 |
}, [stableSubscriptionOptions]); |
| 489 |
const trigger = useCallback(function(arg2, direction) { |
| 490 |
let promise; |
| 491 |
batch(() => { |
| 492 |
var _a; |
| 493 |
(_a = promiseRef.current) == null ? void 0 : _a.unsubscribe(); |
| 494 |
promiseRef.current = promise = dispatch(initiate(arg2, { |
| 495 |
subscriptionOptions: subscriptionOptionsRef.current, |
| 496 |
direction |
| 497 |
})); |
| 498 |
}); |
| 499 |
return promise; |
| 500 |
}, [promiseRef, dispatch, initiate]); |
| 501 |
usePromiseRefUnsubscribeOnUnmount(promiseRef); |
| 502 |
return useMemo2(() => { |
| 503 |
const fetchNextPage = () => { |
| 504 |
return trigger(arg, "forward"); |
| 505 |
}; |
| 506 |
const fetchPreviousPage = () => { |
| 507 |
return trigger(arg, "backward"); |
| 508 |
}; |
| 509 |
return { |
| 510 |
trigger, |
| 511 |
/** |
| 512 |
* A method to manually refetch data for the query |
| 513 |
*/ |
| 514 |
refetch: () => refetchOrErrorIfUnmounted(promiseRef), |
| 515 |
fetchNextPage, |
| 516 |
fetchPreviousPage |
| 517 |
}; |
| 518 |
}, [promiseRef, trigger, arg]); |
| 519 |
}; |
| 520 |
const useInfiniteQueryState = buildUseQueryState(endpointName, infiniteQueryStatePreSelector); |
| 521 |
return { |
| 522 |
useInfiniteQueryState, |
| 523 |
useInfiniteQuerySubscription, |
| 524 |
useInfiniteQuery(arg, options) { |
| 525 |
const { |
| 526 |
refetch, |
| 527 |
fetchNextPage, |
| 528 |
fetchPreviousPage |
| 529 |
} = useInfiniteQuerySubscription(arg, options); |
| 530 |
const queryStateResults = useInfiniteQueryState(arg, __spreadValues({ |
| 531 |
selectFromResult: arg === skipToken || (options == null ? void 0 : options.skip) ? void 0 : noPendingQueryStateSelector |
| 532 |
}, options)); |
| 533 |
const debugValue = pick(queryStateResults, ...COMMON_HOOK_DEBUG_FIELDS, "hasNextPage", "hasPreviousPage"); |
| 534 |
useDebugValue(debugValue); |
| 535 |
return useMemo2(() => __spreadProps(__spreadValues({}, queryStateResults), { |
| 536 |
fetchNextPage, |
| 537 |
fetchPreviousPage, |
| 538 |
refetch |
| 539 |
}), [queryStateResults, fetchNextPage, fetchPreviousPage, refetch]); |
| 540 |
} |
| 541 |
}; |
| 542 |
} |
| 543 |
function buildMutationHook(name) { |
| 544 |
return ({ |
| 545 |
selectFromResult, |
| 546 |
fixedCacheKey |
| 547 |
} = {}) => { |
| 548 |
const { |
| 549 |
select, |
| 550 |
initiate |
| 551 |
} = api.endpoints[name]; |
| 552 |
const dispatch = useDispatch(); |
| 553 |
const [promise, setPromise] = useState(); |
| 554 |
useEffect3(() => () => { |
| 555 |
if (!(promise == null ? void 0 : promise.arg.fixedCacheKey)) { |
| 556 |
promise == null ? void 0 : promise.reset(); |
| 557 |
} |
| 558 |
}, [promise]); |
| 559 |
const triggerMutation = useCallback(function(arg) { |
| 560 |
const promise2 = dispatch(initiate(arg, { |
| 561 |
fixedCacheKey |
| 562 |
})); |
| 563 |
setPromise(promise2); |
| 564 |
return promise2; |
| 565 |
}, [dispatch, initiate, fixedCacheKey]); |
| 566 |
const { |
| 567 |
requestId |
| 568 |
} = promise || {}; |
| 569 |
const selectDefaultResult = useMemo2(() => select({ |
| 570 |
fixedCacheKey, |
| 571 |
requestId: promise == null ? void 0 : promise.requestId |
| 572 |
}), [fixedCacheKey, promise, select]); |
| 573 |
const mutationSelector = useMemo2(() => selectFromResult ? createSelector([selectDefaultResult], selectFromResult) : selectDefaultResult, [selectFromResult, selectDefaultResult]); |
| 574 |
const currentState = useSelector(mutationSelector, shallowEqual2); |
| 575 |
const originalArgs = fixedCacheKey == null ? promise == null ? void 0 : promise.arg.originalArgs : void 0; |
| 576 |
const reset = useCallback(() => { |
| 577 |
batch(() => { |
| 578 |
if (promise) { |
| 579 |
setPromise(void 0); |
| 580 |
} |
| 581 |
if (fixedCacheKey) { |
| 582 |
dispatch(api.internalActions.removeMutationResult({ |
| 583 |
requestId, |
| 584 |
fixedCacheKey |
| 585 |
})); |
| 586 |
} |
| 587 |
}); |
| 588 |
}, [dispatch, fixedCacheKey, promise, requestId]); |
| 589 |
const debugValue = pick(currentState, ...COMMON_HOOK_DEBUG_FIELDS, "endpointName"); |
| 590 |
useDebugValue(debugValue); |
| 591 |
const finalState = useMemo2(() => __spreadProps(__spreadValues({}, currentState), { |
| 592 |
originalArgs, |
| 593 |
reset |
| 594 |
}), [currentState, originalArgs, reset]); |
| 595 |
return useMemo2(() => [triggerMutation, finalState], [triggerMutation, finalState]); |
| 596 |
}; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
// src/query/react/module.ts |
| 601 |
var reactHooksModuleName = /* @__PURE__ */ Symbol(); |
| 602 |
var reactHooksModule = (_a = {}) => { |
| 603 |
var _b = _a, { |
| 604 |
batch = rrBatch, |
| 605 |
hooks = { |
| 606 |
useDispatch: rrUseDispatch, |
| 607 |
useSelector: rrUseSelector, |
| 608 |
useStore: rrUseStore |
| 609 |
}, |
| 610 |
createSelector = _createSelector, |
| 611 |
unstable__sideEffectsInRender = false |
| 612 |
} = _b, rest = __objRest(_b, [ |
| 613 |
"batch", |
| 614 |
"hooks", |
| 615 |
"createSelector", |
| 616 |
"unstable__sideEffectsInRender" |
| 617 |
]); |
| 618 |
if (process.env.NODE_ENV !== "production") { |
| 619 |
const hookNames = ["useDispatch", "useSelector", "useStore"]; |
| 620 |
let warned = false; |
| 621 |
for (const hookName of hookNames) { |
| 622 |
if (countObjectKeys(rest) > 0) { |
| 623 |
if (rest[hookName]) { |
| 624 |
if (!warned) { |
| 625 |
console.warn("As of RTK 2.0, the hooks now need to be specified as one object, provided under a `hooks` key:\n`reactHooksModule({ hooks: { useDispatch, useSelector, useStore } })`"); |
| 626 |
warned = true; |
| 627 |
} |
| 628 |
} |
| 629 |
hooks[hookName] = rest[hookName]; |
| 630 |
} |
| 631 |
if (typeof hooks[hookName] !== "function") { |
| 632 |
throw new Error(process.env.NODE_ENV === "production" ? _formatProdErrorMessage3(36) : `When using custom hooks for context, all ${hookNames.length} hooks need to be provided: ${hookNames.join(", ")}. |
| 633 |
Hook ${hookName} was either not provided or not a function.`); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
return { |
| 638 |
name: reactHooksModuleName, |
| 639 |
init(api, { |
| 640 |
serializeQueryArgs |
| 641 |
}, context) { |
| 642 |
const anyApi = api; |
| 643 |
const { |
| 644 |
buildQueryHooks, |
| 645 |
buildInfiniteQueryHooks, |
| 646 |
buildMutationHook, |
| 647 |
usePrefetch |
| 648 |
} = buildHooks({ |
| 649 |
api, |
| 650 |
moduleOptions: { |
| 651 |
batch, |
| 652 |
hooks, |
| 653 |
unstable__sideEffectsInRender, |
| 654 |
createSelector |
| 655 |
}, |
| 656 |
serializeQueryArgs, |
| 657 |
context |
| 658 |
}); |
| 659 |
safeAssign(anyApi, { |
| 660 |
usePrefetch |
| 661 |
}); |
| 662 |
safeAssign(context, { |
| 663 |
batch |
| 664 |
}); |
| 665 |
return { |
| 666 |
injectEndpoint(endpointName, definition) { |
| 667 |
if (isQueryDefinition(definition)) { |
| 668 |
const { |
| 669 |
useQuery, |
| 670 |
useLazyQuery, |
| 671 |
useLazyQuerySubscription, |
| 672 |
useQueryState, |
| 673 |
useQuerySubscription |
| 674 |
} = buildQueryHooks(endpointName); |
| 675 |
safeAssign(anyApi.endpoints[endpointName], { |
| 676 |
useQuery, |
| 677 |
useLazyQuery, |
| 678 |
useLazyQuerySubscription, |
| 679 |
useQueryState, |
| 680 |
useQuerySubscription |
| 681 |
}); |
| 682 |
api[`use${capitalize(endpointName)}Query`] = useQuery; |
| 683 |
api[`useLazy${capitalize(endpointName)}Query`] = useLazyQuery; |
| 684 |
} |
| 685 |
if (isMutationDefinition(definition)) { |
| 686 |
const useMutation = buildMutationHook(endpointName); |
| 687 |
safeAssign(anyApi.endpoints[endpointName], { |
| 688 |
useMutation |
| 689 |
}); |
| 690 |
api[`use${capitalize(endpointName)}Mutation`] = useMutation; |
| 691 |
} else if (isInfiniteQueryDefinition(definition)) { |
| 692 |
const { |
| 693 |
useInfiniteQuery, |
| 694 |
useInfiniteQuerySubscription, |
| 695 |
useInfiniteQueryState |
| 696 |
} = buildInfiniteQueryHooks(endpointName); |
| 697 |
safeAssign(anyApi.endpoints[endpointName], { |
| 698 |
useInfiniteQuery, |
| 699 |
useInfiniteQuerySubscription, |
| 700 |
useInfiniteQueryState |
| 701 |
}); |
| 702 |
api[`use${capitalize(endpointName)}InfiniteQuery`] = useInfiniteQuery; |
| 703 |
} |
| 704 |
} |
| 705 |
}; |
| 706 |
} |
| 707 |
}; |
| 708 |
}; |
| 709 |
|
| 710 |
// src/query/react/index.ts |
| 711 |
export * from "@reduxjs/toolkit/query"; |
| 712 |
|
| 713 |
// src/query/react/ApiProvider.tsx |
| 714 |
import { configureStore, formatProdErrorMessage as _formatProdErrorMessage4 } from "@reduxjs/toolkit"; |
| 715 |
import { useContext } from "react"; |
| 716 |
import { useEffect as useEffect4 } from "react"; |
| 717 |
import * as React from "react"; |
| 718 |
import { Provider, ReactReduxContext } from "react-redux"; |
| 719 |
import { setupListeners } from "@reduxjs/toolkit/query"; |
| 720 |
function ApiProvider(props) { |
| 721 |
const context = props.context || ReactReduxContext; |
| 722 |
const existingContext = useContext(context); |
| 723 |
if (existingContext) { |
| 724 |
throw new Error(process.env.NODE_ENV === "production" ? _formatProdErrorMessage4(35) : "Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup."); |
| 725 |
} |
| 726 |
const [store] = React.useState(() => configureStore({ |
| 727 |
reducer: { |
| 728 |
[props.api.reducerPath]: props.api.reducer |
| 729 |
}, |
| 730 |
middleware: (gDM) => gDM().concat(props.api.middleware) |
| 731 |
})); |
| 732 |
useEffect4(() => props.setupListeners === false ? void 0 : setupListeners(store.dispatch, props.setupListeners), [props.setupListeners, store.dispatch]); |
| 733 |
return /* @__PURE__ */ React.createElement(Provider, { store, context }, props.children); |
| 734 |
} |
| 735 |
|
| 736 |
// src/query/react/index.ts |
| 737 |
var createApi = /* @__PURE__ */ buildCreateApi(coreModule(), reactHooksModule()); |
| 738 |
export { |
| 739 |
ApiProvider, |
| 740 |
UNINITIALIZED_VALUE, |
| 741 |
createApi, |
| 742 |
reactHooksModule, |
| 743 |
reactHooksModuleName |
| 744 |
}; |
| 745 |
//# sourceMappingURL=rtk-query-react.legacy-esm.js.map |