Browse Source

Moves throttle and fixes implementation

main
Eric Amodio 1 year ago
parent
commit
592fb60bc5
2 changed files with 24 additions and 21 deletions
  1. +24
    -0
      src/system/function.ts
  2. +0
    -21
      src/webviews/apps/shared/utils.ts

+ 24
- 0
src/system/function.ts View File

@ -207,3 +207,27 @@ export async function sequentialize unknown>(
export function szudzikPairing(x: number, y: number): number {
return x >= y ? x * x + x + y : x + y * y;
}
export function throttle<T extends (...args: any[]) => ReturnType<T>>(fn: T, delay: number) {
let waiting = false;
let waitingArgs: Parameters<T> | undefined;
return function (this: unknown, ...args: Parameters<T>) {
if (waiting) {
waitingArgs = args;
return;
}
waiting = true;
fn.apply(this, args);
setTimeout(() => {
waiting = false;
if (waitingArgs != null) {
fn.apply(this, waitingArgs);
}
}, delay);
};
}

+ 0
- 21
src/webviews/apps/shared/utils.ts View File

@ -1,21 +0,0 @@
export function throttle(callback: (this: unknown, ...args: unknown[]) => unknown, limit: number) {
let waiting = false;
let pending = false;
return function (this: unknown, ...args: unknown[]) {
if (waiting) {
pending = true;
return;
}
callback.apply(this, args);
waiting = true;
setTimeout(() => {
waiting = false;
if (pending) {
callback.apply(this, args);
pending = false;
}
}, limit);
};
}

Loading…
Cancel
Save