UNPKG

rc-hooks

Version:
3 lines (2 loc) 28.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).rcHooks={},t.React)}(this,function(t,e){"use strict";var n=function(t,e){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},n(t,e)};var r=function(){return r=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var o in e=arguments[n])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},r.apply(this,arguments)};function o(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}function i(t,e,n,r){return new(n||(n=Promise))(function(o,i){function u(t){try{a(r.next(t))}catch(t){i(t)}}function c(t){try{a(r.throw(t))}catch(t){i(t)}}function a(t){var e;t.done?o(t.value):(e=t.value,e instanceof n?e:new n(function(t){t(e)})).then(u,c)}a((r=r.apply(t,e||[])).next())})}function u(t,e){var n,r,o,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]},u=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return u.next=c(0),u.throw=c(1),u.return=c(2),"function"==typeof Symbol&&(u[Symbol.iterator]=function(){return this}),u;function c(c){return function(a){return function(c){if(n)throw new TypeError("Generator is already executing.");for(;u&&(u=0,c[0]&&(i=0)),i;)try{if(n=1,r&&(o=2&c[0]?r.return:c[0]?r.throw||((o=r.return)&&o.call(r),0):r.next)&&!(o=o.call(r,c[1])).done)return o;switch(r=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,r=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=e.call(t,i)}catch(t){c=[6,t],r=0}finally{n=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,a])}}}function c(t,e){var n="function"==typeof Symbol&&t[Symbol.iterator];if(!n)return t;var r,o,i=n.call(t),u=[];try{for(;(void 0===e||e-- >0)&&!(r=i.next()).done;)u.push(r.value)}catch(t){o={error:t}}finally{try{r&&!r.done&&(n=i.return)&&n.call(i)}finally{if(o)throw o.error}}return u}"function"==typeof SuppressedError&&SuppressedError;var a=void 0,s="undefined",f=Object.prototype.toString,l=Number.MAX_SAFE_INTEGER||9007199254740991,h="[object ",p=h+"String]",d=h+"Symbol]",v=["Function","AsyncFunction","GeneratorFunction","Proxy"].map(function(t){return h+t+"]"}),y=h+"Window]";function m(t){return Array.isArray(t)}function g(t){return null!==t&&"object"==typeof t}function b(t){return f.call(t)}var w=/^0b[01]+$/i,k=/^0o[0-7]+$/i,T=/^[-+]0x[0-9a-f]+$/i;function E(t){if("number"==typeof t)return t;if(function(t){return"symbol"==typeof t||b(t)===d}(t))return NaN;if(function(t){return"function"==typeof t||g(t)}(t)&&(t=Number(t)),"string"!=typeof t)return 0===t?t:+t;t=t.trim();var e=w.test(t);return e||k.test(t)?parseInt(t.slice(2),e?2:8):T.test(t)?NaN:+t}function _(t,e,n){return void 0===n&&(n=!1),t===e?!n||(0!==t||1/t==1/e):t!=t&&e!=e}function S(t){return null!=t&&function(t){return"number"==typeof t&&t>-1&&t%1==0&&t<=l}(t.length)&&!function(t){if("function"==typeof t)return!0;var e=b(t);return v.some(function(t){return t===e})}(t)}var O=typeof window!==s&&g(window)&&typeof document!==s&&g(document)&&window.document===document;b(function(){return arguments}());function C(t,e,n,r){if(void 0===r&&(r=!1),"function"!=typeof t)throw new TypeError("Expected a function");var o,i,u,c,s,f,l,h;function p(e){return u=e,f=t.apply(s,c),s=c=a,f}function d(){for(var t=[],l=0;l<arguments.length;l++)t[l]=arguments[l];s=this,c=t;var h=Date.now(),d=function(t){if(i===a)return!0;var n=t-i;return n>=e||n<0||r&&t-u>=e}(h),v=r?d||i===a||o!==a?e:e-(h-i):e;return i=h,d&&n&&o===a?p(h):(o===a||r||(clearTimeout(o),o=a),o===a&&(o=setTimeout(function(){o=a,p(Date.now())},v)),f)}return l=E(e),h=0,e=null==l||l!=l?h:l,d.cancel=function(){o!==a&&(clearTimeout(o),o=a),i=o=c=s=a},d.flush=function(){return o!==a?(clearTimeout(o),o=a,p(Date.now())):f},d.pending=function(){return o!==a},d}function L(t,e,n){return void 0===e&&(e=0),void 0===n&&(n=!1),C(t,e,n)}function R(t,e){var n=!1;return function(){for(var r=[],o=0;o<arguments.length;o++)r[o]=arguments[o];n||(n=!0,t.apply(void 0,function(t,e,n){if(n||2===arguments.length)for(var r,o=0,i=e.length;o<i;o++)!r&&o in e||(r||(r=Array.prototype.slice.call(e,0,o)),r[o]=e[o]);return t.concat(r||Array.prototype.slice.call(e))}([],c(r),!1)),setTimeout(function(){n=!1},e))}}function I(t,e,n){return void 0===e&&(e=0),void 0===n&&(n=!0),C(t,e,n,!0)}function P(t){return g(t)&&b(t)===y}function x(t){return arguments.length?m(t)?t:[t]:[]}function j(){}function N(t){var n=e.useRef(t);return n.current=t,n}function F(t){var n=N(t),r=e.useCallback(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];var r=n.current;return null==r?void 0:r.apply(void 0,t)},[]);return r}var V=function(t,n){var r=e.useRef(!1);e.useEffect(function(){if(r.current)return t();r.current=!0},n)},A=function(){var t=e.useRef(!1);return e.useEffect(function(){return t.current=!1,function(){t.current=!0}},[]),t},M=function(){function t(){this.handlers={}}return t.prototype.eventNames=function(){var t,e=(null===(t=Object.getOwnPropertySymbols)||void 0===t?void 0:t.call(Object,this.handlers))||[];return Object.keys(this.handlers).concat(e)},t.prototype.rawListeners=function(t){var e=this.handlers[t];return e?e.map(function(t){return t.raw}):[]},t.prototype.listeners=function(t){var e=this.handlers[t];return e?e.map(function(t){return t.wrap}):[]},t.prototype.hasListener=function(t,e){return this.rawListeners(t).some(function(t){return t===e})},t.prototype._on=function(t,e,n,r,o){void 0===r&&(r=null),void 0===o&&(o=1);var i={raw:e,wrap:n,context:r};if(this.handlers[t]){var u=1===o?"push":"unshift";this.handlers[t][u](i)}else this.handlers[t]=[i];return this},t.prototype.prependListener=function(t,e,n){return this._on(t,e,e,n,0)},t.prototype.on=function(t,e,n){return this._on(t,e,e,n)},t.prototype._wrapOnce=function(t,e,n){var r=this;void 0===n&&(n=null);var o=function(){for(var i=[],u=0;u<arguments.length;u++)i[u]=arguments[u];e.apply(n,i),r.off(t,o)};return o},t.prototype.once=function(t,e,n){var r=this._wrapOnce(t,e,n);return this._on(t,e,r,n)},t.prototype.prependOnceListener=function(t,e,n){var r=this._wrapOnce(t,e,n);return this._on(t,e,r,n,0)},t.prototype.off=function(t,e){var n=this.handlers[t];if(n)if(e){var r=n.findIndex(function(t){return t.wrap===e||t.raw===e});-1!==r&&n.splice(r,1)}else delete this.handlers[t];return this},t.prototype.offAll=function(){return this.handlers={},this},t.prototype.emit=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];var r=this.handlers[t];return!!(r&&r.length>0)&&(r.forEach(function(t){t.wrap.apply(t.context,e)}),!0)},t}(),D={},W=function(){function t(t){void 0===t&&(t="default"),this.scope=t,D[this.scope]||(D[this.scope]={}),this.data=D[this.scope]}return t.prototype.getItem=function(t){return t in this.data?this.data[t]:null},t.prototype.setItem=function(t,e){this.data[t]=e},t.prototype.removeItem=function(t){delete this.data[t]},t.prototype.clear=function(){D[this.scope]={},this.data=D[this.scope]},t}();function z(t){try{var e="object"==typeof t&&null!==t&&!!t.setItem&&!!t.getItem&&!!t.removeItem;if(e){var n=Math.random().toString(16).substring(2,8)+(new Date).getTime();if(t.setItem(n,"1"),"1"!==t.getItem(n))return!1;t.removeItem(n)}return e}catch(e){return console.error("[cache2] ".concat(t," is not supported. The default memory cache will be used.")),!1}}var K=function(){function t(t,e){void 0===e&&(e={});var n=!!t&&z(t);this.options=r({needParsed:n,prefix:""},e),this.storage=n?t:new W(this.options.memoryScope)}return t.prototype.getKey=function(t){return this.options.prefix+t},t.prototype.get=function(t){var e=this.storage.getItem(this.getKey(t));return this.options.needParsed?function(t,e){try{return JSON.parse(t,e)}catch(e){return t}}(e,this.options.reviver):e},t.prototype.set=function(t,e){this.storage.setItem(this.getKey(t),this.options.needParsed?function(t,e){return JSON.stringify(t,e)}(e,this.options.replacer):e)},t.prototype.del=function(t){this.storage.removeItem(this.getKey(t))},t.prototype.clear=function(){"function"==typeof this.storage.clear&&this.storage.clear()},t}(),H="default",U=function(t){function e(e,n){var o,i=t.call(this)||this,u=H;return"string"==typeof e?u=e||H:"object"==typeof e&&(o=e),o||"object"!=typeof n||(o=n),i.options=r({max:-1,stdTTL:0,maxStrategy:"limited",checkperiod:0,prefix:"cache2_"},o),i.storage=new K(i.options.storage,r({memoryScope:u},i.options)),i.cacheKey=u,i.startCheckperiod(),i}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)}(e,t),e.prototype._check=function(t,e){var n=!0;return 0!==e.t&&e.t<Date.now()&&(n=!1,this.del(t),this.emit("expired",t,e.v)),n},e.prototype._wrap=function(t,e){var n=Date.now(),r="number"==typeof e?e:this.options.stdTTL;return{v:t,t:r>0?n+r:0,n:n}},e.prototype._isLimited=function(t){return this.options.max>-1&&t>=this.options.max},e.prototype._getReplaceKey=function(t,e){var n=t[0];return t.forEach(function(t){(e[t].t<e[n].t||e[t].t===e[n].t&&e[t].n<e[n].n)&&(n=t)}),n},Object.defineProperty(e.prototype,"cacheValues",{get:function(){return this.storage.get(this.cacheKey)||{}},enumerable:!1,configurable:!0}),e.prototype.setCacheValues=function(t){this.storage.set(this.cacheKey,t)},e.prototype.get=function(t){var e=this.cacheValues[t];if(e&&this._check(t,e))return e.v},e.prototype.mget=function(t){var e=this,n={};if(!Array.isArray(t))return n;var r=this.cacheValues;return t.forEach(function(t){var o=r[t];o&&e._check(t,o)&&(n[t]=o.v)}),n},e.prototype.getAll=function(){var t=Object.keys(this.cacheValues);return this.mget(t)},e.prototype.set=function(t,e,n){if(0===this.options.max)return!1;var r=this.cacheValues,o=Object.keys(r);if(!r[t]&&this._isLimited(o.length)){var i=this.keys();if(this._isLimited(i.length)){if("replaced"!==this.options.maxStrategy)return!1;var u=this._getReplaceKey(i,r);this.del(u)}}return r[t]=this._wrap(e,n),this.setCacheValues(r),this.emit("set",t,r[t].v),!0},e.prototype.mset=function(t){var e=this,n=!0;return t.forEach(function(t){var r=e.set(t.key,t.value,t.ttl);n&&!r&&(n=!1)}),n},e.prototype.del=function(t){var e=this,n=this.cacheValues,r=0;return(Array.isArray(t)?t:[t]).forEach(function(t){if(n[t]){r++;var o=n[t];delete n[t],e.emit("del",t,o.v)}}),r>0&&this.setCacheValues(n),r},e.prototype.clear=function(){this.storage.del(this.cacheKey)},e.prototype.keys=function(){var t=this,e=this.cacheValues;return Object.keys(e).filter(function(n){return t._check(n,e[n])})},e.prototype.has=function(t){var e=this.cacheValues[t];return!(!e||!this._check(t,e))},e.prototype.take=function(t){var e,n=this.cacheValues[t];return n&&this._check(t,n)&&(e=n.v,this.del(t)),e},e.prototype.ttl=function(t,e){var n=this.cacheValues,r=n[t];return!(!r||!this._check(t,r))&&(n[t]=this._wrap(r.v,e),!0)},e.prototype.getTtl=function(t){var e=this.cacheValues,n=e[t];if(n&&this._check(t,n))return e[t].t},e.prototype.getLastModified=function(t){var e=this.cacheValues,n=e[t];if(n&&this._check(t,n))return e[t].n},e.prototype.startCheckperiod=function(){var t=this;this.keys(),this.options.checkperiod>0&&(clearTimeout(this._checkTimeout),this._checkTimeout=setTimeout(function(){t.startCheckperiod()},this.options.checkperiod))},e.prototype.stopCheckperiod=function(){clearTimeout(this._checkTimeout)},e}(M),B=function(){function t(t,e){void 0===e&&(e="uh_async_memo"),this.promiseCache={},this.cache=new U(e,t)}return t.prototype.run=function(t,e,n){var o,i=this;if(!e||"string"!=typeof(o=e)&&b(o)!==p)return t();var u=r({persisted:!0},n);if(u.persisted){var c=this.cache.get(e);if(c)return Promise.resolve(c)}return this.promiseCache[e]||(this.promiseCache[e]=t().then(function(t){return delete i.promiseCache[e],i.cache.set(e,t,u.ttl),t}).catch(function(t){return delete i.promiseCache[e],Promise.reject(t)})),this.promiseCache[e]},t}();function G(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}var $=[];var J=!1;if("undefined"!=typeof window&&window.addEventListener&&!J){window.addEventListener("visibilitychange",function(){if(G())for(var t=0;t<$.length;t++){(0,$[t])()}},!1),J=!0}var q=[];var X=!1;if("undefined"!=typeof window&&window.addEventListener&&!X){var Y=function(){if(G()&&("undefined"==typeof navigator||void 0===navigator.onLine||navigator.onLine))for(var t=0;t<q.length;t++){(0,q[t])()}};window.addEventListener("visibilitychange",Y,!1),window.addEventListener("focus",Y,!1),X=!0}var Q=new B({prefix:"rc-hooks",stdTTL:3e5});function Z(t){return Q.cache.get(t)}var tt=function(){function t(t,e){this.params=[],this.pollingWhenVisibleFlag=!1,this.pollingTimer=null,this.counter=1,this.destroyed=!1,this.async=t,this.options=r({cacheTime:3e5,persisted:!1,pollingWhenHidden:!0,refreshOnWindowFocus:!1,focusTimespan:5e3},e),this.unsubscribes=[],this.init()}return t.prototype.init=function(){var t,e=this.options,n=e.pollingInterval,r=e.refreshOnWindowFocus,o=e.focusTimespan;if(this.updateDebounce(),n&&this.unsubscribes.push((t=this.rePolling.bind(this),$.push(t),function(){var e=$.indexOf(t);$.splice(e,1)})),r){var i=R(this.refresh.bind(this),o);this.unsubscribes.push(function(t){return q.push(t),function(){var e=q.indexOf(t);q.splice(e,1)}}(i))}},t.prototype.updateDebounce=function(){var t=this.options,e=t.debounceInterval,n=t.throttleInterval;this.debounce="number"==typeof e&&e>0?L(this._run,e):void 0,this.throttle="number"==typeof n&&n>0?I(this._run,n):void 0},t.prototype.rePolling=function(){this.pollingWhenVisibleFlag&&(this.pollingWhenVisibleFlag=!1,this.refresh())},t.prototype.afterUpdateOptions=function(t,e){void 0===e&&(e={}),("debounceInterval"in e&&e.debounceInterval!==t.debounceInterval||"throttleInterval"in e&&e.throttleInterval!==t.throttleInterval)&&this.updateDebounce()},t.prototype.updateOptions=function(t){var e=this.options,n=r(r({},this.options),t);this.options=n,this.afterUpdateOptions(e,n)},t.prototype._run=function(){for(var t=this,e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];this.pollingTimer&&clearTimeout(this.pollingTimer);var r=this.counter;this.params=e;var o=this.options,i=o.cacheKey,u=o.cacheTime,c=o.persisted,a=o.formatResult,s=o.onSuccess,f=o.onError,l=o.onFinally,h=o.onBefore,p=o.pollingWhenHidden,d=o.pollingInterval;return null==h||h(e),new Promise(function(n,o){Q.run(function(){return t.async.apply(t,e).then(function(t){return"function"==typeof a?a(t,e):t})},i,{persisted:c,ttl:u}).then(function(o){r===t.counter&&(null==s||s(o,e),n(o))}).catch(function(n){r===t.counter&&(null==f||f(n,e),o(n))}).finally(function(){if(r===t.counter&&(null==l||l(),d)){if(!G()&&!p)return void(t.pollingWhenVisibleFlag=!0);t.pollingTimer=setTimeout(function(){t.run.apply(t,e)},d)}})})},t.prototype.run=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return this.debounce?(this.debounce.apply(this,t),Promise.resolve(null)):this.throttle?(this.throttle.apply(this,t),Promise.resolve(null)):(this.counter+=1,this._run.apply(this,t))},t.prototype.refresh=function(){return this.run.apply(this,this.params)},t.prototype.cancel=function(){this.debounce&&this.debounce.cancel(),this.throttle&&this.throttle.cancel(),this.pollingTimer&&(clearTimeout(this.pollingTimer),this.pollingTimer=null),this.counter+=1},t.prototype.destroy=function(t){void 0===t&&(t=!0),t&&this.cancel(),this.destroyed||(this.destroyed=!0,this.unsubscribes.forEach(function(t){return t()}),this.unsubscribes=[])},t.prototype.resume=function(){this.destroyed&&(this.destroyed=!1,this.init())},t}(),et=function(t,n){var o=n||{},i=o.autoRun,u=void 0===i||i,s=o.refreshDeps,f=void 0===s?[]:s,l=o.defaultParams,h=o.loadingDelay,p=o.__INTERNAL_FORMAT__,d=o.defaultLoading,v=o.initialData,y=o.cacheKey,g=void 0===y?"":y,b=o.cacheTime,w=void 0===b?3e5:b,k=o.persisted,T=void 0!==k&&k,E=o.onSuccess,O=void 0===E?j:E,C=o.onError,L=void 0===C?j:C,R=o.onFinally,I=void 0===R?j:R,P=o.onBefore,x=void 0===P?j:P,M=o.pollingInterval,D=void 0===M?0:M,W=o.pollingWhenHidden,z=void 0===W||W,K=o.refreshOnWindowFocus,H=void 0!==K&&K,U=o.focusTimespan,B=void 0===U?5e3:U,G=o.debounceInterval,$=o.throttleInterval,J=c(e.useState(function(){return{params:[],loading:(t=d,!!(t===a?u&&!h:d)),error:null,data:g?Z(g):v};var t}),2),q=J[0],X=J[1],Y=N(q),Q=A(),et=e.useRef(null),nt=F(t),rt=F(O),ot=F(L),it=F(I),ut=F(x),ct=N(p),at=e.useCallback(function(t){ut(t),et.current&&clearTimeout(et.current),(g?Z(g):void 0)&&T||(Y.current.loading===!h&&function(t,e,n){if(void 0===n&&(n=!0),_(t,e,n))return!0;if(!S(t)||!S(e))return!1;if(t.length!==e.length)return!1;for(var r=0;r<t.length;r++)if(!_(t[r],e[r],n))return!1;return!0}(Y.current.params,t)||X(function(e){return r(r({},e),{loading:!h,params:t})}),h&&(et.current=setTimeout(function(){Q.current||X(function(t){return r(r({},t),{loading:!0})})},h)))},[g,Y,h,ut,T,Q]),st=e.useCallback(function(t,e){et.current&&clearTimeout(et.current),X(function(e){return r(r({},e),{data:t,error:null,loading:!1})}),rt(t,e)},[rt]),ft=e.useCallback(function(t,e){et.current&&clearTimeout(et.current),X(function(e){return r(r({},e),{error:t,loading:!1})}),ot(t,e)},[ot]),lt=e.useRef();lt.current||(lt.current=new tt(nt,{cacheKey:g,cacheTime:w,persisted:T,formatResult:ct.current,onSuccess:st,onError:ft,onFinally:it,onBefore:at,debounceInterval:G,throttleInterval:$,pollingInterval:D,pollingWhenHidden:z,refreshOnWindowFocus:H,focusTimespan:B})),V(function(){lt.current.updateOptions({cacheKey:g,cacheTime:w,persisted:T,formatResult:ct.current,onSuccess:st,onError:ft,onFinally:it,onBefore:at,debounceInterval:G,throttleInterval:$,pollingInterval:D,pollingWhenHidden:z,refreshOnWindowFocus:H,focusTimespan:B})},[rt,ot,g,w,T,G,$,it,ut,D,z,H,B,st,ft,at]);var ht=e.useCallback(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return lt.current.run.apply(lt.current,t)},[]),pt=e.useCallback(function(){return lt.current.refresh()},[]),dt=e.useCallback(function(){lt.current.cancel(),et.current&&clearTimeout(et.current),X(function(t){return r(r({},t),{loading:!1})})},[]);return V(function(){u&&m(f)&&f.length>0&&pt()},[u].concat(f)),e.useEffect(function(){if(u){var t=m(l)?l:void 0!==l?[l]:[];ht.apply(void 0,t)}return lt.current.resume(),function(){dt(),lt.current.destroy(!1)}},[]),r(r({},q),{run:ht,cancel:dt,mutate:function(t){X("function"==typeof t?function(e){return r(r({},e),{data:t(q.data)})}:function(e){return r(r({},e),{data:t})})},refresh:pt})};function nt(t){return"function"==typeof t?t():t}var rt=function(t){var n=void 0===t?{}:t,r=n.target,o=n.threshold,i=void 0===o?100:o,u=n.onScrollLower,c=F(void 0===u?function(){}:u),a=e.useCallback(function(){if(r){var t,e=nt(r);if(e)void 0===(t=e)&&(t=window),(O&&t?P(t)?document.documentElement.scrollHeight:t.scrollHeight:0)-function(t){return void 0===t&&(t=window),O&&t?P(t)?window.scrollY:t.scrollTop:0}(e)<=function(t){return void 0===t&&(t=window),O&&t?P(t)?document.documentElement.clientHeight:t.clientHeight:0}(e)+i&&c()}},[c,r,i]);e.useEffect(function(){if(r){var t=nt(r);if(t)return t.addEventListener("scroll",a),function(){t.removeEventListener("scroll",a)}}},[r,a])};var ot=function(t){var n=N(t);e.useEffect(function(){return function(){return n.current()}},[])};function it(t,n,r){void 0===n&&(n=0),void 0===r&&(r=!1);var o=N(t),i=e.useRef(L(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return o.current.apply(void 0,t)},n,r));return ot(function(){i.current.cancel()}),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}function ut(t,n,r){void 0===n&&(n=0),void 0===r&&(r=!0);var o=N(t),i=e.useRef(I(function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return o.current.apply(void 0,t)},n,r));return ot(function(){i.current.cancel()}),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}var ct=function(){var t=e.useRef(!1);return e.useEffect(function(){return t.current=!0,function(){t.current=!1}},[]),t};function at(t){var n=A(),r=c(e.useState(t),2),o=r[0],i=r[1];return[o,e.useCallback(function(t){n.current||i(t)},[])]}function st(t){return"function"==typeof t?t():"object"==typeof t&&t&&"current"in t?t.current:t}var ft=["top","right","bottom","left","width","height","size","weight"],lt={characterData:!0,childList:!0,attributes:!0,subtree:!0},ht=function(t){var e=this;this.refresh=function(){e.targetNode&&e.callback([{target:e.targetNode}],{})},this.onTransitionEnd_=function(t){var n=t.propertyName,r=void 0===n?"":n;ft.some(function(t){return r.indexOf(t)>-1})&&e.throttleRefresh()},this.observe=function(t,n){O&&(e.targetNode=t,document.addEventListener("transitionend",e.onTransitionEnd_),window.addEventListener("resize",e.throttleRefresh),e.observer.observe(document,r(r({},lt),n)))},this.disconnect=function(){O&&(e.throttleRefresh.cancel(),e.targetNode=null,document.removeEventListener("transitionend",e.onTransitionEnd_),window.removeEventListener("resize",e.throttleRefresh),e.observer.disconnect())},this.throttleRefresh=I(this.refresh,50),this.targetNode=null,this.callback=t,this.observer=new MutationObserver(this.throttleRefresh)};t.clearCache=function(t){t?Q.cache.del(t):Q.cache.clear()},t.useAsync=et,t.useClickAway=function(t,n,r){void 0===r&&(r="click");var o=x(t),i=N(o),u=o.every(function(t){return"function"==typeof t}),c=u?i:o,a=N(n),s=x(r),f=N(s),l=s.join("");e.useEffect(function(){var t=function(t){var e;(u?c.current:c).some(function(e){var n=st(e);return!n||(null==n?void 0:n.contains(t.target))})||null===(e=a.current)||void 0===e||e.call(a,t)},e=x(f.current);return e.forEach(function(e){document.addEventListener(e,t)}),function(){e.forEach(function(e){document.removeEventListener(e,t)})}},[c,u,l])},t.useControllableValue=function(t,n){void 0===t&&(t={}),void 0===n&&(n={});var r=n.defaultValue,o=n.defaultValuePropName,i=void 0===o?"defaultValue":o,u=n.valuePropName,a=void 0===u?"value":u,s=n.trigger,f=void 0===s?"onChange":s,l=a in t,h=t[a],p=c(e.useState(function(){return l?h:i in t?t[i]:r}),2),d=p[0],v=p[1];V(function(){l&&v(h)},[h,l]);var y=e.useCallback(function(e){for(var n=[],r=1;r<arguments.length;r++)n[r-1]=arguments[r];l||v(e),t[f]&&t[f].apply(t,[e].concat(n))},[l,t,f]);return[l?h:d,y]},t.useDebounce=function(t,n,r){void 0===n&&(n=0),void 0===r&&(r=!1);var o=c(e.useState(t),2),i=o[0],u=it(o[1],n,r).run;return e.useEffect(function(){u(t)},[u,t]),i},t.useDebounceFn=it,t.useLatest=N,t.useLimitList=function(t,n){void 0===t&&(t=[]),void 0===n&&(n={});var r=n||{},o=r.count,i=void 0===o?3:o,u=r.defaultLimited,a=void 0===u||u,s=e.useRef(a),f=e.useMemo(function(){return m(t)?t:[]},[t]),l=e.useMemo(function(){return i>0?Math.ceil(i):0},[i]),h=e.useMemo(function(){return f.length>l},[l,f.length]),p=c(e.useState(function(){return h&&a?f.slice(0,l):f}),2),d=p[0],v=p[1];return e.useEffect(function(){v(s.current?f.slice(0,l):f)},[l,f]),{canLimit:h,limited:s.current,data:d,toggle:function(){h&&(s.current=!s.current),v(s.current?f.slice(0,l):f)}}},t.useLoadMore=function(t,n){var a=this,s=n||{},f=s.threshold,l=void 0===f?100:f,h=s.target,p=s.isNoMore,d=void 0===p?function(){return!1}:p,v=s.refreshDeps,y=void 0===v?[]:v,g=s.autoRun,b=void 0===g||g,w=o(s,["threshold","target","isNoMore","refreshDeps","autoRun"]),k=e.useRef([]),T=e.useRef(1),E=et(t,r(r({defaultParams:[{current:T.current}],autoRun:b},w),{onError:function(t,e){var n;T.current>1&&(T.current-=1),null===(n=null==w?void 0:w.onError)||void 0===n||n.call(w,t,e)},__INTERNAL_FORMAT__:function(t){return k.current=1===T.current?t.list:k.current.concat(t.list),r(r({},t),{list:k.current})}})),_=E.run,S=E.data,O=E.loading,C=E.cancel,L=E.params,R=E.mutate,I=o(E,["run","data","loading","cancel","params","mutate"]),P=!!d&&(!O&&d(S)),x=c(L||[]),j=x[0],N=x.slice(1),F=e.useCallback(function(){return _.apply(void 0,[{current:T.current}].concat(N))},[N,_]),A=e.useCallback(function(){O&&T.current>1&&(T.current-=1),C()},[C,O]),M=e.useCallback(function(){return i(a,void 0,void 0,function(){return u(this,function(t){return O||P?[2]:(T.current+=1,[2,F()])})})},[O,P,F]),D=e.useCallback(function(t){var e="function"==typeof t?t(S):t;k.current=(null==e?void 0:e.list)||[],R(e)},[S,R]),W=e.useCallback(function(){return A(),T.current=1,D(function(t){return r(r({},t),{list:[]})}),F()},[A,F,D]),z=e.useCallback(function(){if(!O&&h)return M()},[M,h,O]);return rt({target:h,threshold:l,onScrollLower:z}),V(function(){b&&m(y)&&y.length>0&&W()},y),r(r({},I),{loading:O,data:S,run:_,refresh:W,cancel:A,mutate:D,params:[r(r({},j),{current:T.current})].concat(N),loadMore:M,loadingMore:O&&T.current>1,noMore:P})},t.useMount=function(t){e.useEffect(function(){t()},[])},t.useMountedRef=ct,t.usePagination=function(t,n){var i=n||{},u=i.defaultPageSize,a=void 0===u?10:u,s=i.refreshDeps,f=void 0===s?[]:s,l=i.defaultParams,h=i.autoRun,p=void 0===h||h,d=o(i,["defaultPageSize","refreshDeps","defaultParams","autoRun"]),v=e.useMemo(function(){return l||[{current:1,pageSize:a}]},[a,l]),y=et(t,r({defaultParams:v,autoRun:p},d)),g=y.run,b=y.data,w=y.params,k=y.loading,T=o(y,["run","data","params","loading"]),E=w&&w[0]?w[0]:v[0],_=E.current,S=E.pageSize,O=(null==b?void 0:b.total)||0,C=e.useCallback(function(t){var e=c(w),n=e[0],o=e.slice(1);return g.apply(void 0,[r(r({},n),t)].concat(o))},[w,g]),L=e.useCallback(function(){return C({current:_,pageSize:S})},[C,_,S]),R=e.useCallback(function(t,e){var n=t<=0?1:t,r=e<=0?1:e,o=Math.ceil(O/r);n>o&&o>0&&(n=o),C({current:n,pageSize:r})},[C,O]),I=e.useCallback(function(t){R(t,S)},[R,S]),P=e.useCallback(function(t){R(_,t)},[_,R]),x=e.useCallback(function(t,e,n,r){C({current:null==t?void 0:t.current,pageSize:null==t?void 0:t.pageSize,filters:e,sorter:n,extra:r})},[C]);return V(function(){p&&m(f)&&f.length>0&&I(1)},f),r(r({},T),{data:b,run:g,refresh:L,loading:k,params:w,pagination:{current:_,pageSize:S,total:O,onChange:R,changeCurrent:I,changePageSize:P},tableProps:{dataSource:(null==b?void 0:b.list)||[],loading:k,onChange:x,pagination:{total:O,current:_,pageSize:S}}})},t.usePersistFn=F,t.usePrevious=function(t){var n=ct(),r=e.useRef(),o=e.useRef(t);return n.current&&o.current!==t&&(r.current=o.current,o.current=t),r.current},t.useSafeState=at,t.useSetState=function(t){var n=c(e.useState(t),2),o=n[0],i=n[1];return[o,e.useCallback(function(t){i(function(e){var n=t instanceof Function?t(e):t;return n instanceof Object?r(r({},e),n):e})},[])]},t.useSize=function(t){var n=N(t),r="function"==typeof t,o=r?n:t,i=c(e.useState(function(){var e=st(t);return{width:null==e?void 0:e.clientWidth,height:null==e?void 0:e.clientHeight}}),2),u=i[0],a=i[1];return e.useEffect(function(){var t=st(r?o.current:o);function e(t){t&&a({width:t.clientWidth,height:t.clientHeight})}if(!t)return function(){};e(t);var n=new ht(function(t){t.forEach(function(t){e(t.target)})});return n.observe(t),function(){n.disconnect()}},[o,r]),u},t.useThrottle=function(t,n,r){void 0===n&&(n=0),void 0===r&&(r=!0);var o=c(e.useState(t),2),i=o[0],u=ut(o[1],n,r).run;return e.useEffect(function(){u(t)},[u,t]),i},t.useThrottleFn=ut,t.useUnmount=ot,t.useUnmountedRef=A,t.useUpdate=function(){var t=c(at(0),2)[1];return e.useCallback(function(){t(function(t){return t+1})},[])},t.useUpdateEffect=V,t.useUpdateLayoutEffect=function(t,n){var r=e.useRef(!1);e.useLayoutEffect(function(){if(r.current)return t();r.current=!0},n)}}); //# sourceMappingURL=rcHooks.min.js.map