mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-05 05:29:01 +08:00
30 lines
737 B
TypeScript
30 lines
737 B
TypeScript
import { onBeforeUpdate } from 'vue';
|
|
|
|
/**
|
|
* Locker return cached mark.
|
|
* If set to `true`, will return `true` in a short time even if set `false`.
|
|
* If set to `false` and then set to `true`, will change to `true`.
|
|
* And after time duration, it will back to `null` automatically.
|
|
*/
|
|
export default function useLock(duration = 250): [() => boolean | null, (lock: boolean) => void] {
|
|
let lock: boolean | null = null;
|
|
let timeout: any;
|
|
|
|
onBeforeUpdate(() => {
|
|
clearTimeout(timeout);
|
|
});
|
|
|
|
function doLock(locked: boolean) {
|
|
if (locked || lock === null) {
|
|
lock = locked;
|
|
}
|
|
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(() => {
|
|
lock = null;
|
|
}, duration);
|
|
}
|
|
|
|
return [() => lock, doLock];
|
|
}
|