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;AACH,KAAI,aAAa,EAAG,QAAO;CAE3B,MAAM,YAAY,IADF,SAAS,SAAS,IAAI,QAAQ,SAAS,QAAQ,CACjC;CAC9B,IAAI,gBAAsD;CAE1D,MAAM,sBAAsB,UAA8B;AACxD,MAAI,YAAY,MAAM,EAAE;AACtB,aAAU,QAAQ,QAAQ;AAC1B;;AAEF,MAAI,cACF,cAAa,cAAc;AAE7B,kBAAgB,iBAAiB;AAC/B,aAAU,QAAQ,QAAQ;KACzB,MAAM;;CAGX,MAAM,cAAc,SAAiC;AACnD,MAAI,SAAS,UACX,KAAI,SAAS,SAAS,CACpB,oBAAmB,SAAS;MAE5B,oBAAmB,SAAS,QAAQ;WAGlC,SAAS,SAAS,CACpB,oBAAmB,SAAS,SAAS;MAErC,WAAU,QAAQ;;AAKxB,iBAAgB,WAAW,UAAU,CAAC;AAEtC,aACQ,QAAQ,QACb,QAAQ;AACP,aAAW,MAAM,YAAY,WAAW;GAE3C;AAED,QAAO"}