UNPKG

7.61 kBJavaScriptView Raw
1import{objectSpread2 as a,objectWithoutProperties as e,slicedToArray as r,defineProperty as t,toConsumableArray as n,typeof as o,asyncToGenerator as p,regeneratorRuntime as s}from"./_virtual/_rollupPluginBabelHelpers.js";import{useSyncExternalStore as i,useRef as c,useState as u,useReducer as d,useMemo as l,useCallback as g,useEffect as m}from"react";import{pick as f,omit as y,debounce as P,skipParams as v}from"./utils/index.js";import{subscribe as h,getSnapshot as b,has as E,clear as B,setKey as k,setTTL as x,setPeriodTime as C,setPeriodically as L,get as w,add as O}from"./garbage.js";var R=["params"],S=["params"],T=["sort","pagination"],A=function(e,r){switch(r.type){case"SET":return a(a({},e),"function"==typeof r.payload?r.payload(e):r.payload);case"CLEAR":return{};case"CLEAR_EXCEPT":return f(e,r.payload);case"CLEAR_ONLY":return y(e,r.payload);case"REMOVE":return y(e,[r.payload]);default:return e}},_=function(e,r){switch(r.type){case"set":return n(r.payload);case"update":var t=e[r.index];return e[r.index]="object"===o(r.value)?a(a({},t),r.value):r.value,n(e);case"add":return"number"==typeof r.position?e.splice(r.position,0,r.value):"start"===r.position?e.unshift(r.value):e.push(r.value),n(e);case"remove":return e.splice(e.findIndex(r.condition),1),n(e);default:return n(e)}},M={selector:function(a){return[]},total:function(a){return 0},service:function(a){},cache:{key:"",ttl:3600,periodTime:6e4,periodically:!1},replaceUrl:!1,disabledOnDidMount:!1,sort:{params:{order:"order",orderBy:"orderBy"},order:"desc",orderBy:"id"},deps:[],pagination:{params:{page:"page",perPage:"perPage"},startPage:1,perPage:10,perPageOptions:[5,10,15,25],from:function(a,e,r,t){return(e-1+(0===t?1:0))*r+(0===a?0:0===t?1:t)},to:function(a,e,r,t){return Math.min(a,(e+(0===t?1:0))*r)},lastPage:function(a,e){return Math.max(0,Math.ceil(a/e))},disableFirst:function(a,e,r){return 0!==a&&e===r},disableLast:function(a,e,r){return 0!==a&&e===r}}},j=function(o){var j=new AbortController,U=a(a({},f(M,["deps"])),f(o,["deps"])),N=M.sort,F=N.params,D=e(N,R),I=M.pagination,J=I.params,V=e(I,S),X=e(M,T),Y=a(a({},X),y(o||{},["pagination","sort"])),q=!!o.replaceUrl,G=a(a({},D),{},{params:a(a({},F),o&&o.sort&&o.sort.params)},y(o&&o.sort||{},["params"])),H=a(a({},V),{},{params:a(a({},J),o&&o.pagination&&o.pagination.params)},y(o&&o.pagination||{},["params"])),Q=a(a({},M.cache),o&&o.cache),W=new URL(window.location.href),z=new URLSearchParams(W.search),K=y(z,[G.params.order,G.params.orderBy,H.params.page,H.params.perPage]),Z=0===H.startPage?1:0;i(h,b);var $=c(!Y.disabledOnDidMount),aa=u(!1),ea=r(aa,2),ra=ea[0],ta=ea[1],na=u(!1),oa=r(na,2),pa=oa[0],sa=oa[1],ia=d(A,q?K:{}),ca=r(ia,2),ua=ca[0],da=ca[1],la=d(_,[]),ga=r(la,2),ma=ga[0],fa=ga[1],ya=u(t(t({},G.params.order,q&&z.get(G.params.order)||G.order),G.params.orderBy,q&&z.get(G.params.orderBy)||G.orderBy)),Pa=r(ya,2),va=Pa[0],ha=Pa[1],ba=u(0),Ea=r(ba,2),Ba=Ea[0],ka=Ea[1],xa=u(t(t({},H.params.page,+(q&&z.get(H.params.page)||H.startPage)),H.params.perPage,+(q&&z.get(H.params.perPage)||H.perPage))),Ca=r(xa,2),La=Ca[0],wa=Ca[1],Oa=l((function(){return{from:H.from(Ba,La[H.params.page],La[H.params.perPage],H.startPage),to:H.to(Ba,La[H.params.page],La[H.params.perPage],H.startPage),lastPage:H.lastPage(Ba,La[H.params.perPage])}}),[Ba,La,W]),Ra=Oa.from,Sa=Oa.to,Ta=Oa.lastPage,Aa=g((function(e){da({type:"SET",payload:e}),wa((function(e){return a(a({},e),{},t({},H.params.page,H.startPage))}))}),[ua]),_a=function(){var e=p(s().mark((function e(){var r,t,n,p,i,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(e.prev=0,ta(!0),r=a({},v(a(a(a({},ua),La),va))),t={ctr:j,params:r},n=!1,p="",Q.key&&(p="".concat(Q.key,"._").concat(JSON.stringify(r)),n=E(p)),n){e.next=16;break}return e.next=10,o.service(t);case 10:i=e.sent,ka(Y.total(i)),fa({type:"set",payload:Y.selector(i)}),Q.key&&O({total:Y.total(i),data:Y.selector(i)},p),e.next=19;break;case 16:c=w(p),ka(c.total),fa({type:"set",payload:c.data});case 19:q&&(W.search=new URLSearchParams(r).toString(),window.history.replaceState(null,"",W.href)),e.next=24;break;case 22:e.prev=22,e.t0=e.catch(0);case 24:return e.prev=24,ta(!1),e.finish(24);case 27:case"end":return e.stop()}}),e,null,[[0,22,24,27]])})));return function(){return e.apply(this,arguments)}}();m((function(){var a;return(a=Q).key&&(k(a.key),x(a.ttl),C(a.periodTime),L(a.periodically)),$.current?P(_a,200)():$.current=!0,function(){j.abort()}}),[ua,va,La,pa].concat(n(U.deps)));var Ma=l((function(){return ma}),[ma]),ja=La[H.params.page]+Z,Ua=H.perPageOptions,Na=La[H.params.perPage],Fa="".concat(Ra,"–").concat(Sa," of ").concat(Ba>0?Ba:"more than ".concat(Sa)),Da=va[G.params.order],Ia=va[G.params.orderBy];return{isEmpty:0===ma.length,loading:ra,total:Ba,data:Ma,order:Da,orderBy:Ia,update:function(a,e){if(ma.length>0){var r=ma.findIndex(a);r>-1&&fa({type:"update",index:r,value:e})}},add:function(a){fa({type:"add",value:a,position:arguments.length>1&&void 0!==arguments[1]?arguments[1]:"end"})},destroy:function(a){fa({type:"remove",condition:a})},query:function(){var a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:void 0,e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0;return a?void 0===ua[a]&&void 0!==e?e:ua[a]:ua},setTotal:function(a){ka(a)},setQuery:Aa,onSort:function(a){ha((function(e){return t(t({},G.params.orderBy,a),G.params.order,e[G.params.orderBy]===a&&"desc"===e[G.params.order]?"asc":"desc")}))},reload:function(a){sa((function(a){return!a}))},clear:function(a){var e,r,n={},o="CLEAR";if((null==a||null===(e=a.except)||void 0===e?void 0:e.length)>0?(o="CLEAR_EXCEPT",n=a.except):(null==a||null===(r=a.only)||void 0===r?void 0:r.length)>0&&(o="CLEAR_ONLY",n=a.only),Q.key){var p="".concat(Q.key,"._").concat(JSON.stringify(n));E(p)&&B(p)}da({type:o,payload:n}),wa(t(t({},H.params.page,H.startPage),H.params.perPage,H.perPage)),ha(t(t({},G.params.orderBy,G.orderBy),G.params.order,G.order)),sa((function(a){return!a}))},remove:function(e){var r=arguments.length>1&&void 0!==arguments[1]&&arguments[1];da({type:"REMOVE",payload:e}),r&&wa((function(e){return a(a({},e),{},t({},H.params.page,H.startPage))}))},has:function(a){return ua.hasOwnProperty(a)},cancel:function(){j.abort()},pagination:{total:Ba,page:ja,from:Ra,to:Sa,lastPage:Ta,perPageOptions:Ua,perPage:Na,text:Fa,setPage:function(e){P((function(){wa((function(r){return a(a({},r),{},t({},H.params.page,e))})),da({type:"SET",payload:t({},H.params.page,e)})}),60)()},setPerPage:function(e){P((function(){wa((function(r){return a(a({},r),{},t({},H.params.perPage,e))})),da({type:"SET",payload:t({},H.params.perPage,e)})}),60)()},nextButton:function(){return{disabled:H.disableLast(Ba,La[H.params.page],Ta),onClick:function(e){Ba>0&&La[H.params.page]!==Ta&&wa((function(e){return a(a({},e),{},t({},H.params.page,e[H.params.page]+1))}))}}},backButton:function(){return{disabled:H.disableFirst(Ba,La[H.params.page],H.startPage),onClick:function(e){0!==Ba&&La[H.params.page]>H.startPage&&wa((function(e){return a(a({},e),{},t({},H.params.page,e[H.params.page]-1))}))}}},firstButton:function(){return{disabled:H.disableFirst(Ba,La[H.params.page],H.startPage),onClick:function(e){Ba>0&&La[H.params.page]>H.startPage&&wa((function(e){return a(a({},e),{},t({},H.params.page,H.startPage-Z))}))}}},lastButton:function(e){return{disabled:H.disableLast(Ba,La[H.params.page],Ta),onClick:function(e){Ba>0&&La[H.params.page]!==Ta&&wa((function(e){return a(a({},e),{},t({},H.params.page,Ta-Z))}))}}},onPerPageChange:function(e){wa((function(r){return a(a({},r),{},t({},H.params.perPage,+e.target.value))}))}}}};export{j as default};