2021-05-10 08:34:14 +00:00
|
|
|
import { ComputedRef } from '@vue/reactivity';
|
|
|
|
|
|
|
|
export default (callback: () => boolean, buffer: ComputedRef<number>) => {
|
2020-10-10 05:57:37 +00:00
|
|
|
let called = false;
|
|
|
|
let timeout = null;
|
|
|
|
|
|
|
|
function cancelTrigger() {
|
|
|
|
window.clearTimeout(timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
function trigger(force?: boolean) {
|
|
|
|
if (!called || force === true) {
|
|
|
|
if (callback() === false) {
|
|
|
|
// Not delay since callback cancelled self
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
called = true;
|
|
|
|
cancelTrigger();
|
|
|
|
timeout = window.setTimeout(() => {
|
|
|
|
called = false;
|
2021-05-10 08:34:14 +00:00
|
|
|
}, buffer.value);
|
2020-10-10 05:57:37 +00:00
|
|
|
} else {
|
|
|
|
cancelTrigger();
|
|
|
|
timeout = window.setTimeout(() => {
|
|
|
|
called = false;
|
|
|
|
trigger();
|
2021-05-10 08:34:14 +00:00
|
|
|
}, buffer.value);
|
2020-10-10 05:57:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
trigger,
|
|
|
|
() => {
|
|
|
|
called = false;
|
|
|
|
cancelTrigger();
|
|
|
|
},
|
|
|
|
];
|
|
|
|
};
|