feat: Utils for queueGC, debounce and throttle

This commit is contained in:
Nathan Walker
2022-02-26 10:14:23 -08:00
parent 9e6371fdaf
commit 40c5984966
4 changed files with 55 additions and 16 deletions

View File

@ -103,13 +103,16 @@ export type { InstrumentationMode, TimerInfo } from './profiling';
export { encoding } from './text'; export { encoding } from './text';
export * from './trace'; export * from './trace';
export * from './ui'; export * from './ui';
import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject, getModuleName, openFile, openUrl, isRealDevice, layout, ad as androidUtils, iOSNativeHelper as iosUtils, Source, escapeRegexSymbols, convertString, dismissSoftInput, queueMacrotask } from './utils'; import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, releaseNativeObject, getModuleName, openFile, openUrl, isRealDevice, layout, ad as androidUtils, iOSNativeHelper as iosUtils, Source, escapeRegexSymbols, convertString, dismissSoftInput, queueMacrotask, queueGC, throttle, debounce } from './utils';
import { ClassInfo, getClass, getBaseClasses, getClassInfo, isBoolean, isDefined, isFunction, isNullOrUndefined, isNumber, isObject, isString, isUndefined, toUIString, verifyCallback } from './utils/types'; import { ClassInfo, getClass, getBaseClasses, getClassInfo, isBoolean, isDefined, isFunction, isNullOrUndefined, isNumber, isObject, isString, isUndefined, toUIString, verifyCallback } from './utils/types';
export declare const Utils: { export declare const Utils: {
GC: typeof GC; GC: typeof GC;
RESOURCE_PREFIX: string; RESOURCE_PREFIX: string;
FILE_PREFIX: string; FILE_PREFIX: string;
queueMacrotask: typeof queueMacrotask; queueMacrotask: typeof queueMacrotask;
queueGC: typeof queueGC;
debounce: typeof debounce;
throttle: typeof throttle;
isFontIconURI: typeof isFontIconURI; isFontIconURI: typeof isFontIconURI;
isDataURI: typeof isDataURI; isDataURI: typeof isDataURI;
isFileOrResourcePath: typeof isFileOrResourcePath; isFileOrResourcePath: typeof isFileOrResourcePath;

View File

@ -129,7 +129,7 @@ export * from './trace';
export * from './ui'; export * from './ui';
import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, queueMacrotask, releaseNativeObject, getModuleName, openFile, openUrl, isRealDevice, layout, ad as androidUtils, iOSNativeHelper as iosUtils, Source, RESOURCE_PREFIX, FILE_PREFIX, escapeRegexSymbols, convertString, dismissSoftInput } from './utils'; import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, queueMacrotask, queueGC, debounce, throttle, releaseNativeObject, getModuleName, openFile, openUrl, isRealDevice, layout, ad as androidUtils, iOSNativeHelper as iosUtils, Source, RESOURCE_PREFIX, FILE_PREFIX, escapeRegexSymbols, convertString, dismissSoftInput } from './utils';
import { ClassInfo, getClass, getBaseClasses, getClassInfo, isBoolean, isDefined, isFunction, isNullOrUndefined, isNumber, isObject, isString, isUndefined, toUIString, verifyCallback } from './utils/types'; import { ClassInfo, getClass, getBaseClasses, getClassInfo, isBoolean, isDefined, isFunction, isNullOrUndefined, isNumber, isObject, isString, isUndefined, toUIString, verifyCallback } from './utils/types';
export const Utils = { export const Utils = {
@ -144,6 +144,9 @@ export const Utils = {
isMainThread, isMainThread,
dispatchToMainThread, dispatchToMainThread,
queueMacrotask, queueMacrotask,
queueGC,
debounce,
throttle,
releaseNativeObject, releaseNativeObject,
convertString, convertString,
escapeRegexSymbols, escapeRegexSymbols,

View File

@ -188,9 +188,25 @@ export namespace ad {
export function GC(); export function GC();
/** /**
* An utility function that queues a garbage collection, subseqent calls will be throttled and only one gc will be executed. * An utility function that queues a garbage collection, multiple calls in quick succession are debounced by default and only one gc will be executed after 900ms.
* @param delay Customize the delay
* @param useThrottle Instead of default debounce strategy, use throttling
*/ */
export function queueGC(); export function queueGC(delay?: number, useThrottle?: boolean);
/**
* A simple throttle utility
* @param fn Function to throttle
* @param delay Customize the delay (default is 300ms)
*/
export function throttle(fn: any, delay?: number);
/**
* A simple debounce utility
* @param fn Function to debounce
* @param delay Customize the delay (default is 300ms)
*/
export function debounce(fn: any, delay?: number);
/** /**
* Releases the reference to the wrapped native object * Releases the reference to the wrapped native object

View File

@ -132,16 +132,33 @@ export function mainThreadify(func: Function): (...args: any[]) => void {
}; };
} }
let hasQueuedGC = false; export function debounce(fn: any, delay = 300) {
export function queueGC() { let timer: NodeJS.Timeout;
if (hasQueuedGC) { return (...args: Array<any>) => {
return; clearTimeout(timer);
} timer = setTimeout(() => {
fn.apply(this, args);
hasQueuedGC = true; }, delay);
};
setTimeout(() => { }
hasQueuedGC = false;
GC(); export function throttle(fn: any, delay = 300) {
}, 1000); let waiting = false;
return function () {
if (!waiting) {
fn.apply(this, arguments);
waiting = true;
setTimeout(function () {
waiting = false;
}, delay);
}
};
}
export function queueGC(delay = 900, useThrottle?: boolean) {
if (useThrottle) {
throttle(() => GC(), delay);
} else {
debounce(() => GC(), delay);
}
} }