UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 2.16 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../../../../../packages/hooks/use-throttle-render/index.ts"],"sourcesContent":["import { onMounted, ref, watch } from 'vue'\nimport { isNumber, isObject, isUndefined } from '@element-plus/utils'\n\nimport type { Ref } from 'vue'\n\nexport type ThrottleType =\n | { leading?: number; trailing?: number; initVal?: boolean }\n | number\n\nexport const useThrottleRender = (\n loading: Ref<boolean>,\n throttle: ThrottleType = 0\n) => {\n if (throttle === 0) return loading\n const initVal = isObject(throttle) && Boolean(throttle.initVal)\n const throttled = ref(initVal)\n let timeoutHandle: ReturnType<typeof setTimeout> | null = null\n\n const dispatchThrottling = (timer: number | undefined) => {\n if (isUndefined(timer)) {\n throttled.value = loading.value\n return\n }\n if (timeoutHandle) {\n clearTimeout(timeoutHandle)\n }\n timeoutHandle = setTimeout(() => {\n throttled.value = loading.value\n }, timer)\n }\n\n const dispatcher = (type: 'leading' | 'trailing') => {\n if (type === 'leading') {\n if (isNumber(throttle)) {\n dispatchThrottling(throttle)\n } else {\n dispatchThrottling(throttle.leading)\n }\n } else {\n if (isObject(throttle)) {\n dispatchThrottling(throttle.trailing)\n } else {\n throttled.value = false\n }\n }\n }\n\n onMounted(() => dispatcher('leading'))\n\n watch(\n () => loading.value,\n (val) => {\n dispatcher(val ? 'leading' : 'trailing')\n }\n )\n\n return throttled\n}\n"],"mappings":";;;AASA,MAAa,qBACX,SACA,WAAyB,MACtB;CACH,IAAI,aAAa,GAAG,OAAO;CAE3B,MAAM,YAAY,IADF,SAAS,SAAS,IAAI,QAAQ,SAAS,QAAQ,CACjC;CAC9B,IAAI,gBAAsD;CAE1D,MAAM,sBAAsB,UAA8B;EACxD,IAAI,YAAY,MAAM,EAAE;GACtB,UAAU,QAAQ,QAAQ;GAC1B;;EAEF,IAAI,eACF,aAAa,cAAc;EAE7B,gBAAgB,iBAAiB;GAC/B,UAAU,QAAQ,QAAQ;KACzB,MAAM;;CAGX,MAAM,cAAc,SAAiC;EACnD,IAAI,SAAS,WACX,IAAI,SAAS,SAAS,EACpB,mBAAmB,SAAS;OAE5B,mBAAmB,SAAS,QAAQ;OAGtC,IAAI,SAAS,SAAS,EACpB,mBAAmB,SAAS,SAAS;OAErC,UAAU,QAAQ;;CAKxB,gBAAgB,WAAW,UAAU,CAAC;CAEtC,YACQ,QAAQ,QACb,QAAQ;EACP,WAAW,MAAM,YAAY,WAAW;GAE3C;CAED,OAAO"}