MOBILE-3808 utils: Add throttle function
parent
a5be9fbc95
commit
88e82350bf
|
@ -1541,7 +1541,6 @@ export class CoreUtilsProvider {
|
||||||
/**
|
/**
|
||||||
* Debounce a function so consecutive calls are ignored until a certain time has passed since the last call.
|
* Debounce a function so consecutive calls are ignored until a certain time has passed since the last call.
|
||||||
*
|
*
|
||||||
* @param context The context to apply to the function.
|
|
||||||
* @param fn Function to debounce.
|
* @param fn Function to debounce.
|
||||||
* @param delay Time that must pass until the function is called.
|
* @param delay Time that must pass until the function is called.
|
||||||
* @return Debounced function.
|
* @return Debounced function.
|
||||||
|
@ -1558,6 +1557,31 @@ export class CoreUtilsProvider {
|
||||||
return debounced;
|
return debounced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Throttle a function so consecutive calls are ignored until a certain time has passed since the last executed call.
|
||||||
|
*
|
||||||
|
* @param fn Function to throttle.
|
||||||
|
* @param duration Time that must pass until the function is called.
|
||||||
|
* @return Throttled function.
|
||||||
|
*/
|
||||||
|
throttle<T extends unknown[]>(fn: (...args: T) => unknown, duration: number): (...args: T) => void {
|
||||||
|
let shouldWait = false;
|
||||||
|
|
||||||
|
const throttled = (...args: T): void => {
|
||||||
|
if (!shouldWait) {
|
||||||
|
fn.apply(null, args);
|
||||||
|
|
||||||
|
shouldWait = true;
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
shouldWait = false;
|
||||||
|
}, duration);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return throttled;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the app can scan QR codes.
|
* Check whether the app can scan QR codes.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue