1 | import{objectSpread2 as e,slicedToArray as r,toConsumableArray as t,asyncToGenerator as n,regeneratorRuntime as a}from"./_virtual/_rollupPluginBabelHelpers.js";import o from"react";import{set as i,debounce as u,pick as c,omit as s,skipParams as l}from"./utils/index.js";import{subscribe as y,getSnapshot as d,has as f,clear as p,setKey as v,setTTL as m,setPeriodTime as h,setPeriodically as g,get as A,add as b}from"./garbage.js";var x=function(r,n){switch(n.type){case"set":return Array.isArray(r)?t(n.payload):e(e({},r),n.payload);case"update":var a=r[n.index];return r[n.index]=e(e({},a),n.value),t(r);case"add":return"number"==typeof n.position?r.splice(n.position,0,n.value):"start"===n.position?r.unshift(n.value):r.push(n.value),t(r);case"remove":return r.splice(r.findIndex(n.condition),1),t(r);default:return Array.isArray(r)?t(r):e(e({},r),n.payload)}},k=function(k){var w=new AbortController,D=e({key:"",ttl:3600,periodTime:6e4,periodically:!1},k.cache);o.useSyncExternalStore(y,d);var S=o.useRef(!k.disabledOnDidMount),j=o.useState({}),E=r(j,2),O=E[0],T=E[1],_=o.useState(!1),C=r(_,2),M=C[0],R=C[1],I=o.useState(!1),J=r(I,2),N=J[0],P=J[1],Q=o.useReducer(x,(null==k?void 0:k.defaultValue)||{}),q=r(Q,2),B=q[0],G=q[1],H=o.useCallback((function(t){T("function"==typeof t?function(r){return e({},t(r))}:function(n){for(var a=0,o=Object.entries(t);a<o.length;a++){var u=r(o[a],2),c=u[0],s=u[1];i(n,s,c)}return e({},n)})}),[O]),V=function(){var e=n(a().mark((function e(){var r,t,n,o,i;return a().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(e.prev=0,P(!0),r={ctr:w,params:l(O)},t=!1,n="",D.key&&(n="".concat(D.key,"._").concat(JSON.stringify(l(O))),t=f(n)),t){e.next=15;break}return e.next=9,k.service(r);case 9:o=e.sent,G({type:"set",payload:k&&k.selector(o)||o||{}}),D.key&&b(k&&k.selector(o)||o||{},n),k.getData&&k.getData(k&&k.selector(o)||o||{}),e.next=18;break;case 15:i=A(n),G({type:"set",payload:i}),k.getData&&k.getData(A(n));case 18:e.next=22;break;case 20:e.prev=20,e.t0=e.catch(0);case 22:return e.prev=22,P(!1),e.finish(22);case 25:case"end":return e.stop()}}),e,null,[[0,20,22,25]])})));return function(){return e.apply(this,arguments)}}();o.useEffect((function(){var e;return(e=D).key&&(v(e.key),m(e.ttl),h(e.periodTime),g(e.periodically)),S.current?u(V,k.debounceTime||100)():S.current=!0,function(){w.abort()}}),[O,M].concat(t((null==k?void 0:k.deps)||[])));var z=o.useMemo((function(){return B}),[B]),F=o.useMemo((function(){return O}),[O]);return{isEmpty:Array.isArray(B)?0===B.length:0===Object.keys(B),loading:N,data:z,query:F,update:function(e,r){if(!Array.isArray(B))throw new Error("Data must Array");if(B.length>0){var t=B.findIndex(e);t>-1&&G({type:"update",index:t,value:r})}},add:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"end";if(!Array.isArray(B))throw new Error("Data must Array");G({type:"add",position:r,value:e})},destroy:function(e){if(!Array.isArray(B))throw new Error("Data must Array");G({type:"remove",condition:e})},getQuery:function(e){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0;return void 0===O[e]&&void 0!==r?r:O[e]},setQuery:H,clear:function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:void 0;R((function(e){return!e}));var t=r&&r.except?c(O,r.except):r&&r.only?s(O,r.only):{};if(D.key){var n="".concat(D.key,"._").concat(JSON.stringify(t));f(n)&&p(n)}T(e({},t))},refresh:function(){R((function(e){return!e}))},has:function(e){return O.hasOwnProperty(e)},cancel:function(){w.abort()}}};export{k as default};
|