add mouse movement observer

This commit is contained in:
Yanzhen Yu
2026-04-01 12:00:00 +08:00
parent d2175eae87
commit 61312a0ad0
4 changed files with 117 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import { Mirror } from './types';
import { Mirror, throttleOptions } from './types';
export const mirror: Mirror = {
map: {},
@@ -9,3 +9,37 @@ export const mirror: Mirror = {
return mirror.map[id];
},
};
// copy from underscore
export function throttle<T>(
func: (arg: T) => void,
wait: number,
options: throttleOptions = {},
) {
let timeout: number | null = null;
let previous = 0;
// tslint:disable-next-line: only-arrow-functions
return function() {
let now = Date.now();
if (!previous && options.leading === false) {
previous = now;
}
let remaining = wait - (now - previous);
let context = this;
let args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
func.apply(context, args);
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(() => {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
func.apply(context, args);
}, remaining);
}
};
}