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