| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | export function createSampler<T extends (...args: any[]) => any>(fn: T, sampleInterval: number): T {
|
| | let lastArgs: Parameters<T> | null = null;
|
| | let lastTime = 0;
|
| | let timeout: NodeJS.Timeout | null = null;
|
| |
|
| |
|
| | const sampled = function (this: any, ...args: Parameters<T>) {
|
| | const now = Date.now();
|
| | lastArgs = args;
|
| |
|
| |
|
| | if (now - lastTime < sampleInterval) {
|
| |
|
| | if (!timeout) {
|
| | timeout = setTimeout(
|
| | () => {
|
| | timeout = null;
|
| | lastTime = Date.now();
|
| |
|
| | if (lastArgs) {
|
| | fn.apply(this, lastArgs);
|
| | lastArgs = null;
|
| | }
|
| | },
|
| | sampleInterval - (now - lastTime),
|
| | );
|
| | }
|
| |
|
| | return;
|
| | }
|
| |
|
| |
|
| | lastTime = now;
|
| | fn.apply(this, args);
|
| | lastArgs = null;
|
| | } as T;
|
| |
|
| | return sampled;
|
| | }
|
| |
|