diff --git a/packages/core/index.d.ts b/packages/core/index.d.ts index 4d3b0a309..9ccf23cb4 100644 --- a/packages/core/index.d.ts +++ b/packages/core/index.d.ts @@ -103,13 +103,16 @@ export type { InstrumentationMode, TimerInfo } from './profiling'; export { encoding } from './text'; export * from './trace'; 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'; export declare const Utils: { GC: typeof GC; RESOURCE_PREFIX: string; FILE_PREFIX: string; queueMacrotask: typeof queueMacrotask; + queueGC: typeof queueGC; + debounce: typeof debounce; + throttle: typeof throttle; isFontIconURI: typeof isFontIconURI; isDataURI: typeof isDataURI; isFileOrResourcePath: typeof isFileOrResourcePath; diff --git a/packages/core/index.ts b/packages/core/index.ts index ac43c40c3..cee10d382 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -129,7 +129,7 @@ export * from './trace'; 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'; export const Utils = { @@ -144,6 +144,9 @@ export const Utils = { isMainThread, dispatchToMainThread, queueMacrotask, + queueGC, + debounce, + throttle, releaseNativeObject, convertString, escapeRegexSymbols, diff --git a/packages/core/utils/index.d.ts b/packages/core/utils/index.d.ts index 48ab142b3..2e7993dd2 100644 --- a/packages/core/utils/index.d.ts +++ b/packages/core/utils/index.d.ts @@ -188,9 +188,25 @@ export namespace ad { 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 diff --git a/packages/core/utils/utils-common.ts b/packages/core/utils/utils-common.ts index 0706ac58a..ae78b2335 100644 --- a/packages/core/utils/utils-common.ts +++ b/packages/core/utils/utils-common.ts @@ -132,16 +132,33 @@ export function mainThreadify(func: Function): (...args: any[]) => void { }; } -let hasQueuedGC = false; -export function queueGC() { - if (hasQueuedGC) { - return; - } - - hasQueuedGC = true; - - setTimeout(() => { - hasQueuedGC = false; - GC(); - }, 1000); +export function debounce(fn: any, delay = 300) { + let timer: NodeJS.Timeout; + return (...args: Array) => { + clearTimeout(timer); + timer = setTimeout(() => { + fn.apply(this, args); + }, delay); + }; +} + +export function throttle(fn: any, delay = 300) { + 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); + } }