mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 01:43:14 +08:00
feat: Utils for queueGC, debounce and throttle
This commit is contained in:
5
packages/core/index.d.ts
vendored
5
packages/core/index.d.ts
vendored
@ -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;
|
||||
|
@ -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,
|
||||
|
20
packages/core/utils/index.d.ts
vendored
20
packages/core/utils/index.d.ts
vendored
@ -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
|
||||
|
@ -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<any>) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user