react-async-hook
Version:
Async hook
3 lines (2 loc) • 3.49 kB
JavaScript
var e=require("react");function r(){return(r=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var t="undefined"!=typeof window&&void 0!==window.document&&void 0!==window.document.createElement?e.useLayoutEffect:e.useEffect,n={status:"not-requested",loading:!1,result:void 0,error:void 0},u={status:"loading",loading:!0,result:void 0,error:void 0},o=function(){},c={initialState:function(e){return e&&e.executeOnMount?u:n},executeOnMount:!0,executeOnUpdate:!0,setLoading:function(e){return u},setResult:function(e,r){return{status:"success",loading:!1,result:e,error:void 0}},setError:function(e,r){return{status:"error",loading:!1,result:void 0,error:e}},onSuccess:o,onError:o},i=function(n,u,o){!u&&(u=[]);var i,s=function(e){return r({},c,{},e)}(o),a=e.useState(null),l=a[0],f=a[1],d=function(t){var n=e.useState(function(){return t.initialState(t)}),u=n[0],o=n[1],c=e.useCallback(function(){return o(t.initialState(t))},[o,t]),i=e.useCallback(function(){return o(t.setLoading(u))},[u,o]),s=e.useCallback(function(e){return o(t.setResult(e,u))},[u,o]),a=e.useCallback(function(e){return o(t.setError(e,u))},[u,o]),l=e.useCallback(function(e){return o(r({},u,{},e))},[u,o]);return{value:u,set:o,merge:l,reset:c,setLoading:i,setResult:s,setError:a}}(s),v=(i=e.useRef(!1),e.useEffect(function(){return i.current=!0,function(){i.current=!1}},[]),function(){return i.current}),y=function(){var r=e.useRef(null);return{set:function(e){return r.current=e},get:function(){return r.current},is:function(e){return r.current===e}}}(),b=function(e){return v()&&y.is(e)},m=function(r){var n=e.useRef(r);return t(function(){n.current=r}),e.useCallback(function(){return n.current},[n])}(function(){for(var e=arguments.length,r=new Array(e),t=0;t<e;t++)r[t]=arguments[t];var u=function(){try{return Promise.resolve(n.apply(void 0,r))}catch(e){return Promise.reject(e)}}();return f(r),y.set(u),d.setLoading(),u.then(function(e){b(u)&&d.setResult(e),s.onSuccess(e,{isCurrent:function(){return y.is(u)}})},function(e){b(u)&&d.setError(e),s.onError(e,{isCurrent:function(){return y.is(u)}})}),u}),p=e.useCallback(function(){return m().apply(void 0,arguments)},[m]),g=!v();return e.useEffect(function(){var e=function(){return m().apply(void 0,u)};g&&s.executeOnMount&&e(),!g&&s.executeOnUpdate&&e()},u),r({},d.value,{set:d.set,merge:d.merge,reset:d.reset,execute:p,currentPromise:y.get(),currentParams:l})};function s(e,r,t){return i(e,r,t)}exports.useAsync=s,exports.useAsyncAbortable=function(r,t,n){var u=e.useRef();return s(function(){for(var e=arguments.length,t=new Array(e),n=0;n<e;n++)t[n]=arguments[n];try{u.current&&u.current.abort();var o=new AbortController;return u.current=o,Promise.resolve(function(e,n){try{var u=Promise.resolve(r.apply(void 0,[o.signal].concat(t)))}catch(e){return n(!0,e)}return u&&u.then?u.then(n.bind(null,!1),n.bind(null,!0)):n(!1,value)}(0,function(e,r){if(u.current===o&&(u.current=void 0),e)throw r;return r}))}catch(e){return Promise.reject(e)}},t,n)},exports.useAsyncCallback=function(e,t){return i(e,[],r({},t,{executeOnMount:!1,executeOnUpdate:!1}))};
//# sourceMappingURL=react-async-hook.cjs.production.min.js.map
;