iking-vue-hooks
Version:
金合Vue Hooks 事件监听hook
2 lines (1 loc) • 3.08 kB
JavaScript
import{tryOnUnmounted as e,useDebounceFn as t,useThrottleFn as n,tryOnMounted as o}from"@vueuse/core";export{useDebounceFn,useThrottleFn}from"@vueuse/core";import{onMounted as r,nextTick as u,onActivated as i,reactive as s,readonly as c,provide as a,inject as f,getCurrentInstance as l,shallowRef as d,watchEffect as m,ref as v,unref as p,onBeforeUpdate as w,watch as y}from"vue";import{paramType as x}from"iking-utils";function R(e,t=Symbol(),n={}){const{readonly:o=!0,createProvider:r=!1,native:u=!1}=n,i=s(e),f=o?c(i):i;return!r&&a(t,u?e:f),{state:i}}function C(e=Symbol(),t){return f(e,t||{})}const b=Symbol();function h(){return C(b)}const E=["class","style"],F=/^on[A-Z]/;function T(e){return Object.keys(e).map((t=>[t,e[t]]))}function L(t){const n=v(!1);let o;function r(){n.value=!1,o&&window.clearTimeout(o)}function u(){r(),o=setTimeout((()=>{n.value=!0}),t)}return u(),e(r),{readyRef:n,stop:r,start:u}}var A=Object.freeze({__proto__:null,onMountedOrActivated:function(e){let t;r((()=>{e(),u((()=>{t=!0}))})),i((()=>{t&&e()}))},createAppProviderContext:function(e){return R(e,b)},useAppProviderContext:h,entries:T,useAttrs:function(e={}){const t=l();if(!t)return{};const{excludeListeners:n=!1,excludeKeys:o=[],excludeDefaultKeys:r=!0}=e,u=d({}),i=o.concat(r?E:[]);return t.attrs=s(t.attrs),m((()=>{const e=T(t.attrs).reduce(((e,[t,o])=>(i.includes(t)||n&&F.test(t)||(e[t]=o),e)),{});u.value=e})),u},createContext:R,useContext:C,useDesign:function(e){const t=h();return{prefixCls:`${t.prefixCls}-${e}`,prefixVar:t.prefixCls}},useLockFn:function(e){const t=v(!1);return async function(...n){if(!p(t)){t.value=!0;try{const o=await e(...n);return t.value=!1,o}catch(e){throw t.value=!1,e}}}},useRefs:function(){const e=v([]);return w((()=>{e.value=[]})),[e,t=>n=>{e.value[t]=n}]},useTimeoutFn:function(e,t,n=!1){if(!x.isFunction(e))throw new Error("handle is not Function!");const{readyRef:o,stop:r,start:u}=L(t);return n?e():y(o,(t=>{t&&e()}),{immediate:!1}),{readyRef:o,stop:r,start:u}},useTimeoutRef:L});function k({el:e=window,name:o,listener:r,options:u,autoRemove:i=!0,isDebounce:s=!0,wait:c=80}){let a=()=>{};const f=v(!1);if(e){const l=v(e),d=s?t(r,c):n(r,c),m=c?d:r,w=e=>{f.value=!0,e.removeEventListener(o,m,u)},x=e=>e.addEventListener(o,m,u),R=y(l,((e,t,n)=>{e&&(!p(f)&&x(e),n((()=>{i&&w(e)})))}),{immediate:!0});a=()=>{w(l.value),R()}}return{removeEvent:a}}function D(e,t,n=!1){if("function"!=typeof e)throw new Error("handle is not Function!");const{readyRef:o,stop:r,start:u}=_(t);return n?e():y(o,(t=>{t&&e()}),{immediate:!1}),{readyRef:o,stop:r,start:u}}function _(t){const n=v(!1);let o;function r(){n.value=!1,o&&window.clearTimeout(o)}function u(){r(),o=setTimeout((()=>{n.value=!0}),t)}return u(),e(r),{readyRef:n,stop:r,start:u}}function g(n,r=150,u){let i=()=>{n()};const s=t(i,r);i=s;const c=()=>{u&&u.immediate&&i(),window.addEventListener("resize",i)},a=()=>{window.removeEventListener("resize",i)};return o((()=>{c()})),e((()=>{a()})),[c,a]}export{A as ikingCore,k as useEventListener,D as useTimeoutFn,_ as useTimeoutRef,g as useWindowResize};