mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +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 { 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;
|
||||||
|
@ -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,
|
||||||
|
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();
|
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
|
||||||
|
@ -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);
|
||||||
|
}, delay);
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
hasQueuedGC = true;
|
export function throttle(fn: any, delay = 300) {
|
||||||
|
let waiting = false;
|
||||||
setTimeout(() => {
|
return function () {
|
||||||
hasQueuedGC = false;
|
if (!waiting) {
|
||||||
GC();
|
fn.apply(this, arguments);
|
||||||
}, 1000);
|
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