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