add mouse movement observer
This commit is contained in:
36
src/utils.ts
36
src/utils.ts
@@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user