@reduxjs/toolkit
Version:
The official, opinionated, batteries-included toolset for efficient Redux development
2 lines • 8.53 kB
JavaScript
import{QueryStatus as e,buildCreateApi as t,copyWithStructuralSharing as n,coreModule as r,setupListeners as i,skipToken as a}from"@reduxjs/toolkit/query";import{configureStore as o,formatProdErrorMessage as s}from"@reduxjs/toolkit";import{Provider as c,ReactReduxContext as l,batch as u,shallowEqual as d,useDispatch as f,useSelector as p,useStore as m}from"react-redux";import{createSelector as h}from"reselect";import*as g from"react";import{useCallback as _,useContext as v,useDebugValue as y,useEffect as b,useLayoutEffect as x,useMemo as S,useRef as C,useState as w}from"react";export*from"@reduxjs/toolkit/query";function T(e){return e.replace(e[0],e[0].toUpperCase())}function E(e){return e.type===`query`}function D(e){return e.type===`mutation`}function O(e){return e.type===`infinitequery`}function k(e,...t){return Object.assign(e,...t)}const A=Symbol();function j(e){let t=C(e),r=S(()=>n(t.current,e),[e]);return b(()=>{t.current!==r&&(t.current=r)},[r]),r}function M(e){let t=C(e);return b(()=>{d(t.current,e)||(t.current=e)},[e]),d(t.current,e)?t.current:e}const N=typeof window<`u`&&window.document!==void 0&&window.document.createElement!==void 0,P=typeof navigator<`u`&&navigator.product===`ReactNative`,F=N||P?x:b,I=t=>t.isUninitialized?{...t,isUninitialized:!1,isFetching:!0,isLoading:t.data===void 0,status:e.pending}:t;function L(e,...t){let n={};return t.forEach(t=>{n[t]=e[t]}),n}const R=[`data`,`status`,`isLoading`,`isSuccess`,`isError`,`error`];function z({api:e,moduleOptions:{batch:t,hooks:{useDispatch:n,useSelector:r},unstable__sideEffectsInRender:i,createSelector:o},serializeQueryArgs:c,context:l}){let u=i?e=>e():b,f=e=>{e.current?.unsubscribe?.(),e.current=void 0},p=l.endpointDefinitions;return{buildQueryHooks:D,buildInfiniteQueryHooks:k,buildMutationHook:N,usePrefetch:g};function m(e,t,n){if(t?.endpointName&&e.isUninitialized){let{endpointName:e}=t,r=p[e];n!==a&&c({queryArgs:t.originalArgs,endpointDefinition:r,endpointName:e})===c({queryArgs:n,endpointDefinition:r,endpointName:e})&&(t=void 0)}let r=e.data;r===void 0&&(r=t?.data);let i=r!==void 0,o=e.isLoading,s=(!t||t.isLoading||t.isUninitialized)&&!i&&o,l=e.isSuccess||i&&(o&&e.error===void 0||e.isUninitialized);return{...e,data:r,currentData:e.data,isFetching:o,isLoading:s,isSuccess:l}}function h(e,t,n){if(t?.endpointName&&e.isUninitialized){let{endpointName:e}=t,r=p[e];n!==a&&c({queryArgs:t.originalArgs,endpointDefinition:r,endpointName:e})===c({queryArgs:n,endpointDefinition:r,endpointName:e})&&(t=void 0)}let r=e.data;r===void 0&&(r=t?.data);let i=r!==void 0,o=e.isLoading,s=(!t||t.isLoading||t.isUninitialized)&&!i&&o,l=e.isSuccess||i&&(o&&e.error===void 0||e.isUninitialized);return{...e,data:r,currentData:e.data,isFetching:o,isLoading:s,isSuccess:l}}function g(t,r){let i=n(),a=M(r);return _((n,r)=>i(e.util.prefetch(t,n,{...a,...r})),[t,i,a])}function v(t,r,{refetchOnReconnect:i,refetchOnFocus:o,refetchOnMountOrArgChange:s,skip:c=!1,pollingInterval:l=0,skipPollingIfUnfocused:d=!1,...f}={}){let{initiate:m}=e.endpoints[t],h=n(),g=C(void 0);g.current||=h(e.internalActions.internal_getRTKQSubscriptions());let _=j(c?a:r),v=M({refetchOnReconnect:i,refetchOnFocus:o,pollingInterval:l,skipPollingIfUnfocused:d}),y=f.initialPageParam,b=M(y),x=f.refetchCachedPages,S=M(x),w=C(void 0),{queryCacheKey:T,requestId:E}=w.current||{},D=!1;T&&E&&(D=g.current.isRequestSubscribed(T,E));let k=!D&&w.current!==void 0;return u(()=>{k&&(w.current=void 0)},[k]),u(()=>{let e=w.current;if(_===a){e?.unsubscribe(),w.current=void 0;return}let n=w.current?.subscriptionOptions;if(!e||e.arg!==_){e?.unsubscribe();let n=h(m(_,{subscriptionOptions:v,forceRefetch:s,...O(p[t])?{initialPageParam:b,refetchCachedPages:S}:{}}));w.current=n}else v!==n&&e.updateSubscriptionOptions(v)},[h,m,s,_,v,k,b,S,t]),[w,h,m,v]}function x(t,n){return(i,{skip:s=!1,selectFromResult:c}={})=>{let{select:l}=e.endpoints[t],u=j(s?a:i),f=C(void 0),p=C(void 0),m=S(()=>o([l(u),(e,t)=>t,e=>u],n,{memoizeOptions:{resultEqualityCheck:d}}),[l,u]),h=r(S(()=>{let e=c?o([m],c,{devModeChecks:{identityFunctionCheck:`never`}}):m;return t=>{let n=f.current,r=e(t,n);return p.current=m(t,n),r}},[m,c]),d);return F(()=>{f.current=p.current}),h}}function T(e){b(()=>()=>{f(e)},[e])}function E(e){if(!e.current)throw Error(s(38));return e.current.refetch()}function D(r){let i=(e,t={})=>{let[n]=v(r,e,t);return T(n),S(()=>({refetch:()=>E(n)}),[n])},o=({refetchOnReconnect:i,refetchOnFocus:a,pollingInterval:o=0,skipPollingIfUnfocused:s=!1}={})=>{let{initiate:c}=e.endpoints[r],l=n(),[d,p]=w(A),m=C(void 0),h=M({refetchOnReconnect:i,refetchOnFocus:a,pollingInterval:o,skipPollingIfUnfocused:s});u(()=>{let e=m.current?.subscriptionOptions;h!==e&&m.current?.updateSubscriptionOptions(h)},[h]);let g=C(h);u(()=>{g.current=h},[h]);let v=_(function(e,n=!1){let r;return t(()=>{f(m),m.current=r=l(c(e,{subscriptionOptions:g.current,forceRefetch:!n})),p(e)}),r},[l,c]),y=_(()=>{m.current?.queryCacheKey&&l(e.internalActions.removeQueryResult({queryCacheKey:m.current?.queryCacheKey}))},[l]);return b(()=>()=>{f(m)},[]),b(()=>{d!==A&&!m.current&&v(d,!0)},[d,v]),S(()=>[v,d,{reset:y}],[v,d,y])},s=x(r,m);return{useQueryState:s,useQuerySubscription:i,useLazyQuerySubscription:o,useLazyQuery(e){let[t,n,{reset:r}]=o(e),i=s(n,{...e,skip:n===A}),a=S(()=>({lastArg:n}),[n]);return S(()=>[t,{...i,reset:r},a],[t,i,r,a])},useQuery(e,t){let n=i(e,t),r=s(e,{selectFromResult:e===a||t?.skip?void 0:I,...t}),o=L(r,...R);return y(o),S(()=>({...r,...n}),[r,n])}}}function k(e){let n=(n,r={})=>{let[i,o,c,l]=v(e,n,r),d=C(l);u(()=>{d.current=l},[l]);let p=r.refetchCachedPages,m=M(p),h=_(function(e,n){let r;return t(()=>{f(i),i.current=r=o(c(e,{subscriptionOptions:d.current,direction:n}))}),r},[i,o,c]);T(i);let g=j(r.skip?a:n),y=_(e=>{if(!i.current)throw Error(s(38));let t={refetchCachedPages:e?.refetchCachedPages??m};return i.current.refetch(t)},[i,m]);return S(()=>({trigger:h,refetch:y,fetchNextPage:()=>h(g,`forward`),fetchPreviousPage:()=>h(g,`backward`)}),[y,h,g])},r=x(e,h);return{useInfiniteQueryState:r,useInfiniteQuerySubscription:n,useInfiniteQuery(e,t){let{refetch:i,fetchNextPage:o,fetchPreviousPage:s}=n(e,t),c=r(e,{selectFromResult:e===a||t?.skip?void 0:I,...t}),l=L(c,...R,`hasNextPage`,`hasPreviousPage`);return y(l),S(()=>({...c,fetchNextPage:o,fetchPreviousPage:s,refetch:i}),[c,o,s,i])}}}function N(i){return({selectFromResult:a,fixedCacheKey:s}={})=>{let{select:c,initiate:l}=e.endpoints[i],u=n(),[f,p]=w();b(()=>()=>{f?.arg.fixedCacheKey||f?.reset()},[f]);let m=_(function(e){let t=u(l(e,{fixedCacheKey:s}));return p(t),t},[u,l,s]),{requestId:h}=f||{},g=S(()=>c({fixedCacheKey:s,requestId:f?.requestId}),[s,f,c]),v=r(S(()=>a?o([g],a):g,[a,g]),d),x=s==null?f?.arg.originalArgs:void 0,C=_(()=>{t(()=>{f&&p(void 0),s&&u(e.internalActions.removeMutationResult({requestId:h,fixedCacheKey:s}))})},[u,s,f,h]),T=L(v,...R,`endpointName`);y(T);let E=S(()=>({...v,originalArgs:x,reset:C}),[v,x,C]);return S(()=>[m,E],[m,E])}}}const B=Symbol(),V=({batch:e=u,hooks:t={useDispatch:f,useSelector:p,useStore:m},createSelector:n=h,unstable__sideEffectsInRender:r=!1,...i}={})=>({name:B,init(i,{serializeQueryArgs:a},o){let s=i,{buildQueryHooks:c,buildInfiniteQueryHooks:l,buildMutationHook:u,usePrefetch:d}=z({api:i,moduleOptions:{batch:e,hooks:t,unstable__sideEffectsInRender:r,createSelector:n},serializeQueryArgs:a,context:o});return k(s,{usePrefetch:d}),k(o,{batch:e}),{injectEndpoint(e,t){if(E(t)){let{useQuery:t,useLazyQuery:n,useLazyQuerySubscription:r,useQueryState:a,useQuerySubscription:o}=c(e);k(s.endpoints[e],{useQuery:t,useLazyQuery:n,useLazyQuerySubscription:r,useQueryState:a,useQuerySubscription:o}),i[`use${T(e)}Query`]=t,i[`useLazy${T(e)}Query`]=n}if(D(t)){let t=u(e);k(s.endpoints[e],{useMutation:t}),i[`use${T(e)}Mutation`]=t}else if(O(t)){let{useInfiniteQuery:t,useInfiniteQuerySubscription:n,useInfiniteQueryState:r}=l(e);k(s.endpoints[e],{useInfiniteQuery:t,useInfiniteQuerySubscription:n,useInfiniteQueryState:r}),i[`use${T(e)}InfiniteQuery`]=t}}}}});function H(e){let t=e.context||l;if(v(t))throw Error(s(35));let[n]=g.useState(()=>o({reducer:{[e.api.reducerPath]:e.api.reducer},middleware:t=>t().concat(e.api.middleware)}));return b(()=>e.setupListeners===!1?void 0:i(n.dispatch,e.setupListeners),[e.setupListeners,n.dispatch]),g.createElement(c,{store:n,context:t},e.children)}const U=t(r(),V());export{H as ApiProvider,A as UNINITIALIZED_VALUE,U as createApi,V as reactHooksModule,B as reactHooksModuleName};
//# sourceMappingURL=rtk-query-react.browser.mjs.map