UNPKG

2.17 kBJavaScriptView Raw
1import{objectSpread2 as r,typeof as t,slicedToArray as a,toConsumableArray as e,asyncToGenerator as n,regeneratorRuntime as o}from"./_virtual/_rollupPluginBabelHelpers.js";import u from"react";import{skipParams as c}from"./utils/index.js";var s=function(a,n){switch(n.type){case"SET_OBJECT":return"object"===t(n.data)&&null!==n.data?r(r({},a),n.data):r({},a);case"SET_ARRAY":return Array.isArray(n.data)&&null!==n.data?e(n.data):e(a);default:return a}},i=function(e){var i=r({data:{}},e),l=Array.isArray(i.data)||i.data instanceof File?i.data:"object"===t(i.data)?Object.assign({},i.data):i.data,d=new AbortController,f=u.useReducer(s,l),y=a(f,2),p=y[0],A=y[1],v=u.useState({}),m=a(v,2),E=m[0],S=m[1],g=u.useState({loading:!1}),T=a(g,2),b=T[0],R=T[1],_=function(){var t=n(o().mark((function t(a){var n,u,s,i,l;return o().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return n=r({service:function(r){},onSuccess:void 0,onError:function(r){},onAlways:function(){}},a),t.prev=1,u=Array.isArray(e.data)?"SET_ARRAY":"SET_OBJECT",R((function(t){return r(r({},t),{},{loading:!0})})),s={ctr:d,param:c(E)},t.next=7,n.service(s);case 7:return i=t.sent,l=n.onSuccess?n.onSuccess(i):i,A({type:u,data:l}),t.abrupt("return",l);case 13:t.prev=13,t.t0=t.catch(1),n.onError(t.t0);case 16:return t.prev=16,R((function(t){return r(r({},t),{},{loading:!1})})),n.onAlways(),t.finish(16);case 20:case"end":return t.stop()}}),t,null,[[1,13,16,20]])})));return function(r){return t.apply(this,arguments)}}(),h=u.useCallback((function(t){S((function(a){return r(r({},a),"function"==typeof t?t(a):t)}))}),[E]),j=Array.isArray(p)?0===p.length:0===Object.keys(p),w=u.useMemo((function(){return p}),[p]),C=u.useMemo((function(){return E}),[E,p]);return{isEmpty:j,loading:b.loading,data:w,exec:_,setQuery:h,query:function(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"";return C[r]||t||""},clear:function(){var t=arguments.length>0&&void 0!==arguments[0]&&arguments[0],a=Array.isArray(e.data)?"SET_ARRAY":"SET_OBJECT";R((function(t){return r(r({},t),{},{loading:!1})})),S({}),A({type:a,data:t?"SET_ARRAY"===a?[]:{}:l})},cancel:function(){d.abort()}}};export{i as default};