@livelybone/use-async-data
Version:
A react hook for deal data that generated asynchronously
10 lines (8 loc) • 2.19 kB
JavaScript
/**
* Bundle of @livelybone/use-async-data
* Generated: 2020-07-29
* Version: 2.2.2
* License: MIT
* Author: 2631541504@qq.com
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):(t=t||self).useAsyncData=e(t.React)}(this,function(y){"use strict";function m(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){if("undefined"==typeof Symbol||!(Symbol.iterator in Object(t)))return;var n=[],r=!0,u=!1,o=void 0;try{for(var i,c=t[Symbol.iterator]();!(r=(i=c.next()).done)&&(n.push(i.value),!e||n.length!==e);r=!0);}catch(t){u=!0,o=t}finally{try{r||null==c.return||c.return()}finally{if(u)throw o}}return n}(t,e)||function(t,e){if(!t)return;if("string"==typeof t)return r(t,e);var n=Object.prototype.toString.call(t).slice(8,-1);"Object"===n&&t.constructor&&(n=t.constructor.name);if("Map"===n||"Set"===n)return Array.from(t);if("Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))return r(t,e)}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}return function(t,r,u,e){var n=m(y.useState(r),2),o=n[0],i=n[1],c=m(y.useState(!1),2),a=c[0],f=c[1],l=function(t){var e=y.useRef(t);e.current=t;var n=y.useRef({mounted:!1,unmounted:!1});return y.useEffect(function(){n.current.mounted=!0,n.current.unmounted=!1;var t=e.current&&e.current();return function(){"function"==typeof t&&t(),n.current.unmounted=!0}},[]),n}(),s=y.useRef({api:t});s.current.api=t,s.current.dealFn=e&&function(t){return e(t,o)};var d=y.useCallback(function(){var t=s.current,e=t.api,n=t.dealFn;return f(!0),Promise.resolve(e.apply(void 0,arguments)).then(function(t){return n?n(t):t}).then(function(t){return l.current.unmounted||(f(!1),i(t)),t}).catch(function(t){return l.current.unmounted||f(!1),Promise.resolve(u(t)).then(function(t){return t?(l.current.unmounted||i(r),"function"==typeof r?r():r):o})})},[]);return[o,d,a,i]}});