UNPKG

@reduxjs/toolkit

Version:

The official, opinionated, batteries-included toolset for efficient Redux development

2 lines 6.86 kB
var e=Object.defineProperty,t=Object.defineProperties,r=Object.getOwnPropertyDescriptors,n=Object.getOwnPropertySymbols,i=Object.prototype.hasOwnProperty,u=Object.prototype.propertyIsEnumerable,o=(t,r,n)=>r in t?e(t,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[r]=n,s=(e,t)=>{for(var r in t||(t={}))i.call(t,r)&&o(e,r,t[r]);if(n)for(var r of n(t))u.call(t,r)&&o(e,r,t[r]);return e},c=(e,n)=>t(e,r(n));import{coreModule as a,buildCreateApi as l}from"@reduxjs/toolkit/query";import{createSelector as d}from"@reduxjs/toolkit";import{useCallback as p,useEffect as f,useLayoutEffect as y,useMemo as m,useRef as b,useState as v}from"react";import{QueryStatus as h,skipToken as g}from"@reduxjs/toolkit/query";import{shallowEqual as O}from"react-redux";import{useEffect as S,useRef as Q,useMemo as x}from"react";function q(e,t,r,n){const i=x((()=>({queryArgs:e,serialized:"object"==typeof e?t({queryArgs:e,endpointDefinition:r,endpointName:n}):e})),[e,t,r,n]),u=Q(i);return S((()=>{u.current.serialized!==i.serialized&&(u.current=i)}),[i]),u.current.serialized===i.serialized?u.current.queryArgs:e}var z=Symbol();import{useEffect as j,useRef as R}from"react";import{shallowEqual as k}from"react-redux";function A(e){const t=R(e);return j((()=>{k(t.current,e)||(t.current=e)}),[e]),k(t.current,e)?t.current:e}var w,I,D="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?y:f,L=e=>e,E=e=>e,P=e=>e.isUninitialized?c(s({},e),{isUninitialized:!1,isFetching:!0,isLoading:void 0===e.data,status:h.pending}):e;function F(e){return e.replace(e[0],e[0].toUpperCase())}function C(e,...t){Object.assign(e,...t)}(I=w||(w={})).query="query",I.mutation="mutation";import{useDispatch as M,useSelector as K,useStore as _,batch as N}from"react-redux";var H=Symbol(),U=({batch:e=N,useDispatch:t=M,useSelector:r=K,useStore:n=_,unstable__sideEffectsInRender:i=!1}={})=>({name:H,init(u,{serializeQueryArgs:o},a){const l=u,{buildQueryHooks:y,buildMutationHook:h,usePrefetch:S}=function({api:e,moduleOptions:{batch:t,useDispatch:r,useSelector:n,useStore:i,unstable__sideEffectsInRender:u},serializeQueryArgs:o,context:a}){const l=u?e=>e():f;return{buildQueryHooks:function(u){const h=(t,{refetchOnReconnect:i,refetchOnFocus:s,refetchOnMountOrArgChange:c,skip:d=!1,pollingInterval:p=0}={})=>{const{initiate:y}=e.endpoints[u],v=r(),h=q(d?g:t,o,a.endpointDefinitions[u],u),O=A({refetchOnReconnect:i,refetchOnFocus:s,pollingInterval:p}),S=b();let{queryCacheKey:Q,requestId:x}=S.current||{};const z=n((t=>{var r;return!!Q&&!!x&&!(null==(r=t[e.reducerPath].subscriptions[Q])?void 0:r[x])}));return l((()=>{S.current=void 0}),[z]),l((()=>{var e;const t=S.current;if(h===g)return null==t||t.unsubscribe(),void(S.current=void 0);const r=null==(e=S.current)?void 0:e.subscriptionOptions;if(t&&t.arg===h)O!==r&&t.updateSubscriptionOptions(O);else{null==t||t.unsubscribe();const e=v(y(h,{subscriptionOptions:O,forceRefetch:c}));S.current=e}}),[v,y,c,h,O,z]),f((()=>()=>{var e;null==(e=S.current)||e.unsubscribe(),S.current=void 0}),[]),m((()=>({refetch:()=>{var e;null==(e=S.current)||e.refetch()}})),[])},S=({refetchOnReconnect:n,refetchOnFocus:i,pollingInterval:o=0}={})=>{const{initiate:s}=e.endpoints[u],c=r(),[a,d]=v(z),y=b(),h=A({refetchOnReconnect:n,refetchOnFocus:i,pollingInterval:o});l((()=>{var e,t;const r=null==(e=y.current)?void 0:e.subscriptionOptions;h!==r&&(null==(t=y.current)||t.updateSubscriptionOptions(h))}),[h]);const g=b(h);l((()=>{g.current=h}),[h]);const O=p((function(e,r=!1){let n;return t((()=>{var t;null==(t=y.current)||t.unsubscribe(),y.current=n=c(s(e,{subscriptionOptions:g.current,forceRefetch:!r})),d(e)})),n}),[c,s]);return f((()=>()=>{var e;null==(e=null==y?void 0:y.current)||e.unsubscribe()}),[]),f((()=>{a===z||y.current||O(a,!0)}),[a,O]),m((()=>[O,a]),[O,a])},Q=(t,{skip:r=!1,selectFromResult:s=L}={})=>{const{select:c}=e.endpoints[u],l=q(r?g:t,o,a.endpointDefinitions[u],u),p=b(),f=m((()=>d([c(l),(e,t)=>t,e=>l],y)),[c,l]),v=m((()=>d([f],s)),[f,s]),h=n((e=>v(e,p.current)),O),S=i(),Q=f(S.getState(),p.current);return D((()=>{p.current=Q}),[Q]),h};return{useQueryState:Q,useQuerySubscription:h,useLazyQuerySubscription:S,useLazyQuery(e){const[t,r]=S(e),n=Q(r,c(s({},e),{skip:r===z})),i=m((()=>({lastArg:r})),[r]);return m((()=>[t,n,i]),[t,n,i])},useQuery(e,t){const r=h(e,t),n=Q(e,s({selectFromResult:e===g||(null==t?void 0:t.skip)?void 0:P},t));return m((()=>s(s({},n),r)),[n,r])}}},buildMutationHook:function(i){return({selectFromResult:u=E,fixedCacheKey:o}={})=>{const{select:a,initiate:l}=e.endpoints[i],y=r(),[b,h]=v();f((()=>()=>{(null==b?void 0:b.arg.fixedCacheKey)||null==b||b.reset()}),[b]);const g=p((function(e){const t=y(l(e,{fixedCacheKey:o}));return h(t),t}),[y,l,o]),{requestId:S}=b||{},Q=m((()=>d([a({fixedCacheKey:o,requestId:null==b?void 0:b.requestId})],u)),[a,b,u,o]),x=n(Q,O),q=null==o?null==b?void 0:b.arg.originalArgs:void 0,z=p((()=>{t((()=>{b&&h(void 0),o&&y(e.internalActions.removeMutationResult({requestId:S,fixedCacheKey:o}))}))}),[y,o,b,S]),j=m((()=>c(s({},x),{originalArgs:q,reset:z})),[x,q,z]);return m((()=>[g,j]),[g,j])}},usePrefetch:function(t,n){const i=r(),u=A(n);return p(((r,n)=>i(e.util.prefetch(t,r,s(s({},u),n)))),[t,i,u])}};function y(e,t,r){if((null==t?void 0:t.endpointName)&&e.isUninitialized){const{endpointName:e}=t,n=a.endpointDefinitions[e];o({queryArgs:t.originalArgs,endpointDefinition:n,endpointName:e})===o({queryArgs:r,endpointDefinition:n,endpointName:e})&&(t=void 0)}let n=e.isSuccess?e.data:null==t?void 0:t.data;void 0===n&&(n=e.data);const i=void 0!==n,u=e.isLoading,l=!i&&u,d=e.isSuccess||u&&i;return c(s({},e),{data:n,currentData:e.data,isFetching:u,isLoading:l,isSuccess:d})}}({api:u,moduleOptions:{batch:e,useDispatch:t,useSelector:r,useStore:n,unstable__sideEffectsInRender:i},serializeQueryArgs:o,context:a});return C(l,{usePrefetch:S}),C(a,{batch:e}),{injectEndpoint(e,t){if(t.type===w.query){const{useQuery:t,useLazyQuery:r,useLazyQuerySubscription:n,useQueryState:i,useQuerySubscription:o}=y(e);C(l.endpoints[e],{useQuery:t,useLazyQuery:r,useLazyQuerySubscription:n,useQueryState:i,useQuerySubscription:o}),u[`use${F(e)}Query`]=t,u[`useLazy${F(e)}Query`]=r}else if(t.type===w.mutation){const t=h(e);C(l.endpoints[e],{useMutation:t}),u[`use${F(e)}Mutation`]=t}}}}});export*from"@reduxjs/toolkit/query";import{configureStore as $}from"@reduxjs/toolkit";import B from"react";import{Provider as G}from"react-redux";import{setupListeners as J}from"@reduxjs/toolkit/query";function T(e){const[t]=B.useState((()=>$({reducer:{[e.api.reducerPath]:e.api.reducer},middleware:t=>t().concat(e.api.middleware)})));return J(t.dispatch,e.setupListeners),B.createElement(G,{store:t,context:e.context},e.children)}var V=l(a(),U());export{T as ApiProvider,V as createApi,U as reactHooksModule}; //# sourceMappingURL=rtk-query-react.modern.production.min.js.map