Comparing version 1.8.4 to 2.0.0
@@ -15,3 +15,3 @@ !function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self).rcHooks={},e.React)}(this,(function(e,n){"use strict";function t(e){if(e&&e.__esModule)return e;var n=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var r=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(n,t,r.get?r:{enumerable:!0,get:function(){return e[t]}})}})),n.default=e,Object.freeze(n)}var r=t(n),u=function(){return u=Object.assign||function(e){for(var n,t=1,r=arguments.length;t<r;t++)for(var u in n=arguments[t])Object.prototype.hasOwnProperty.call(n,u)&&(e[u]=n[u]);return e},u.apply(this,arguments)}; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */function i(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.indexOf(r)<0&&(t[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(e);u<r.length;u++)n.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(e,r[u])&&(t[r[u]]=e[r[u]])}return t}function o(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,u,i=t.call(e),o=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)o.push(r.value)}catch(e){u={error:e}}finally{try{r&&!r.done&&(t=i.return)&&t.call(i)}finally{if(u)throw u.error}}return o}function c(e,n,t){if(t||2===arguments.length)for(var r,u=0,i=n.length;u<i;u++)!r&&u in n||(r||(r=Array.prototype.slice.call(n,0,u)),r[u]=n[u]);return e.concat(r||Array.prototype.slice.call(n))}var f="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var l=function(e){var n=typeof e;return null!=e&&("object"==n||"function"==n)},s="object"==typeof f&&f&&f.Object===Object&&f,d="object"==typeof self&&self&&self.Object===Object&&self,v=s||d||Function("return this")(),p=v,h=function(){return p.Date.now()},y=/\s/;var g=function(e){for(var n=e.length;n--&&y.test(e.charAt(n)););return n},m=/^\s+/;var b=function(e){return e?e.slice(0,g(e)+1).replace(m,""):e},w=v.Symbol,E=w,R=Object.prototype,S=R.hasOwnProperty,T=R.toString,O=E?E.toStringTag:void 0;var C=function(e){var n=S.call(e,O),t=e[O];try{e[O]=void 0;var r=!0}catch(e){}var u=T.call(e);return r&&(n?e[O]=t:delete e[O]),u},j=Object.prototype.toString;var k=C,L=function(e){return j.call(e)},x=w?w.toStringTag:void 0;var P=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":x&&x in Object(e)?k(e):L(e)},M=function(e){return null!=e&&"object"==typeof e};var z=b,D=l,N=function(e){return"symbol"==typeof e||M(e)&&"[object Symbol]"==P(e)},A=/^[-+]0x[0-9a-f]+$/i,H=/^0b[01]+$/i,W=/^0o[0-7]+$/i,_=parseInt;var I=l,U=h,F=function(e){if("number"==typeof e)return e;if(N(e))return NaN;if(D(e)){var n="function"==typeof e.valueOf?e.valueOf():e;e=D(n)?n+"":n}if("string"!=typeof e)return 0===e?e:+e;e=z(e);var t=H.test(e);return t||W.test(e)?_(e.slice(2),t?2:8):A.test(e)?NaN:+e},V=Math.max,$=Math.min;var q=function(e,n,t){var r,u,i,o,a,c,f=0,l=!1,s=!1,d=!0;if("function"!=typeof e)throw new TypeError("Expected a function");function v(n){var t=r,i=u;return r=u=void 0,f=n,o=e.apply(i,t)}function p(e){return f=e,a=setTimeout(y,n),l?v(e):o}function h(e){var t=e-c;return void 0===c||t>=n||t<0||s&&e-f>=i}function y(){var e=U();if(h(e))return g(e);a=setTimeout(y,function(e){var t=n-(e-c);return s?$(t,i-(e-f)):t}(e))}function g(e){return a=void 0,d&&r?v(e):(r=u=void 0,o)}function m(){var e=U(),t=h(e);if(r=arguments,u=this,c=e,t){if(void 0===a)return p(c);if(s)return clearTimeout(a),a=setTimeout(y,n),v(c)}return void 0===a&&(a=setTimeout(y,n)),o}return n=F(n)||0,I(t)&&(l=!!t.leading,i=(s="maxWait"in t)?V(F(t.maxWait)||0,n):i,d="trailing"in t?!!t.trailing:d),m.cancel=function(){void 0!==a&&clearTimeout(a),f=0,r=c=u=a=void 0},m.flush=function(){return void 0===a?o:g(U())},m},B=q,K=l;var Y=function(e,n,t){var r=!0,u=!0;if("function"!=typeof e)throw new TypeError("Expected a function");return K(t)&&(r="leading"in t?!!t.leading:r,u="trailing"in t?!!t.trailing:u),B(e,n,{leading:r,maxWait:n,trailing:u})};function G(e){var t=n.useRef(e);t.current=e;var r=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=t.current;return null==r?void 0:r.apply(void 0,c([],a(e),!1))}),[t]);return r}var J=function(e,t){var r=n.useRef(!1);n.useEffect((function(){if(r.current)return e();r.current=!0}),t)};function Q(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}var X={},Z=function(e){var n=X[e];if(n){var t=n.startTime,r=n.cacheTime,u=n.data;if(!((new Date).getTime()-t>=r))return u;n.timer&&(clearTimeout(n.timer),n.timer=null,delete X[e])}},ee=function(e,n,t){void 0===t&&(t=3e5),X[e]&&clearTimeout(X[e].timer);var r=setTimeout((function(){delete X[e]}),t);X[e]={data:n,timer:r,cacheTime:t,startTime:(new Date).getTime()}};var ne=[];var te=!1;if("undefined"!=typeof window&&window.addEventListener&&!te){var re=function(){if(Q()&&("undefined"==typeof navigator||void 0===navigator.onLine||navigator.onLine))for(var e=0;e<ne.length;e++){(0,ne[e])()}};window.addEventListener("visibilitychange",re,!1),window.addEventListener("focus",re,!1),te=!0}var ue=[];var ie=!1;if("undefined"!=typeof window&&window.addEventListener&&!ie){window.addEventListener("visibilitychange",(function(){if(Q())for(var e=0;e<ue.length;e++){(0,ue[e])()}}),!1),ie=!0}var oe=function(){};function ae(e,t){var r=t||{},i=r.autoRun,o=void 0===i||i,f=r.refreshDeps,l=void 0===f?[]:f,s=r.defaultParams,d=void 0===s?[]:s,v=r.defaultLoading,p=void 0!==v&&v,h=r.initialData,y=r.cacheKey,g=void 0===y?"":y,m=r.cacheTime,b=void 0===m?3e5:m,w=r.persisted,E=void 0!==w&&w,R=r.onSuccess,S=void 0===R?oe:R,T=r.onError,O=void 0===T?oe:T,C=r.formatResult,j=r.pollingInterval,k=void 0===j?0:j,L=r.pollingWhenHidden,x=void 0===L||L,P=r.refreshOnWindowFocus,M=void 0!==P&&P,z=r.focusTimespan,D=void 0===z?5e3:z,N=r.loadingDelay,A=r.debounceInterval,H=r.throttleInterval,W=a(n.useState({params:Array.isArray(d)?d:[d],loading:p,error:null,data:g&&Z(g)||h}),2),_=W[0],I=W[1],U=n.useRef(0),F=n.useRef(null),V=n.useRef(!1),$=n.useRef(null),B=n.useRef(!1),K=n.useRef([]),X=G(e),te=G(S),re=G(O),ie=n.useRef(C);ie.current=C;var ae=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];F.current&&clearTimeout(F.current),$.current&&clearTimeout($.current),U.current+=1;var t=U.current,r=g?Z(g):void 0;r&&E||(I((function(n){return u(u({},n),{loading:!N,params:e})})),$.current=N?setTimeout((function(){I((function(e){return u(u({},e),{loading:!0})}))}),N):null);var i=function(){return new Promise((function(n,i){r&&E?B.current||t!==U.current||(te(r,e),n(r)):X.apply(void 0,c([],a(e),!1)).then((function(r){if(!B.current&&t===U.current){$.current&&clearTimeout($.current);var i="function"==typeof ie.current?ie.current(r,e):r;I((function(e){return u(u({},e),{data:i,error:null,loading:!1})})),g&&ee(g,i,b),te(i,e),n(i)}})).catch((function(n){B.current||t!==U.current||($.current&&clearTimeout($.current),I((function(e){return u(u({},e),{error:n,loading:!1})})),re(n,e),i(n))}))}))};return i().finally((function(){if(!B.current&&t===U.current&&k){if(!Q()&&!x)return void(V.current=!0);F.current=setTimeout((function(){le.apply(void 0,c([],a(e),!1))}),k)}}))}),[]),ce=n.useRef(A?q(ae,A):void 0),fe=n.useRef(H?Y(ae,H):void 0),le=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return ce.current?(ce.current.apply(ce,c([],a(e),!1)),Promise.resolve(null)):fe.current?(fe.current.apply(fe,c([],a(e),!1)),Promise.resolve(null)):ae.apply(void 0,c([],a(e),!1))}),[ae]),se=n.useCallback((function(){return le.apply(void 0,c([],a(_.params),!1))}),[le,_.params]),de=n.useCallback((function(){V.current&&(V.current=!1,se())}),[se]),ve=n.useCallback((function(){ce.current&&ce.current.cancel(),fe.current&&fe.current.cancel(),F.current&&clearTimeout(F.current),$.current&&clearTimeout($.current),V.current=!1,U.current+=1,I((function(e){return u(u({},e),{loading:!1})}))}),[]);J((function(){o&&se()}),c([],a(l),!1));return n.useEffect((function(){o&&le.apply(void 0,c([],a(_.params),!1));var e,n,t,r,u=K.current;if(k&&u.push((e=de,ue.push(e),function(){var n=ue.indexOf(e);ue.splice(n,1)})),M){var i=(n=se,t=D,r=!1,function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];r||(r=!0,n.apply(void 0,c([],a(e),!1)),setTimeout((function(){r=!1}),t))});u.push(function(e){return ne.push(e),function(){var n=ne.indexOf(e);ne.splice(n,1)}}(i))}return function(){B.current=!0,ve(),u.forEach((function(e){return e()}))}}),[]),u(u({},_),{run:le,cancel:ve,mutate:function(e){I("function"==typeof e?function(n){return u(u({},n),{data:e(_.data)})}:function(n){return u(u({},n),{data:e})})},refresh:se})}var ce=function(e){return null!=e&&e===(null==e?void 0:e.window)},fe=function(e){var t=void 0===e?{}:e,r=t.ref,u=t.threshold,i=void 0===u?100:u,o=t.onScrollLower,a=G(void 0===o?function(){}:o),c=n.useCallback((function(){var e;(null==r?void 0:r.current)&&(void 0===(e=r.current)&&(e=window),("undefined"==typeof window?0:ce(e)?document.documentElement.scrollHeight:e.scrollHeight)-function(e){var n;return void 0===e&&(e=window),"undefined"==typeof window?0:ce(e)?window.scrollY:(null===(n=e)||void 0===n?void 0:n.scrollTop)||0}(r.current)<=function(e){return void 0===e&&(e=window),"undefined"==typeof window?0:ce(e)?document.documentElement.clientHeight:e.clientHeight}(r.current)+i&&a())}),[a,r,i]);n.useEffect((function(){var e=null==r?void 0:r.current;if(e)return e.addEventListener("scroll",c),function(){e.removeEventListener("scroll",c)}}),[r,c])};var le=function(e){var t=n.useRef(e);t.current=e,n.useEffect((function(){return function(){return t.current()}}),[])};function se(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=n.useRef(e);u.current=e;var i=n.useRef(q((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return u.current.apply(u,c([],a(e),!1))}),t,r));return le((function(){i.current.cancel()})),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}function de(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=n.useRef(e);u.current=e;var i=n.useRef(Y((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return u.current.apply(u,c([],a(e),!1))}),t,r));return le((function(){i.current.cancel()})),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}var ve=function(){var e=r.useRef(!1);return r.useEffect((function(){return e.current=!1,function(){e.current=!0}}),[]),e};function pe(e){return"function"==typeof e?e():e&&"current"in e?e.current:e}var he="undefined"!=typeof window&&"undefined"!=typeof document&&window.document===document,ye=["top","right","bottom","left","width","height","size","weight"],ge={characterData:!0,childList:!0,attributes:!0,subtree:!0},me=function(e){var n=this;this.refresh=function(){n.targetNode&&n.callback([{target:n.targetNode}],{})},this.onTransitionEnd_=function(e){var t=e.propertyName,r=void 0===t?"":t;ye.some((function(e){return r.indexOf(e)>-1}))&&n.throttleRefresh()},this.observe=function(e,t){he&&(n.targetNode=e,document.addEventListener("transitionend",n.onTransitionEnd_),window.addEventListener("resize",n.throttleRefresh),n.observer.observe(document,u(u({},ge),t)))},this.disconnect=function(){he&&(n.throttleRefresh.cancel(),n.targetNode=null,document.removeEventListener("transitionend",n.onTransitionEnd_),window.removeEventListener("resize",n.throttleRefresh),n.observer.disconnect())},this.throttleRefresh=Y(this.refresh,50),this.targetNode=null,this.callback=e,this.observer=new MutationObserver(this.throttleRefresh)};e.useAsync=ae,e.useClickAway=function(e,n,t){void 0===t&&(t="click");var u=r.useRef(n);u.current=n,r.useEffect((function(){var n,r,i=function(n){var t;(Array.isArray(e)?e:[e]).some((function(e){var t=pe(e);return!t||(null==t?void 0:t.contains(n.target))}))||null===(t=u.current)||void 0===t||t.call(u,n)},a=Array.isArray(t)?t:[t];try{for(var c=o(a),f=c.next();!f.done;f=c.next()){var l=f.value;document.addEventListener(l,i)}}catch(e){n={error:e}}finally{try{f&&!f.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}return function(){var e,n;try{for(var t=o(a),r=t.next();!r.done;r=t.next()){var u=r.value;document.removeEventListener(u,i)}}catch(n){e={error:n}}finally{try{r&&!r.done&&(n=t.return)&&n.call(t)}finally{if(e)throw e.error}}}}),[e,t])},e.useClientRect=function(){var e=n.useRef(null),t=a(n.useState(),2),r=t[0],u=t[1];return n.useEffect((function(){var n;e.current&&u(null===(n=e.current)||void 0===n?void 0:n.getBoundingClientRect())}),[e]),[r,e]},e.useControllableValue=function(e,t){void 0===e&&(e={}),void 0===t&&(t={});var r=t.defaultValue,u=t.defaultValuePropName,i=void 0===u?"defaultValue":u,o=t.valuePropName,f=void 0===o?"value":o,l=t.trigger,s=void 0===l?"onChange":l,d=f in e,v=e[f],p=a(n.useState((function(){return d?v:i in e?e[i]:r})),2),h=p[0],y=p[1];J((function(){d&&y(v)}),[v,d]);var g=n.useCallback((function(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];d||y(n),e[s]&&e[s].apply(e,c([n],a(t),!1))}),[d,e,s]);return[d?v:h,g]},e.useDebounce=function(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=a(n.useState(e),2),i=u[0],o=se(u[1],t,r).run;return n.useEffect((function(){o(e)}),[o,e]),i},e.useDebounceFn=se,e.useIsMounted=function(){var e=n.useRef(!1);return n.useEffect((function(){return e.current=!0,function(){e.current=!1}}),[]),e},e.useLimitList=function(e,t){void 0===t&&(t={});var r=t||{},u=r.count,i=void 0===u?3:u,o=r.defaultLimited,c=void 0===o||o,f=n.useRef(e);f.current=Array.isArray(e)?e:[];var l=n.useMemo((function(){return i<0?0:Math.ceil(i)}),[i]),s=f.current.length>l,d=a(n.useState((function(){return s&&c?f.current.slice(0,l):f.current})),2),v=d[0],p=d[1],h=s&&v.length!==f.current.length,y=n.useCallback((function(){p(h?f.current:f.current.slice(0,l))}),[l,h]);return n.useEffect((function(){p(h?f.current.slice(0,l):f.current)}),[l,e]),{canLimit:s,limited:h,data:v,toggle:y}},e.useLoadMore=function(e,n){var t=n||{},o=t.threshold,f=void 0===o?100:o,l=t.ref,s=t.isNoMore,d=void 0===s?function(){return!1}:s,v=t.refreshDeps,p=void 0===v?[]:v,h=t.formatResult,y=i(t,["threshold","ref","isNoMore","refreshDeps","formatResult"]),g=r.useRef([]),m=r.useRef(1),b=ae(e,u(u({defaultParams:[{current:m.current,data:null==n?void 0:n.initialData}]},y),{onError:function(e,n){var t;m.current>1&&(m.current-=1),null===(t=null==y?void 0:y.onError)||void 0===t||t.call(y,e,n)},formatResult:function(e,n){var t=h?h(e,n):e;return g.current=1===m.current?t.list:g.current.concat(t.list),u(u({},t),{list:g.current})}})),w=b.run,E=b.loading,R=b.cancel,S=b.data,T=b.params,O=b.mutate,C=i(b,["run","loading","cancel","data","params","mutate"]),j=!!d&&(!E&&d(S)),k=r.useCallback((function(){var e=a(T).slice(1);return w.apply(void 0,c([{current:m.current,data:S}],a(e),!1))}),[S,T,w]),L=r.useCallback((function(){E&&m.current>1&&(m.current-=1),R()}),[R,E]),x=r.useCallback((function(){E||j||(m.current+=1,k())}),[E,j,k]),P=r.useCallback((function(e){var n="function"==typeof e?e(S):e;g.current=(null==n?void 0:n.list)||[],O(n)}),[S,O]),M=r.useCallback((function(){return L(),m.current=1,P((function(e){return u(u({},e),{list:[]})})),k()}),[L,k,P]),z=r.useCallback((function(){if(!E&&(null==l?void 0:l.current))return x()}),[x,l,E]);return fe({ref:l,threshold:f,onScrollLower:z}),J((function(){!1!==(null==n?void 0:n.autoRun)&&M()}),p),u(u({},C),{loading:E,data:S,run:w,refresh:M,cancel:L,mutate:P,params:T,loadMore:x,loadingMore:E&&m.current>1,noMore:j})},e.useMount=function(e){n.useEffect((function(){e()}),[])},e.useMountedRef=function(){var e=n.useRef(!1);return n.useEffect((function(){return e.current=!0,function(){e.current=!1}}),[]),e},e.usePagination=function(e,n){var t=n||{},o=t.defaultPageSize,f=void 0===o?10:o,l=t.refreshDeps,s=void 0===l?[]:l,d=i(t,["defaultPageSize","refreshDeps"]),v=ae(e,u({defaultParams:[{current:1,pageSize:f}]},d)),p=v.run,h=v.data,y=v.params,g=v.loading,m=i(v,["run","data","params","loading"]),b=y&&y[0]?y[0]:{},w=b.current,E=b.pageSize,R=(null==h?void 0:h.total)||0,S=Math.ceil(R/E),T=r.useCallback((function(e){var n=a(y),t=n[0],r=n.slice(1);p.apply(void 0,c([u(u({},t),e)],a(r),!1))}),[y,p]),O=r.useCallback((function(e,n){var t=e<=0?1:e,r=n<=0?1:n,u=Math.ceil(R/r);t>u&&u>0&&(t=u),T({current:t,pageSize:r})}),[T,R]),C=r.useCallback((function(e){O(e,E)}),[O,E]),j=r.useCallback((function(e){O(w,e)}),[w,O]),k=r.useCallback((function(e,n,t,r){T({current:e.current,pageSize:e.pageSize,filters:n,sorter:t,extra:r})}),[T]);return J((function(){!1!==(null==n?void 0:n.autoRun)&&C(1)}),s),u(u({},m),{data:h,run:p,loading:g,params:y,pagination:{current:w,pageSize:E,total:R,totalPage:S,onChange:O,changeCurrent:C,changePageSize:j},tableProps:{dataSource:(null==h?void 0:h.list)||[],loading:g,onChange:k,pagination:{total:R,current:w,pageSize:E}}})},e.usePersistFn=G,e.usePrevious=function(e){var t=n.useRef();return n.useEffect((function(){t.current=e})),t.current},e.useSafeState=function(e){var n=ve(),t=a(r.useState(e),2),u=t[0],i=t[1];return[u,r.useCallback((function(e){n.current||i(e)}),[n])]},e.useSize=function(e){var t=a(n.useState((function(){var n=pe(e);return{width:(n||{}).clientWidth,height:(n||{}).clientHeight}})),2),r=t[0],u=t[1];return n.useEffect((function(){var n=pe(e);function t(e){e&&u({width:e.clientWidth,height:e.clientHeight})}if(!n)return function(){};t(n);var r=new me((function(e){var n,r;try{for(var u=o(e),i=u.next();!i.done;i=u.next()){t(i.value.target)}}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}));return r.observe(n),function(){r.disconnect()}}),[e]),r},e.useThrottle=function(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=a(n.useState(e),2),i=u[0],o=de(u[1],t,r).run;return n.useEffect((function(){o(e)}),[o,e]),i},e.useThrottleFn=de,e.useUnmount=le,e.useUnmountedRef=ve,e.useUpdate=function(){var e=a(n.useState(0),2)[1];return n.useCallback((function(){return e((function(e){return e+1}))}),[])},e.useUpdateEffect=J,e.useUpdateLayoutEffect=function(e,t){var r=n.useRef(!1);n.useLayoutEffect((function(){if(r.current)return e();r.current=!0}),t)},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
***************************************************************************** */function i(e,n){var t={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.indexOf(r)<0&&(t[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var u=0;for(r=Object.getOwnPropertySymbols(e);u<r.length;u++)n.indexOf(r[u])<0&&Object.prototype.propertyIsEnumerable.call(e,r[u])&&(t[r[u]]=e[r[u]])}return t}function o(e){var n="function"==typeof Symbol&&Symbol.iterator,t=n&&e[n],r=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}};throw new TypeError(n?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(e,n){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var r,u,i=t.call(e),o=[];try{for(;(void 0===n||n-- >0)&&!(r=i.next()).done;)o.push(r.value)}catch(e){u={error:e}}finally{try{r&&!r.done&&(t=i.return)&&t.call(i)}finally{if(u)throw u.error}}return o}function c(e,n,t){if(t||2===arguments.length)for(var r,u=0,i=n.length;u<i;u++)!r&&u in n||(r||(r=Array.prototype.slice.call(n,0,u)),r[u]=n[u]);return e.concat(r||Array.prototype.slice.call(n))}var f="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};var l=function(e){var n=typeof e;return null!=e&&("object"==n||"function"==n)},s="object"==typeof f&&f&&f.Object===Object&&f,d="object"==typeof self&&self&&self.Object===Object&&self,v=s||d||Function("return this")(),p=v,h=function(){return p.Date.now()},y=/\s/;var g=function(e){for(var n=e.length;n--&&y.test(e.charAt(n)););return n},m=/^\s+/;var b=function(e){return e?e.slice(0,g(e)+1).replace(m,""):e},w=v.Symbol,E=w,T=Object.prototype,R=T.hasOwnProperty,S=T.toString,O=E?E.toStringTag:void 0;var C=function(e){var n=R.call(e,O),t=e[O];try{e[O]=void 0;var r=!0}catch(e){}var u=S.call(e);return r&&(n?e[O]=t:delete e[O]),u},j=Object.prototype.toString;var k=C,L=function(e){return j.call(e)},x=w?w.toStringTag:void 0;var P=function(e){return null==e?void 0===e?"[object Undefined]":"[object Null]":x&&x in Object(e)?k(e):L(e)},M=function(e){return null!=e&&"object"==typeof e};var N=b,A=l,_=function(e){return"symbol"==typeof e||M(e)&&"[object Symbol]"==P(e)},z=/^[-+]0x[0-9a-f]+$/i,D=/^0b[01]+$/i,H=/^0o[0-7]+$/i,I=parseInt;var F=l,W=h,U=function(e){if("number"==typeof e)return e;if(_(e))return NaN;if(A(e)){var n="function"==typeof e.valueOf?e.valueOf():e;e=A(n)?n+"":n}if("string"!=typeof e)return 0===e?e:+e;e=N(e);var t=D.test(e);return t||H.test(e)?I(e.slice(2),t?2:8):z.test(e)?NaN:+e},V=Math.max,$=Math.min;var q=function(e,n,t){var r,u,i,o,a,c,f=0,l=!1,s=!1,d=!0;if("function"!=typeof e)throw new TypeError("Expected a function");function v(n){var t=r,i=u;return r=u=void 0,f=n,o=e.apply(i,t)}function p(e){return f=e,a=setTimeout(y,n),l?v(e):o}function h(e){var t=e-c;return void 0===c||t>=n||t<0||s&&e-f>=i}function y(){var e=W();if(h(e))return g(e);a=setTimeout(y,function(e){var t=n-(e-c);return s?$(t,i-(e-f)):t}(e))}function g(e){return a=void 0,d&&r?v(e):(r=u=void 0,o)}function m(){var e=W(),t=h(e);if(r=arguments,u=this,c=e,t){if(void 0===a)return p(c);if(s)return clearTimeout(a),a=setTimeout(y,n),v(c)}return void 0===a&&(a=setTimeout(y,n)),o}return n=U(n)||0,F(t)&&(l=!!t.leading,i=(s="maxWait"in t)?V(U(t.maxWait)||0,n):i,d="trailing"in t?!!t.trailing:d),m.cancel=function(){void 0!==a&&clearTimeout(a),f=0,r=c=u=a=void 0},m.flush=function(){return void 0===a?o:g(W())},m},B=q,K=l;var Y=function(e,n,t){var r=!0,u=!0;if("function"!=typeof e)throw new TypeError("Expected a function");return K(t)&&(r="leading"in t?!!t.leading:r,u="trailing"in t?!!t.trailing:u),B(e,n,{leading:r,maxWait:n,trailing:u})};function G(e){var t=n.useRef(e);t.current=e;var r=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];var r=t.current;return null==r?void 0:r.apply(void 0,c([],a(e),!1))}),[t]);return r}var J=function(e,t){var r=n.useRef(!1);n.useEffect((function(){if(r.current)return e();r.current=!0}),t)};function Q(){return"undefined"==typeof document||void 0===document.visibilityState||"hidden"!==document.visibilityState}var X={},Z=function(e){var n=X[e];if(n){var t=n.startTime,r=n.cacheTime,u=n.data;if(!((new Date).getTime()-t>=r))return u;n.timer&&(clearTimeout(n.timer),n.timer=null,delete X[e])}},ee=function(e,n,t){void 0===t&&(t=3e5),X[e]&&clearTimeout(X[e].timer);var r=setTimeout((function(){delete X[e]}),t);X[e]={data:n,timer:r,cacheTime:t,startTime:(new Date).getTime()}};var ne=[];var te=!1;if("undefined"!=typeof window&&window.addEventListener&&!te){var re=function(){if(Q()&&("undefined"==typeof navigator||void 0===navigator.onLine||navigator.onLine))for(var e=0;e<ne.length;e++){(0,ne[e])()}};window.addEventListener("visibilitychange",re,!1),window.addEventListener("focus",re,!1),te=!0}var ue=[];var ie=!1;if("undefined"!=typeof window&&window.addEventListener&&!ie){window.addEventListener("visibilitychange",(function(){if(Q())for(var e=0;e<ue.length;e++){(0,ue[e])()}}),!1),ie=!0}var oe=function(){};function ae(e,t){var r=t||{},i=r.autoRun,o=void 0===i||i,f=r.refreshDeps,l=void 0===f?[]:f,s=r.defaultParams,d=void 0===s?[]:s,v=r.defaultLoading,p=void 0!==v&&v,h=r.initialData,y=r.cacheKey,g=void 0===y?"":y,m=r.cacheTime,b=void 0===m?3e5:m,w=r.persisted,E=void 0!==w&&w,T=r.onSuccess,R=void 0===T?oe:T,S=r.onError,O=void 0===S?oe:S,C=r.__INTERNAL_FORMAT__,j=r.pollingInterval,k=void 0===j?0:j,L=r.pollingWhenHidden,x=void 0===L||L,P=r.refreshOnWindowFocus,M=void 0!==P&&P,N=r.focusTimespan,A=void 0===N?5e3:N,_=r.loadingDelay,z=r.debounceInterval,D=r.throttleInterval,H=a(n.useState({params:Array.isArray(d)?d:[d],loading:p,error:null,data:g&&Z(g)||h}),2),I=H[0],F=H[1],W=n.useRef(0),U=n.useRef(null),V=n.useRef(!1),$=n.useRef(null),B=n.useRef(!1),K=n.useRef([]),X=G(e),te=G(R),re=G(O),ie=n.useRef(C);ie.current=C;var ae=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];U.current&&clearTimeout(U.current),$.current&&clearTimeout($.current),W.current+=1;var t=W.current,r=g?Z(g):void 0;r&&E||(F((function(n){return u(u({},n),{loading:!_,params:e})})),$.current=_?setTimeout((function(){F((function(e){return u(u({},e),{loading:!0})}))}),_):null);var i=function(){return new Promise((function(n,i){r&&E?B.current||t!==W.current||(te(r,e),n(r)):X.apply(void 0,c([],a(e),!1)).then((function(r){if(!B.current&&t===W.current){$.current&&clearTimeout($.current);var i="function"==typeof ie.current?ie.current(r,e):r;F((function(e){return u(u({},e),{data:i,error:null,loading:!1})})),g&&ee(g,i,b),te(i,e),n(i)}})).catch((function(n){B.current||t!==W.current||($.current&&clearTimeout($.current),F((function(e){return u(u({},e),{error:n,loading:!1})})),re(n,e),i(n))}))}))};return i().finally((function(){if(!B.current&&t===W.current&&k){if(!Q()&&!x)return void(V.current=!0);U.current=setTimeout((function(){le.apply(void 0,c([],a(e),!1))}),k)}}))}),[g,b,_,E,k,x]),ce=n.useRef(z?q(ae,z):void 0),fe=n.useRef(D?Y(ae,D):void 0),le=n.useCallback((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return ce.current?(ce.current.apply(ce,c([],a(e),!1)),Promise.resolve(null)):fe.current?(fe.current.apply(fe,c([],a(e),!1)),Promise.resolve(null)):ae.apply(void 0,c([],a(e),!1))}),[ae]),se=n.useCallback((function(){return le.apply(void 0,c([],a(I.params),!1))}),[le,I.params]),de=n.useCallback((function(){V.current&&(V.current=!1,se())}),[se]),ve=n.useCallback((function(){ce.current&&ce.current.cancel(),fe.current&&fe.current.cancel(),U.current&&clearTimeout(U.current),$.current&&clearTimeout($.current),V.current=!1,W.current+=1,F((function(e){return u(u({},e),{loading:!1})}))}),[]);J((function(){o&&se()}),c([],a(l),!1));return n.useEffect((function(){o&&le.apply(void 0,c([],a(I.params),!1));var e,n,t,r,u=K.current;if(k&&u.push((e=de,ue.push(e),function(){var n=ue.indexOf(e);ue.splice(n,1)})),M){var i=(n=se,t=A,r=!1,function(){for(var e=[],u=0;u<arguments.length;u++)e[u]=arguments[u];r||(r=!0,n.apply(void 0,c([],a(e),!1)),setTimeout((function(){r=!1}),t))});u.push(function(e){return ne.push(e),function(){var n=ne.indexOf(e);ne.splice(n,1)}}(i))}return function(){B.current=!0,ve(),u.forEach((function(e){return e()}))}}),[]),u(u({},I),{run:le,cancel:ve,mutate:function(e){F("function"==typeof e?function(n){return u(u({},n),{data:e(I.data)})}:function(n){return u(u({},n),{data:e})})},refresh:se})}var ce=function(e){return null!=e&&e===(null==e?void 0:e.window)};function fe(e){return"function"==typeof e?e():e}var le=function(e){var t=void 0===e?{}:e,r=t.target,u=t.threshold,i=void 0===u?100:u,o=t.onScrollLower,a=G(void 0===o?function(){}:o),c=n.useCallback((function(){if(r){var e,n=fe(r);void 0===(e=n)&&(e=window),("undefined"==typeof window?0:ce(e)?document.documentElement.scrollHeight:e.scrollHeight)-function(e){var n;return void 0===e&&(e=window),"undefined"==typeof window?0:ce(e)?window.scrollY:(null===(n=e)||void 0===n?void 0:n.scrollTop)||0}(n)<=function(e){return void 0===e&&(e=window),"undefined"==typeof window?0:ce(e)?document.documentElement.clientHeight:e.clientHeight}(n)+i&&a()}}),[a,r,i]);n.useEffect((function(){if(r){var e=fe(r);return e.addEventListener("scroll",c),function(){e.removeEventListener("scroll",c)}}}),[r,c])};var se=function(e){var t=n.useRef(e);t.current=e,n.useEffect((function(){return function(){return t.current()}}),[])};function de(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=n.useRef(e);u.current=e;var i=n.useRef(q((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return u.current.apply(u,c([],a(e),!1))}),t,r));return se((function(){i.current.cancel()})),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}function ve(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=n.useRef(e);u.current=e;var i=n.useRef(Y((function(){for(var e=[],n=0;n<arguments.length;n++)e[n]=arguments[n];return u.current.apply(u,c([],a(e),!1))}),t,r));return se((function(){i.current.cancel()})),{run:i.current,cancel:i.current.cancel,flush:i.current.flush}}var pe=function(){var e=r.useRef(!1);return r.useEffect((function(){return e.current=!1,function(){e.current=!0}}),[]),e};function he(e){return"function"==typeof e?e():e&&"current"in e?e.current:e}var ye="undefined"!=typeof window&&"undefined"!=typeof document&&window.document===document,ge=["top","right","bottom","left","width","height","size","weight"],me={characterData:!0,childList:!0,attributes:!0,subtree:!0},be=function(e){var n=this;this.refresh=function(){n.targetNode&&n.callback([{target:n.targetNode}],{})},this.onTransitionEnd_=function(e){var t=e.propertyName,r=void 0===t?"":t;ge.some((function(e){return r.indexOf(e)>-1}))&&n.throttleRefresh()},this.observe=function(e,t){ye&&(n.targetNode=e,document.addEventListener("transitionend",n.onTransitionEnd_),window.addEventListener("resize",n.throttleRefresh),n.observer.observe(document,u(u({},me),t)))},this.disconnect=function(){ye&&(n.throttleRefresh.cancel(),n.targetNode=null,document.removeEventListener("transitionend",n.onTransitionEnd_),window.removeEventListener("resize",n.throttleRefresh),n.observer.disconnect())},this.throttleRefresh=Y(this.refresh,50),this.targetNode=null,this.callback=e,this.observer=new MutationObserver(this.throttleRefresh)};e.useAsync=ae,e.useClickAway=function(e,n,t){void 0===t&&(t="click");var u=r.useRef(n);u.current=n,r.useEffect((function(){var n,r,i=function(n){var t;(Array.isArray(e)?e:[e]).some((function(e){var t=he(e);return!t||(null==t?void 0:t.contains(n.target))}))||null===(t=u.current)||void 0===t||t.call(u,n)},a=Array.isArray(t)?t:[t];try{for(var c=o(a),f=c.next();!f.done;f=c.next()){var l=f.value;document.addEventListener(l,i)}}catch(e){n={error:e}}finally{try{f&&!f.done&&(r=c.return)&&r.call(c)}finally{if(n)throw n.error}}return function(){var e,n;try{for(var t=o(a),r=t.next();!r.done;r=t.next()){var u=r.value;document.removeEventListener(u,i)}}catch(n){e={error:n}}finally{try{r&&!r.done&&(n=t.return)&&n.call(t)}finally{if(e)throw e.error}}}}),[e,t])},e.useClientRect=function(){var e=n.useRef(null),t=a(n.useState(),2),r=t[0],u=t[1];return n.useEffect((function(){var n;e.current&&u(null===(n=e.current)||void 0===n?void 0:n.getBoundingClientRect())}),[e]),[r,e]},e.useControllableValue=function(e,t){void 0===e&&(e={}),void 0===t&&(t={});var r=t.defaultValue,u=t.defaultValuePropName,i=void 0===u?"defaultValue":u,o=t.valuePropName,f=void 0===o?"value":o,l=t.trigger,s=void 0===l?"onChange":l,d=f in e,v=e[f],p=a(n.useState((function(){return d?v:i in e?e[i]:r})),2),h=p[0],y=p[1];J((function(){d&&y(v)}),[v,d]);var g=n.useCallback((function(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];d||y(n),e[s]&&e[s].apply(e,c([n],a(t),!1))}),[d,e,s]);return[d?v:h,g]},e.useDebounce=function(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=a(n.useState(e),2),i=u[0],o=de(u[1],t,r).run;return n.useEffect((function(){o(e)}),[o,e]),i},e.useDebounceFn=de,e.useIsMounted=function(){var e=n.useRef(!1);return n.useEffect((function(){return e.current=!0,function(){e.current=!1}}),[]),e},e.useLimitList=function(e,t){void 0===t&&(t={});var r=t||{},u=r.count,i=void 0===u?3:u,o=r.defaultLimited,c=void 0===o||o,f=n.useRef(e);f.current=Array.isArray(e)?e:[];var l=n.useMemo((function(){return i<0?0:Math.ceil(i)}),[i]),s=f.current.length>l,d=a(n.useState((function(){return s&&c?f.current.slice(0,l):f.current})),2),v=d[0],p=d[1],h=s&&v.length!==f.current.length,y=n.useCallback((function(){p(h?f.current:f.current.slice(0,l))}),[l,h]);return n.useEffect((function(){p(h?f.current.slice(0,l):f.current)}),[l,e]),{canLimit:s,limited:h,data:v,toggle:y}},e.useLoadMore=function(e,n){var t=n||{},o=t.threshold,f=void 0===o?100:o,l=t.target,s=t.isNoMore,d=void 0===s?function(){return!1}:s,v=t.refreshDeps,p=void 0===v?[]:v,h=i(t,["threshold","target","isNoMore","refreshDeps"]),y=r.useRef([]),g=r.useRef(1),m=ae(e,u(u({defaultParams:[{current:g.current}]},h),{onError:function(e,n){var t;g.current>1&&(g.current-=1),null===(t=null==h?void 0:h.onError)||void 0===t||t.call(h,e,n)},__INTERNAL_FORMAT__:function(e){return y.current=1===g.current?e.list:y.current.concat(e.list),u(u({},e),{list:y.current})}})),b=m.run,w=m.data,E=m.loading,T=m.cancel,R=m.params,S=m.mutate,O=i(m,["run","data","loading","cancel","params","mutate"]),C=!!d&&(!E&&d(w)),j=r.useCallback((function(){var e=a(R).slice(1);return b.apply(void 0,c([{current:g.current}],a(e),!1))}),[R,b]),k=r.useCallback((function(){E&&g.current>1&&(g.current-=1),T()}),[T,E]),L=r.useCallback((function(){E||C||(g.current+=1,j())}),[E,C,j]),x=r.useCallback((function(e){var n="function"==typeof e?e(w):e;y.current=(null==n?void 0:n.list)||[],S(n)}),[w,S]),P=r.useCallback((function(){return k(),g.current=1,x((function(e){return u(u({},e),{list:[]})})),j()}),[k,j,x]),M=r.useCallback((function(){if(!E&&l)return L()}),[L,l,E]);return le({target:l,threshold:f,onScrollLower:M}),J((function(){!1!==(null==n?void 0:n.autoRun)&&P()}),p),u(u({},O),{loading:E,data:w,run:b,refresh:P,cancel:k,mutate:x,params:R,loadMore:L,loadingMore:E&&g.current>1,noMore:C})},e.useMount=function(e){n.useEffect((function(){e()}),[])},e.useMountedRef=function(){var e=n.useRef(!1);return n.useEffect((function(){return e.current=!0,function(){e.current=!1}}),[]),e},e.usePagination=function(e,n){var t=n||{},o=t.defaultPageSize,f=void 0===o?10:o,l=t.refreshDeps,s=void 0===l?[]:l,d=i(t,["defaultPageSize","refreshDeps"]),v=ae(e,u({defaultParams:[{current:1,pageSize:f}]},d)),p=v.run,h=v.data,y=v.params,g=v.loading,m=i(v,["run","data","params","loading"]),b=y&&y[0]?y[0]:{},w=b.current,E=b.pageSize,T=(null==h?void 0:h.total)||0,R=Math.ceil(T/E),S=r.useCallback((function(e){var n=a(y),t=n[0],r=n.slice(1);p.apply(void 0,c([u(u({},t),e)],a(r),!1))}),[y,p]),O=r.useCallback((function(e,n){var t=e<=0?1:e,r=n<=0?1:n,u=Math.ceil(T/r);t>u&&u>0&&(t=u),S({current:t,pageSize:r})}),[S,T]),C=r.useCallback((function(e){O(e,E)}),[O,E]),j=r.useCallback((function(e){O(w,e)}),[w,O]),k=r.useCallback((function(e,n,t,r){S({current:e.current,pageSize:e.pageSize,filters:n,sorter:t,extra:r})}),[S]);return J((function(){!1!==(null==n?void 0:n.autoRun)&&C(1)}),s),u(u({},m),{data:h,run:p,loading:g,params:y,pagination:{current:w,pageSize:E,total:T,totalPage:R,onChange:O,changeCurrent:C,changePageSize:j},tableProps:{dataSource:(null==h?void 0:h.list)||[],loading:g,onChange:k,pagination:{total:T,current:w,pageSize:E}}})},e.usePersistFn=G,e.usePrevious=function(e){var t=n.useRef();return n.useEffect((function(){t.current=e})),t.current},e.useSafeState=function(e){var n=pe(),t=a(r.useState(e),2),u=t[0],i=t[1];return[u,r.useCallback((function(e){n.current||i(e)}),[n])]},e.useSize=function(e){var t=a(n.useState((function(){var n=he(e);return{width:(n||{}).clientWidth,height:(n||{}).clientHeight}})),2),r=t[0],u=t[1];return n.useEffect((function(){var n=he(e);function t(e){e&&u({width:e.clientWidth,height:e.clientHeight})}if(!n)return function(){};t(n);var r=new be((function(e){var n,r;try{for(var u=o(e),i=u.next();!i.done;i=u.next()){t(i.value.target)}}catch(e){n={error:e}}finally{try{i&&!i.done&&(r=u.return)&&r.call(u)}finally{if(n)throw n.error}}}));return r.observe(n),function(){r.disconnect()}}),[e]),r},e.useThrottle=function(e,t,r){void 0===t&&(t=0),void 0===r&&(r={});var u=a(n.useState(e),2),i=u[0],o=ve(u[1],t,r).run;return n.useEffect((function(){o(e)}),[o,e]),i},e.useThrottleFn=ve,e.useUnmount=se,e.useUnmountedRef=pe,e.useUpdate=function(){var e=a(n.useState(0),2)[1];return n.useCallback((function(){return e((function(e){return e+1}))}),[])},e.useUpdateEffect=J,e.useUpdateLayoutEffect=function(e,t){var r=n.useRef(!1);n.useLayoutEffect((function(){if(r.current)return e();r.current=!0}),t)},Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=rcHooks.min.js.map |
export declare type AsyncFunction<R = any, P extends any[] = any> = (...args: P) => Promise<R>; | ||
export declare type AsyncBaseOptions<R = any, P extends any[] = any> = Partial<{ | ||
export declare type AsyncOptions<R = any, P extends any[] = any> = Partial<{ | ||
autoRun: boolean; | ||
@@ -7,2 +7,6 @@ refreshDeps: any[]; | ||
defaultLoading: boolean; | ||
/** | ||
* @private 该API仅用于内部传递,请不要使用。 | ||
*/ | ||
__INTERNAL_FORMAT__: (res: R, params: P) => R; | ||
initialData: R; | ||
@@ -22,5 +26,2 @@ cacheKey: string; | ||
}>; | ||
export declare type AsyncOptions<R = any, P extends any[] = any, FP = any> = AsyncBaseOptions<R, P> & { | ||
formatResult: (res: FP, params: P) => R; | ||
}; | ||
export declare type AsyncResult<R = any, P extends any[] = any> = { | ||
@@ -36,4 +37,12 @@ run: (...args: P) => Promise<R | null>; | ||
}; | ||
export declare function useAsync<R = any, P extends any[] = any>(asyncFn: AsyncFunction<R, P>, options?: AsyncBaseOptions<R, P>): AsyncResult<R, P>; | ||
export declare function useAsync<R = any, P extends any[] = any, FP = any>(asyncFn: AsyncFunction<FP, P>, options?: AsyncOptions<R, P, FP>): AsyncResult<R, P>; | ||
export declare function useAsync<R = any, P extends any[] = any>(asyncFn: AsyncFunction<R, P>, options?: AsyncOptions<R, P>): { | ||
run: (...args: P) => Promise<R> | Promise<null>; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
refresh: () => Promise<R> | Promise<null>; | ||
params: P; | ||
loading: boolean; | ||
error: null | Error; | ||
data?: R | undefined; | ||
}; | ||
export default useAsync; |
@@ -14,4 +14,5 @@ import { __assign, __read, __spreadArray } from "tslib"; | ||
var noop = function () { }; | ||
// 异步方法hooks | ||
export function useAsync(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.autoRun, autoRun = _b === void 0 ? true : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, _d = _a.defaultParams, defaultParams = _d === void 0 ? [] : _d, _e = _a.defaultLoading, defaultLoading = _e === void 0 ? false : _e, initialData = _a.initialData, _f = _a.cacheKey, cacheKey = _f === void 0 ? '' : _f, _g = _a.cacheTime, cacheTime = _g === void 0 ? 5 * 60 * 1000 : _g, _h = _a.persisted, persisted = _h === void 0 ? false : _h, _j = _a.onSuccess, onSuccess = _j === void 0 ? noop : _j, _k = _a.onError, onError = _k === void 0 ? noop : _k, formatResult = _a.formatResult, _l = _a.pollingInterval, pollingInterval = _l === void 0 ? 0 : _l, _m = _a.pollingWhenHidden, pollingWhenHidden = _m === void 0 ? true : _m, _o = _a.refreshOnWindowFocus, refreshOnWindowFocus = _o === void 0 ? false : _o, _p = _a.focusTimespan, focusTimespan = _p === void 0 ? 5000 : _p, loadingDelay = _a.loadingDelay, debounceInterval = _a.debounceInterval, throttleInterval = _a.throttleInterval; | ||
var _a = (options || {}), _b = _a.autoRun, autoRun = _b === void 0 ? true : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, _d = _a.defaultParams, defaultParams = _d === void 0 ? [] : _d, _e = _a.defaultLoading, defaultLoading = _e === void 0 ? false : _e, initialData = _a.initialData, _f = _a.cacheKey, cacheKey = _f === void 0 ? '' : _f, _g = _a.cacheTime, cacheTime = _g === void 0 ? 5 * 60 * 1000 : _g, _h = _a.persisted, persisted = _h === void 0 ? false : _h, _j = _a.onSuccess, onSuccess = _j === void 0 ? noop : _j, _k = _a.onError, onError = _k === void 0 ? noop : _k, __INTERNAL_FORMAT__ = _a.__INTERNAL_FORMAT__, _l = _a.pollingInterval, pollingInterval = _l === void 0 ? 0 : _l, _m = _a.pollingWhenHidden, pollingWhenHidden = _m === void 0 ? true : _m, _o = _a.refreshOnWindowFocus, refreshOnWindowFocus = _o === void 0 ? false : _o, _p = _a.focusTimespan, focusTimespan = _p === void 0 ? 5000 : _p, loadingDelay = _a.loadingDelay, debounceInterval = _a.debounceInterval, throttleInterval = _a.throttleInterval; | ||
var _q = __read(useState({ | ||
@@ -34,4 +35,4 @@ // 参数兼容非array的情况 | ||
var onErrorPersist = usePersistFn(onError); | ||
var formatResultRef = useRef(formatResult); | ||
formatResultRef.current = formatResult; | ||
var internalFormatRef = useRef(__INTERNAL_FORMAT__); | ||
internalFormatRef.current = __INTERNAL_FORMAT__; | ||
var _run = useCallback(function () { | ||
@@ -84,4 +85,4 @@ var args = []; | ||
} | ||
var fmtData_1 = typeof formatResultRef.current === 'function' | ||
? formatResultRef.current(data, args) | ||
var fmtData_1 = typeof internalFormatRef.current === 'function' | ||
? internalFormatRef.current(data, args) | ||
: data; | ||
@@ -124,3 +125,3 @@ set(function (s) { return (__assign(__assign({}, s), { data: fmtData_1, error: null, loading: false })); }); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}, [cacheKey, cacheTime, loadingDelay, persisted, pollingInterval, pollingWhenHidden]); | ||
var debounceRunRef = useRef(debounceInterval ? debounce(_run, debounceInterval) : undefined); | ||
@@ -127,0 +128,0 @@ var throttleRunRef = useRef(throttleInterval ? throttle(_run, throttleInterval) : undefined); |
@@ -1,21 +0,18 @@ | ||
import * as React from 'react'; | ||
import type { AsyncBaseOptions, AsyncOptions, AsyncResult } from '.'; | ||
import type { AsyncOptions, AsyncResult } from '.'; | ||
import { TargetType } from './useScrollToLower'; | ||
export interface LoadMoreAsyncReturn<DataItem = any> { | ||
list: DataItem[]; | ||
[key: string]: any; | ||
} | ||
export declare type LoadMoreParams<R extends LoadMoreAsyncReturn = any> = [ | ||
export declare type LoadMoreParams = [ | ||
page: { | ||
current: number; | ||
data?: R; | ||
[key: string]: any; | ||
}, | ||
...args: any[] | ||
]; | ||
export interface LoadMoreAsyncBaseOption<R extends LoadMoreAsyncReturn = any> extends Omit<AsyncBaseOptions<R, LoadMoreParams<R>>, 'cacheKey' | 'cacheTime' | 'persisted' | 'pollingInterval' | 'pollingWhenHidden'> { | ||
export interface LoadMoreAsyncOption<R extends LoadMoreAsyncReturn = any> extends Omit<AsyncOptions<R, LoadMoreParams>, 'cacheKey' | 'cacheTime' | 'persisted' | 'pollingInterval' | 'pollingWhenHidden'> { | ||
threshold?: number; | ||
ref?: React.RefObject<HTMLElement | Window>; | ||
target?: TargetType; | ||
isNoMore?: (data?: R) => boolean; | ||
} | ||
export interface LoadMoreAsyncOption<R extends LoadMoreAsyncReturn = any, FP = any> extends LoadMoreAsyncBaseOption<R>, Pick<AsyncOptions<R, LoadMoreParams<R>, FP>, 'formatResult'> { | ||
} | ||
export interface LoadMoreResult<R extends LoadMoreAsyncReturn = any, P extends any[] = any> extends AsyncResult<R, P> { | ||
@@ -26,4 +23,17 @@ loadMore: () => void; | ||
} | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any>(asyncFn: (...args: LoadMoreParams<R>) => Promise<R>, options?: LoadMoreAsyncBaseOption<R>): LoadMoreResult<R, LoadMoreParams<R>>; | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any, FP = any>(asyncFn: (...args: LoadMoreParams<R>) => Promise<FP>, options?: LoadMoreAsyncOption<R, FP>): LoadMoreResult<R, LoadMoreParams<R>>; | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any>(asyncFn: (...args: LoadMoreParams) => Promise<R>, options?: LoadMoreAsyncOption<R>): { | ||
loading: boolean; | ||
data: R | undefined; | ||
run: (page: { | ||
current: number; | ||
}, ...args: any[]) => Promise<null> | Promise<R>; | ||
refresh: () => Promise<null> | Promise<R>; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
params: LoadMoreParams; | ||
loadMore: () => void; | ||
loadingMore: boolean; | ||
noMore: boolean; | ||
error: Error | null; | ||
}; | ||
export default useLoadMore; |
@@ -7,11 +7,10 @@ import { __assign, __read, __rest, __spreadArray } from "tslib"; | ||
export function useLoadMore(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.threshold, threshold = _b === void 0 ? 100 : _b, ref = _a.ref, _c = _a.isNoMore, isNoMore = _c === void 0 ? function () { return false; } : _c, _d = _a.refreshDeps, refreshDeps = _d === void 0 ? [] : _d, formatResult = _a.formatResult, restOptions = __rest(_a, ["threshold", "ref", "isNoMore", "refreshDeps", "formatResult"]); | ||
var dataGroup = React.useRef([]); | ||
var _a = (options || {}), _b = _a.threshold, threshold = _b === void 0 ? 100 : _b, target = _a.target, _c = _a.isNoMore, isNoMore = _c === void 0 ? function () { return false; } : _c, _d = _a.refreshDeps, refreshDeps = _d === void 0 ? [] : _d, restOptions = __rest(_a, ["threshold", "target", "isNoMore", "refreshDeps"]); | ||
var dataGroup = React.useRef([]); // 缓存之前请求的列表数据 | ||
var currentPageRef = React.useRef(1); // 当前页码 | ||
var _e = useAsync(asyncFn, __assign(__assign({ defaultParams: [ | ||
{ | ||
current: currentPageRef.current, | ||
data: options === null || options === void 0 ? void 0 : options.initialData | ||
current: currentPageRef.current | ||
} | ||
] }, restOptions), { onError: function (err, params) { | ||
] }, restOptions), { onError: function (err, _params) { | ||
var _a; | ||
@@ -22,9 +21,8 @@ // 加载失败并且当前页码大于第一页,页码自减一 | ||
} | ||
(_a = restOptions === null || restOptions === void 0 ? void 0 : restOptions.onError) === null || _a === void 0 ? void 0 : _a.call(restOptions, err, params); | ||
}, formatResult: function (res, params) { | ||
var fmtRes = (formatResult ? formatResult(res, params) : res); | ||
(_a = restOptions === null || restOptions === void 0 ? void 0 : restOptions.onError) === null || _a === void 0 ? void 0 : _a.call(restOptions, err, _params); | ||
}, __INTERNAL_FORMAT__: function (res) { | ||
dataGroup.current = | ||
currentPageRef.current === 1 ? fmtRes.list : dataGroup.current.concat(fmtRes.list); | ||
return __assign(__assign({}, fmtRes), { list: dataGroup.current }); | ||
} })), run = _e.run, loading = _e.loading, reqCancel = _e.cancel, data = _e.data, params = _e.params, reqMutate = _e.mutate, restAsyncReturn = __rest(_e, ["run", "loading", "cancel", "data", "params", "mutate"]); | ||
currentPageRef.current === 1 ? res.list : dataGroup.current.concat(res.list); | ||
return __assign(__assign({}, res), { list: dataGroup.current }); | ||
} })), run = _e.run, data = _e.data, loading = _e.loading, reqCancel = _e.cancel, params = _e.params, reqMutate = _e.mutate, restAsyncReturn = __rest(_e, ["run", "data", "loading", "cancel", "params", "mutate"]); | ||
var noMore = isNoMore ? !loading && isNoMore(data) : false; | ||
@@ -34,6 +32,5 @@ var loadData = React.useCallback(function () { | ||
return run.apply(void 0, __spreadArray([{ | ||
current: currentPageRef.current, | ||
data: data | ||
current: currentPageRef.current | ||
}], __read(restParams), false)); | ||
}, [data, params, run]); | ||
}, [params, run]); | ||
var cancel = React.useCallback(function () { | ||
@@ -65,9 +62,9 @@ // 加载中并且当前页码大于第一页,页码自减一 | ||
var scrollMethod = React.useCallback(function () { | ||
if (loading || !(ref === null || ref === void 0 ? void 0 : ref.current)) { | ||
if (loading || !target) { | ||
return; | ||
} | ||
return loadMore(); | ||
}, [loadMore, ref, loading]); | ||
}, [loadMore, target, loading]); | ||
useScrollToLower({ | ||
ref: ref, | ||
target: target, | ||
threshold: threshold, | ||
@@ -74,0 +71,0 @@ onScrollLower: scrollMethod |
@@ -1,5 +0,6 @@ | ||
import type { AsyncBaseOptions, AsyncOptions, AsyncResult } from '.'; | ||
import type { AsyncOptions, AsyncResult } from '.'; | ||
export interface PaginationAsyncReturn<DataItem = any> { | ||
list: DataItem[]; | ||
total: number; | ||
[key: string]: any; | ||
} | ||
@@ -14,7 +15,5 @@ export declare type PaginationParams = [ | ||
]; | ||
export interface PaginationAsyncBaseOption<R extends PaginationAsyncReturn = any> extends AsyncBaseOptions<R, PaginationParams> { | ||
export interface PaginationAsyncBaseOption<R extends PaginationAsyncReturn = any> extends AsyncOptions<R, PaginationParams> { | ||
defaultPageSize?: number; | ||
} | ||
export interface PaginationAsyncOption<R extends PaginationAsyncReturn = any, FP = any> extends PaginationAsyncBaseOption<R>, Pick<AsyncOptions<R, PaginationParams, FP>, 'formatResult'> { | ||
} | ||
export interface PaginationResult<R extends PaginationAsyncReturn = any, P extends any[] = any> extends AsyncResult<R, P> { | ||
@@ -41,4 +40,35 @@ pagination: { | ||
} | ||
export declare function usePagination<R extends PaginationAsyncReturn = any>(asyncFn: (...args: PaginationParams) => Promise<R>, options?: PaginationAsyncBaseOption<R>): PaginationResult<R, PaginationParams>; | ||
export declare function usePagination<R extends PaginationAsyncReturn = any, FP = any>(asyncFn: (...args: PaginationParams) => Promise<FP>, options?: PaginationAsyncOption<R, FP>): PaginationResult<R, PaginationParams>; | ||
export declare function usePagination<R extends PaginationAsyncReturn = any>(asyncFn: (...args: PaginationParams) => Promise<R>, options?: PaginationAsyncBaseOption<R>): { | ||
data: R | undefined; | ||
run: (param1: { | ||
[key: string]: any; | ||
current: number; | ||
pageSize: number; | ||
}, ...args: any[]) => Promise<null> | Promise<R>; | ||
loading: boolean; | ||
params: PaginationParams; | ||
pagination: { | ||
current: number; | ||
pageSize: number; | ||
total: number; | ||
totalPage: number; | ||
onChange: (c: number, p: number) => void; | ||
changeCurrent: (c: number) => void; | ||
changePageSize: (p: number) => void; | ||
}; | ||
tableProps: { | ||
dataSource: any[]; | ||
loading: boolean; | ||
onChange: (pagination: any, filters: any, sorter: any, extra: any) => void; | ||
pagination: { | ||
total: number; | ||
current: number; | ||
pageSize: number; | ||
}; | ||
}; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
refresh: () => Promise<null> | Promise<R>; | ||
error: Error | null; | ||
}; | ||
export default usePagination; |
@@ -6,3 +6,3 @@ import { __assign, __read, __rest, __spreadArray } from "tslib"; | ||
export function usePagination(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.defaultPageSize, defaultPageSize = _b === void 0 ? 10 : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, restOptions = __rest(_a, ["defaultPageSize", "refreshDeps"]); | ||
var _a = options || {}, _b = _a.defaultPageSize, defaultPageSize = _b === void 0 ? 10 : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, restOptions = __rest(_a, ["defaultPageSize", "refreshDeps"]); | ||
var _d = useAsync(asyncFn, __assign({ defaultParams: [ | ||
@@ -9,0 +9,0 @@ { |
@@ -1,8 +0,9 @@ | ||
/// <reference types="react" /> | ||
declare type ScrollElement = HTMLElement | Window; | ||
export declare type TargetType = ScrollElement | (() => ScrollElement); | ||
interface ScrollToLowerProps { | ||
ref?: React.RefObject<HTMLElement | Window>; | ||
target?: TargetType; | ||
threshold?: number; | ||
onScrollLower?: () => void; | ||
} | ||
declare const useScrollToLower: ({ ref, threshold, onScrollLower }?: ScrollToLowerProps) => void; | ||
declare const useScrollToLower: ({ target: outTarget, threshold, onScrollLower }?: ScrollToLowerProps) => void; | ||
export default useScrollToLower; |
import { useEffect, useCallback } from 'react'; | ||
import usePersistFn from '../usePersistFn'; | ||
import { getScrollHeight, getClientHeight, getScrollTop } from '../utils/dom'; | ||
function getTarget(target) { | ||
if (typeof target === 'function') { | ||
return target(); | ||
} | ||
return target; | ||
} | ||
var useScrollToLower = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, ref = _b.ref, _c = _b.threshold, threshold = _c === void 0 ? 100 : _c, _d = _b.onScrollLower, onScrollLower = _d === void 0 ? function () { } : _d; | ||
var _b = _a === void 0 ? {} : _a, outTarget = _b.target, _c = _b.threshold, threshold = _c === void 0 ? 100 : _c, _d = _b.onScrollLower, onScrollLower = _d === void 0 ? function () { } : _d; | ||
var onScrollLowerPersist = usePersistFn(onScrollLower); | ||
var scrollMethod = useCallback(function () { | ||
if (!(ref === null || ref === void 0 ? void 0 : ref.current)) { | ||
if (!outTarget) { | ||
return; | ||
} | ||
if (getScrollHeight(ref.current) - getScrollTop(ref.current) <= | ||
getClientHeight(ref.current) + threshold) { | ||
var target = getTarget(outTarget); | ||
if (getScrollHeight(target) - getScrollTop(target) <= getClientHeight(target) + threshold) { | ||
onScrollLowerPersist(); | ||
} | ||
}, [onScrollLowerPersist, ref, threshold]); | ||
}, [onScrollLowerPersist, outTarget, threshold]); | ||
useEffect(function () { | ||
var target = ref === null || ref === void 0 ? void 0 : ref.current; | ||
if (target) { | ||
target.addEventListener('scroll', scrollMethod); | ||
if (outTarget) { | ||
var target_1 = getTarget(outTarget); | ||
target_1.addEventListener('scroll', scrollMethod); | ||
return function () { | ||
target.removeEventListener('scroll', scrollMethod); | ||
target_1.removeEventListener('scroll', scrollMethod); | ||
}; | ||
} | ||
}, [ref, scrollMethod]); | ||
}, [outTarget, scrollMethod]); | ||
}; | ||
export default useScrollToLower; |
import { __read, __spreadArray } from "tslib"; | ||
// ref: | ||
// https://zh-hans.reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback | ||
// https://hooks.umijs.org/zh-CN/hooks/advanced/use-persist-fn | ||
import { useCallback, useRef } from 'react'; | ||
@@ -6,0 +5,0 @@ function usePersistFn(fn) { |
@@ -13,2 +13,3 @@ var cache = {}; | ||
clearTimeout(currentCache.timer); | ||
// @ts-ignore | ||
currentCache.timer = null; | ||
@@ -15,0 +16,0 @@ delete cache[key]; |
export declare type AsyncFunction<R = any, P extends any[] = any> = (...args: P) => Promise<R>; | ||
export declare type AsyncBaseOptions<R = any, P extends any[] = any> = Partial<{ | ||
export declare type AsyncOptions<R = any, P extends any[] = any> = Partial<{ | ||
autoRun: boolean; | ||
@@ -7,2 +7,6 @@ refreshDeps: any[]; | ||
defaultLoading: boolean; | ||
/** | ||
* @private 该API仅用于内部传递,请不要使用。 | ||
*/ | ||
__INTERNAL_FORMAT__: (res: R, params: P) => R; | ||
initialData: R; | ||
@@ -22,5 +26,2 @@ cacheKey: string; | ||
}>; | ||
export declare type AsyncOptions<R = any, P extends any[] = any, FP = any> = AsyncBaseOptions<R, P> & { | ||
formatResult: (res: FP, params: P) => R; | ||
}; | ||
export declare type AsyncResult<R = any, P extends any[] = any> = { | ||
@@ -36,4 +37,12 @@ run: (...args: P) => Promise<R | null>; | ||
}; | ||
export declare function useAsync<R = any, P extends any[] = any>(asyncFn: AsyncFunction<R, P>, options?: AsyncBaseOptions<R, P>): AsyncResult<R, P>; | ||
export declare function useAsync<R = any, P extends any[] = any, FP = any>(asyncFn: AsyncFunction<FP, P>, options?: AsyncOptions<R, P, FP>): AsyncResult<R, P>; | ||
export declare function useAsync<R = any, P extends any[] = any>(asyncFn: AsyncFunction<R, P>, options?: AsyncOptions<R, P>): { | ||
run: (...args: P) => Promise<R> | Promise<null>; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
refresh: () => Promise<R> | Promise<null>; | ||
params: P; | ||
loading: boolean; | ||
error: null | Error; | ||
data?: R | undefined; | ||
}; | ||
export default useAsync; |
@@ -17,4 +17,5 @@ "use strict"; | ||
var noop = function () { }; | ||
// 异步方法hooks | ||
function useAsync(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.autoRun, autoRun = _b === void 0 ? true : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, _d = _a.defaultParams, defaultParams = _d === void 0 ? [] : _d, _e = _a.defaultLoading, defaultLoading = _e === void 0 ? false : _e, initialData = _a.initialData, _f = _a.cacheKey, cacheKey = _f === void 0 ? '' : _f, _g = _a.cacheTime, cacheTime = _g === void 0 ? 5 * 60 * 1000 : _g, _h = _a.persisted, persisted = _h === void 0 ? false : _h, _j = _a.onSuccess, onSuccess = _j === void 0 ? noop : _j, _k = _a.onError, onError = _k === void 0 ? noop : _k, formatResult = _a.formatResult, _l = _a.pollingInterval, pollingInterval = _l === void 0 ? 0 : _l, _m = _a.pollingWhenHidden, pollingWhenHidden = _m === void 0 ? true : _m, _o = _a.refreshOnWindowFocus, refreshOnWindowFocus = _o === void 0 ? false : _o, _p = _a.focusTimespan, focusTimespan = _p === void 0 ? 5000 : _p, loadingDelay = _a.loadingDelay, debounceInterval = _a.debounceInterval, throttleInterval = _a.throttleInterval; | ||
var _a = (options || {}), _b = _a.autoRun, autoRun = _b === void 0 ? true : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, _d = _a.defaultParams, defaultParams = _d === void 0 ? [] : _d, _e = _a.defaultLoading, defaultLoading = _e === void 0 ? false : _e, initialData = _a.initialData, _f = _a.cacheKey, cacheKey = _f === void 0 ? '' : _f, _g = _a.cacheTime, cacheTime = _g === void 0 ? 5 * 60 * 1000 : _g, _h = _a.persisted, persisted = _h === void 0 ? false : _h, _j = _a.onSuccess, onSuccess = _j === void 0 ? noop : _j, _k = _a.onError, onError = _k === void 0 ? noop : _k, __INTERNAL_FORMAT__ = _a.__INTERNAL_FORMAT__, _l = _a.pollingInterval, pollingInterval = _l === void 0 ? 0 : _l, _m = _a.pollingWhenHidden, pollingWhenHidden = _m === void 0 ? true : _m, _o = _a.refreshOnWindowFocus, refreshOnWindowFocus = _o === void 0 ? false : _o, _p = _a.focusTimespan, focusTimespan = _p === void 0 ? 5000 : _p, loadingDelay = _a.loadingDelay, debounceInterval = _a.debounceInterval, throttleInterval = _a.throttleInterval; | ||
var _q = (0, tslib_1.__read)((0, react_1.useState)({ | ||
@@ -37,4 +38,4 @@ // 参数兼容非array的情况 | ||
var onErrorPersist = (0, usePersistFn_1.default)(onError); | ||
var formatResultRef = (0, react_1.useRef)(formatResult); | ||
formatResultRef.current = formatResult; | ||
var internalFormatRef = (0, react_1.useRef)(__INTERNAL_FORMAT__); | ||
internalFormatRef.current = __INTERNAL_FORMAT__; | ||
var _run = (0, react_1.useCallback)(function () { | ||
@@ -87,4 +88,4 @@ var args = []; | ||
} | ||
var fmtData_1 = typeof formatResultRef.current === 'function' | ||
? formatResultRef.current(data, args) | ||
var fmtData_1 = typeof internalFormatRef.current === 'function' | ||
? internalFormatRef.current(data, args) | ||
: data; | ||
@@ -127,3 +128,3 @@ set(function (s) { return ((0, tslib_1.__assign)((0, tslib_1.__assign)({}, s), { data: fmtData_1, error: null, loading: false })); }); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
}, [cacheKey, cacheTime, loadingDelay, persisted, pollingInterval, pollingWhenHidden]); | ||
var debounceRunRef = (0, react_1.useRef)(debounceInterval ? (0, debounce_1.default)(_run, debounceInterval) : undefined); | ||
@@ -130,0 +131,0 @@ var throttleRunRef = (0, react_1.useRef)(throttleInterval ? (0, throttle_1.default)(_run, throttleInterval) : undefined); |
@@ -1,21 +0,18 @@ | ||
import * as React from 'react'; | ||
import type { AsyncBaseOptions, AsyncOptions, AsyncResult } from '.'; | ||
import type { AsyncOptions, AsyncResult } from '.'; | ||
import { TargetType } from './useScrollToLower'; | ||
export interface LoadMoreAsyncReturn<DataItem = any> { | ||
list: DataItem[]; | ||
[key: string]: any; | ||
} | ||
export declare type LoadMoreParams<R extends LoadMoreAsyncReturn = any> = [ | ||
export declare type LoadMoreParams = [ | ||
page: { | ||
current: number; | ||
data?: R; | ||
[key: string]: any; | ||
}, | ||
...args: any[] | ||
]; | ||
export interface LoadMoreAsyncBaseOption<R extends LoadMoreAsyncReturn = any> extends Omit<AsyncBaseOptions<R, LoadMoreParams<R>>, 'cacheKey' | 'cacheTime' | 'persisted' | 'pollingInterval' | 'pollingWhenHidden'> { | ||
export interface LoadMoreAsyncOption<R extends LoadMoreAsyncReturn = any> extends Omit<AsyncOptions<R, LoadMoreParams>, 'cacheKey' | 'cacheTime' | 'persisted' | 'pollingInterval' | 'pollingWhenHidden'> { | ||
threshold?: number; | ||
ref?: React.RefObject<HTMLElement | Window>; | ||
target?: TargetType; | ||
isNoMore?: (data?: R) => boolean; | ||
} | ||
export interface LoadMoreAsyncOption<R extends LoadMoreAsyncReturn = any, FP = any> extends LoadMoreAsyncBaseOption<R>, Pick<AsyncOptions<R, LoadMoreParams<R>, FP>, 'formatResult'> { | ||
} | ||
export interface LoadMoreResult<R extends LoadMoreAsyncReturn = any, P extends any[] = any> extends AsyncResult<R, P> { | ||
@@ -26,4 +23,17 @@ loadMore: () => void; | ||
} | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any>(asyncFn: (...args: LoadMoreParams<R>) => Promise<R>, options?: LoadMoreAsyncBaseOption<R>): LoadMoreResult<R, LoadMoreParams<R>>; | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any, FP = any>(asyncFn: (...args: LoadMoreParams<R>) => Promise<FP>, options?: LoadMoreAsyncOption<R, FP>): LoadMoreResult<R, LoadMoreParams<R>>; | ||
export declare function useLoadMore<R extends LoadMoreAsyncReturn = any>(asyncFn: (...args: LoadMoreParams) => Promise<R>, options?: LoadMoreAsyncOption<R>): { | ||
loading: boolean; | ||
data: R | undefined; | ||
run: (page: { | ||
current: number; | ||
}, ...args: any[]) => Promise<null> | Promise<R>; | ||
refresh: () => Promise<null> | Promise<R>; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
params: LoadMoreParams; | ||
loadMore: () => void; | ||
loadingMore: boolean; | ||
noMore: boolean; | ||
error: Error | null; | ||
}; | ||
export default useLoadMore; |
@@ -10,11 +10,10 @@ "use strict"; | ||
function useLoadMore(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.threshold, threshold = _b === void 0 ? 100 : _b, ref = _a.ref, _c = _a.isNoMore, isNoMore = _c === void 0 ? function () { return false; } : _c, _d = _a.refreshDeps, refreshDeps = _d === void 0 ? [] : _d, formatResult = _a.formatResult, restOptions = (0, tslib_1.__rest)(_a, ["threshold", "ref", "isNoMore", "refreshDeps", "formatResult"]); | ||
var dataGroup = React.useRef([]); | ||
var _a = (options || {}), _b = _a.threshold, threshold = _b === void 0 ? 100 : _b, target = _a.target, _c = _a.isNoMore, isNoMore = _c === void 0 ? function () { return false; } : _c, _d = _a.refreshDeps, refreshDeps = _d === void 0 ? [] : _d, restOptions = (0, tslib_1.__rest)(_a, ["threshold", "target", "isNoMore", "refreshDeps"]); | ||
var dataGroup = React.useRef([]); // 缓存之前请求的列表数据 | ||
var currentPageRef = React.useRef(1); // 当前页码 | ||
var _e = (0, _1.default)(asyncFn, (0, tslib_1.__assign)((0, tslib_1.__assign)({ defaultParams: [ | ||
{ | ||
current: currentPageRef.current, | ||
data: options === null || options === void 0 ? void 0 : options.initialData | ||
current: currentPageRef.current | ||
} | ||
] }, restOptions), { onError: function (err, params) { | ||
] }, restOptions), { onError: function (err, _params) { | ||
var _a; | ||
@@ -25,9 +24,8 @@ // 加载失败并且当前页码大于第一页,页码自减一 | ||
} | ||
(_a = restOptions === null || restOptions === void 0 ? void 0 : restOptions.onError) === null || _a === void 0 ? void 0 : _a.call(restOptions, err, params); | ||
}, formatResult: function (res, params) { | ||
var fmtRes = (formatResult ? formatResult(res, params) : res); | ||
(_a = restOptions === null || restOptions === void 0 ? void 0 : restOptions.onError) === null || _a === void 0 ? void 0 : _a.call(restOptions, err, _params); | ||
}, __INTERNAL_FORMAT__: function (res) { | ||
dataGroup.current = | ||
currentPageRef.current === 1 ? fmtRes.list : dataGroup.current.concat(fmtRes.list); | ||
return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, fmtRes), { list: dataGroup.current }); | ||
} })), run = _e.run, loading = _e.loading, reqCancel = _e.cancel, data = _e.data, params = _e.params, reqMutate = _e.mutate, restAsyncReturn = (0, tslib_1.__rest)(_e, ["run", "loading", "cancel", "data", "params", "mutate"]); | ||
currentPageRef.current === 1 ? res.list : dataGroup.current.concat(res.list); | ||
return (0, tslib_1.__assign)((0, tslib_1.__assign)({}, res), { list: dataGroup.current }); | ||
} })), run = _e.run, data = _e.data, loading = _e.loading, reqCancel = _e.cancel, params = _e.params, reqMutate = _e.mutate, restAsyncReturn = (0, tslib_1.__rest)(_e, ["run", "data", "loading", "cancel", "params", "mutate"]); | ||
var noMore = isNoMore ? !loading && isNoMore(data) : false; | ||
@@ -37,6 +35,5 @@ var loadData = React.useCallback(function () { | ||
return run.apply(void 0, (0, tslib_1.__spreadArray)([{ | ||
current: currentPageRef.current, | ||
data: data | ||
current: currentPageRef.current | ||
}], (0, tslib_1.__read)(restParams), false)); | ||
}, [data, params, run]); | ||
}, [params, run]); | ||
var cancel = React.useCallback(function () { | ||
@@ -68,9 +65,9 @@ // 加载中并且当前页码大于第一页,页码自减一 | ||
var scrollMethod = React.useCallback(function () { | ||
if (loading || !(ref === null || ref === void 0 ? void 0 : ref.current)) { | ||
if (loading || !target) { | ||
return; | ||
} | ||
return loadMore(); | ||
}, [loadMore, ref, loading]); | ||
}, [loadMore, target, loading]); | ||
(0, useScrollToLower_1.default)({ | ||
ref: ref, | ||
target: target, | ||
threshold: threshold, | ||
@@ -77,0 +74,0 @@ onScrollLower: scrollMethod |
@@ -1,5 +0,6 @@ | ||
import type { AsyncBaseOptions, AsyncOptions, AsyncResult } from '.'; | ||
import type { AsyncOptions, AsyncResult } from '.'; | ||
export interface PaginationAsyncReturn<DataItem = any> { | ||
list: DataItem[]; | ||
total: number; | ||
[key: string]: any; | ||
} | ||
@@ -14,7 +15,5 @@ export declare type PaginationParams = [ | ||
]; | ||
export interface PaginationAsyncBaseOption<R extends PaginationAsyncReturn = any> extends AsyncBaseOptions<R, PaginationParams> { | ||
export interface PaginationAsyncBaseOption<R extends PaginationAsyncReturn = any> extends AsyncOptions<R, PaginationParams> { | ||
defaultPageSize?: number; | ||
} | ||
export interface PaginationAsyncOption<R extends PaginationAsyncReturn = any, FP = any> extends PaginationAsyncBaseOption<R>, Pick<AsyncOptions<R, PaginationParams, FP>, 'formatResult'> { | ||
} | ||
export interface PaginationResult<R extends PaginationAsyncReturn = any, P extends any[] = any> extends AsyncResult<R, P> { | ||
@@ -41,4 +40,35 @@ pagination: { | ||
} | ||
export declare function usePagination<R extends PaginationAsyncReturn = any>(asyncFn: (...args: PaginationParams) => Promise<R>, options?: PaginationAsyncBaseOption<R>): PaginationResult<R, PaginationParams>; | ||
export declare function usePagination<R extends PaginationAsyncReturn = any, FP = any>(asyncFn: (...args: PaginationParams) => Promise<FP>, options?: PaginationAsyncOption<R, FP>): PaginationResult<R, PaginationParams>; | ||
export declare function usePagination<R extends PaginationAsyncReturn = any>(asyncFn: (...args: PaginationParams) => Promise<R>, options?: PaginationAsyncBaseOption<R>): { | ||
data: R | undefined; | ||
run: (param1: { | ||
[key: string]: any; | ||
current: number; | ||
pageSize: number; | ||
}, ...args: any[]) => Promise<null> | Promise<R>; | ||
loading: boolean; | ||
params: PaginationParams; | ||
pagination: { | ||
current: number; | ||
pageSize: number; | ||
total: number; | ||
totalPage: number; | ||
onChange: (c: number, p: number) => void; | ||
changeCurrent: (c: number) => void; | ||
changePageSize: (p: number) => void; | ||
}; | ||
tableProps: { | ||
dataSource: any[]; | ||
loading: boolean; | ||
onChange: (pagination: any, filters: any, sorter: any, extra: any) => void; | ||
pagination: { | ||
total: number; | ||
current: number; | ||
pageSize: number; | ||
}; | ||
}; | ||
cancel: () => void; | ||
mutate: (newData: R | ((oldData: R) => R) | undefined) => void; | ||
refresh: () => Promise<null> | Promise<R>; | ||
error: Error | null; | ||
}; | ||
export default usePagination; |
@@ -9,3 +9,3 @@ "use strict"; | ||
function usePagination(asyncFn, options) { | ||
var _a = (options || {}), _b = _a.defaultPageSize, defaultPageSize = _b === void 0 ? 10 : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, restOptions = (0, tslib_1.__rest)(_a, ["defaultPageSize", "refreshDeps"]); | ||
var _a = options || {}, _b = _a.defaultPageSize, defaultPageSize = _b === void 0 ? 10 : _b, _c = _a.refreshDeps, refreshDeps = _c === void 0 ? [] : _c, restOptions = (0, tslib_1.__rest)(_a, ["defaultPageSize", "refreshDeps"]); | ||
var _d = (0, _1.default)(asyncFn, (0, tslib_1.__assign)({ defaultParams: [ | ||
@@ -12,0 +12,0 @@ { |
@@ -1,8 +0,9 @@ | ||
/// <reference types="react" /> | ||
declare type ScrollElement = HTMLElement | Window; | ||
export declare type TargetType = ScrollElement | (() => ScrollElement); | ||
interface ScrollToLowerProps { | ||
ref?: React.RefObject<HTMLElement | Window>; | ||
target?: TargetType; | ||
threshold?: number; | ||
onScrollLower?: () => void; | ||
} | ||
declare const useScrollToLower: ({ ref, threshold, onScrollLower }?: ScrollToLowerProps) => void; | ||
declare const useScrollToLower: ({ target: outTarget, threshold, onScrollLower }?: ScrollToLowerProps) => void; | ||
export default useScrollToLower; |
@@ -7,24 +7,30 @@ "use strict"; | ||
var dom_1 = require("../utils/dom"); | ||
function getTarget(target) { | ||
if (typeof target === 'function') { | ||
return target(); | ||
} | ||
return target; | ||
} | ||
var useScrollToLower = function (_a) { | ||
var _b = _a === void 0 ? {} : _a, ref = _b.ref, _c = _b.threshold, threshold = _c === void 0 ? 100 : _c, _d = _b.onScrollLower, onScrollLower = _d === void 0 ? function () { } : _d; | ||
var _b = _a === void 0 ? {} : _a, outTarget = _b.target, _c = _b.threshold, threshold = _c === void 0 ? 100 : _c, _d = _b.onScrollLower, onScrollLower = _d === void 0 ? function () { } : _d; | ||
var onScrollLowerPersist = (0, usePersistFn_1.default)(onScrollLower); | ||
var scrollMethod = (0, react_1.useCallback)(function () { | ||
if (!(ref === null || ref === void 0 ? void 0 : ref.current)) { | ||
if (!outTarget) { | ||
return; | ||
} | ||
if ((0, dom_1.getScrollHeight)(ref.current) - (0, dom_1.getScrollTop)(ref.current) <= | ||
(0, dom_1.getClientHeight)(ref.current) + threshold) { | ||
var target = getTarget(outTarget); | ||
if ((0, dom_1.getScrollHeight)(target) - (0, dom_1.getScrollTop)(target) <= (0, dom_1.getClientHeight)(target) + threshold) { | ||
onScrollLowerPersist(); | ||
} | ||
}, [onScrollLowerPersist, ref, threshold]); | ||
}, [onScrollLowerPersist, outTarget, threshold]); | ||
(0, react_1.useEffect)(function () { | ||
var target = ref === null || ref === void 0 ? void 0 : ref.current; | ||
if (target) { | ||
target.addEventListener('scroll', scrollMethod); | ||
if (outTarget) { | ||
var target_1 = getTarget(outTarget); | ||
target_1.addEventListener('scroll', scrollMethod); | ||
return function () { | ||
target.removeEventListener('scroll', scrollMethod); | ||
target_1.removeEventListener('scroll', scrollMethod); | ||
}; | ||
} | ||
}, [ref, scrollMethod]); | ||
}, [outTarget, scrollMethod]); | ||
}; | ||
exports.default = useScrollToLower; |
@@ -6,3 +6,2 @@ "use strict"; | ||
// https://zh-hans.reactjs.org/docs/hooks-faq.html#how-to-read-an-often-changing-value-from-usecallback | ||
// https://hooks.umijs.org/zh-CN/hooks/advanced/use-persist-fn | ||
var react_1 = require("react"); | ||
@@ -9,0 +8,0 @@ function usePersistFn(fn) { |
@@ -16,2 +16,3 @@ "use strict"; | ||
clearTimeout(currentCache.timer); | ||
// @ts-ignore | ||
currentCache.timer = null; | ||
@@ -18,0 +19,0 @@ delete cache[key]; |
{ | ||
"name": "rc-hooks", | ||
"version": "1.8.4", | ||
"version": "2.0.0", | ||
"description": "React Hooks Library.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
146
4611
307768