2020-12-13 22:28:17 +00:00
|
|
|
// * Jolly Cobra's schedulers
|
2021-01-07 12:36:21 +00:00
|
|
|
import { AnyToVoidFunction, NoneToVoidFunction } from "../types";
|
2020-12-13 22:28:17 +00:00
|
|
|
|
|
|
|
//type Scheduler = typeof requestAnimationFrame | typeof onTickEnd | typeof runNow;
|
|
|
|
|
|
|
|
export function debounce<F extends AnyToVoidFunction>(
|
|
|
|
fn: F,
|
|
|
|
ms: number,
|
|
|
|
shouldRunFirst = true,
|
|
|
|
shouldRunLast = true,
|
|
|
|
) {
|
|
|
|
let waitingTimeout: number | null = null;
|
|
|
|
|
|
|
|
return (...args: Parameters<F>) => {
|
|
|
|
if(waitingTimeout) {
|
|
|
|
clearTimeout(waitingTimeout);
|
|
|
|
waitingTimeout = null;
|
|
|
|
} else if(shouldRunFirst) {
|
|
|
|
// @ts-ignore
|
|
|
|
fn(...args);
|
|
|
|
}
|
|
|
|
|
2021-02-04 06:58:16 +00:00
|
|
|
waitingTimeout = setTimeout(() => {
|
2020-12-13 22:28:17 +00:00
|
|
|
if(shouldRunLast) {
|
|
|
|
// @ts-ignore
|
|
|
|
fn(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
waitingTimeout = null;
|
2021-02-04 06:58:16 +00:00
|
|
|
}, ms) as any;
|
2020-12-13 22:28:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/* export function throttle<F extends AnyToVoidFunction>(
|
|
|
|
fn: F,
|
|
|
|
ms: number,
|
|
|
|
shouldRunFirst = true,
|
|
|
|
) {
|
|
|
|
let interval: number | null = null;
|
|
|
|
let isPending: boolean;
|
|
|
|
let args: Parameters<F>;
|
|
|
|
|
|
|
|
return (..._args: Parameters<F>) => {
|
|
|
|
isPending = true;
|
|
|
|
args = _args;
|
|
|
|
|
|
|
|
if (!interval) {
|
|
|
|
if (shouldRunFirst) {
|
|
|
|
isPending = false;
|
|
|
|
// @ts-ignore
|
|
|
|
fn(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
interval = window.setInterval(() => {
|
|
|
|
if (!isPending) {
|
|
|
|
window.clearInterval(interval!);
|
|
|
|
interval = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
isPending = false;
|
|
|
|
// @ts-ignore
|
|
|
|
fn(...args);
|
|
|
|
}, ms);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} */
|
|
|
|
|
|
|
|
/* export function throttleWithRaf<F extends AnyToVoidFunction>(fn: F) {
|
|
|
|
return throttleWith(fastRaf, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throttleWithTickEnd<F extends AnyToVoidFunction>(fn: F) {
|
|
|
|
return throttleWith(onTickEnd, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throttleWithNow<F extends AnyToVoidFunction>(fn: F) {
|
|
|
|
return throttleWith(runNow, fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function throttleWith<F extends AnyToVoidFunction>(schedulerFn: Scheduler, fn: F) {
|
|
|
|
let waiting = false;
|
|
|
|
let args: Parameters<F>;
|
|
|
|
|
|
|
|
return (..._args: Parameters<F>) => {
|
|
|
|
args = _args;
|
|
|
|
|
|
|
|
if (!waiting) {
|
|
|
|
waiting = true;
|
|
|
|
|
|
|
|
schedulerFn(() => {
|
|
|
|
waiting = false;
|
|
|
|
// @ts-ignore
|
|
|
|
fn(...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function onTickEnd(cb: NoneToVoidFunction) {
|
|
|
|
Promise.resolve().then(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function runNow(fn: NoneToVoidFunction) {
|
|
|
|
fn();
|
|
|
|
} */
|
|
|
|
|
2021-03-30 15:31:11 +00:00
|
|
|
export const pause = (ms: number) => new Promise<void>((resolve) => {
|
2020-12-13 22:28:17 +00:00
|
|
|
setTimeout(resolve, ms);
|
|
|
|
});
|
|
|
|
|
2021-01-07 12:36:21 +00:00
|
|
|
let fastRafCallbacks: NoneToVoidFunction[] | undefined;
|
2020-12-13 22:28:17 +00:00
|
|
|
export function fastRaf(callback: NoneToVoidFunction) {
|
2021-01-07 12:36:21 +00:00
|
|
|
if(!fastRafCallbacks) {
|
2020-12-13 22:28:17 +00:00
|
|
|
fastRafCallbacks = [callback];
|
|
|
|
|
|
|
|
requestAnimationFrame(() => {
|
|
|
|
const currentCallbacks = fastRafCallbacks!;
|
|
|
|
fastRafCallbacks = undefined;
|
|
|
|
currentCallbacks.forEach((cb) => cb());
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fastRafCallbacks.push(callback);
|
|
|
|
}
|
2021-01-07 12:36:21 +00:00
|
|
|
}
|
2021-02-16 15:36:26 +00:00
|
|
|
|
2021-02-18 16:05:33 +00:00
|
|
|
export function doubleRaf() {
|
2021-02-16 15:36:26 +00:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
window.requestAnimationFrame(resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|