jh-vue-hooks
Version:
金合Vue Hooks 事件监听hook
2 lines (1 loc) • 1.25 kB
JavaScript
import{ref as e,watch as t,unref as n}from"vue";import{useDebounceFn as o,useThrottleFn as r,tryOnMounted as i,tryOnUnmounted as u}from"@vueuse/core";export{useDebounceFn,useThrottleFn}from"@vueuse/core";function s({el:i=window,name:u,listener:s,options:a,autoRemove:c=!0,isDebounce:m=!0,wait:f=80}){let v=()=>{};const d=e(!1);if(i){const l=e(i),w=m?o(s,f):r(s,f),p=f?w:s,E=e=>{d.value=!0,e.removeEventListener(u,p,a)},y=e=>e.addEventListener(u,p,a),L=t(l,((e,t,o)=>{e&&(!n(d)&&y(e),o((()=>{c&&E(e)})))}),{immediate:!0});v=()=>{E(l.value),L()}}return{removeEvent:v}}function a(e,t=150,n){let r=()=>{e()};const s=o(r,t);r=s;const a=()=>{n&&n.immediate&&r(),window.addEventListener("resize",r)},c=()=>{window.removeEventListener("resize",r)};return i((()=>{a()})),u((()=>{c()})),[a,c]}function c(e,n,o=!1){if("function"!=typeof e)throw new Error("handle is not Function!");const{readyRef:r,stop:i,start:u}=m(n);return o?e():t(r,(t=>{t&&e()}),{immediate:!1}),{readyRef:r,stop:i,start:u}}function m(t){const n=e(!1);let o;function r(){n.value=!1,o&&window.clearTimeout(o)}function i(){r(),o=setTimeout((()=>{n.value=!0}),t)}return i(),u(r),{readyRef:n,stop:r,start:i}}export{s as useEventListener,c as useTimeoutFn,m as useTimeoutRef,a as useWindowResize};