mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-14 10:01:08 +08:00
feat: added Utils.executeOnUIThread
This commit is contained in:
3
packages/core/index.d.ts
vendored
3
packages/core/index.d.ts
vendored
@ -103,7 +103,7 @@ 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, queueGC, throttle, debounce } from './utils';
|
||||
import { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, executeOnUIThread, 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;
|
||||
@ -117,6 +117,7 @@ export declare const Utils: {
|
||||
isDataURI: typeof isDataURI;
|
||||
isFileOrResourcePath: typeof isFileOrResourcePath;
|
||||
executeOnMainThread: typeof executeOnMainThread;
|
||||
executeOnUIThread: typeof executeOnUIThread;
|
||||
mainThreadify: typeof mainThreadify;
|
||||
isMainThread: typeof isMainThread;
|
||||
dispatchToMainThread: typeof dispatchToMainThread;
|
||||
|
@ -129,7 +129,7 @@ export * from './trace';
|
||||
|
||||
export * from './ui';
|
||||
|
||||
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 { GC, isFontIconURI, isDataURI, isFileOrResourcePath, executeOnMainThread, mainThreadify, isMainThread, dispatchToMainThread, executeOnUIThread, 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 = {
|
||||
@ -140,6 +140,7 @@ export const Utils = {
|
||||
isDataURI,
|
||||
isFileOrResourcePath,
|
||||
executeOnMainThread,
|
||||
executeOnUIThread,
|
||||
mainThreadify,
|
||||
isMainThread,
|
||||
dispatchToMainThread,
|
||||
|
6
packages/core/utils/index.d.ts
vendored
6
packages/core/utils/index.d.ts
vendored
@ -227,6 +227,12 @@ export function queueMacrotask(task: () => void): void;
|
||||
*/
|
||||
export function executeOnMainThread(func: Function);
|
||||
|
||||
/**
|
||||
* Runs the passed function on the UI Thread.
|
||||
* @param func The function to execute on the UI thread.
|
||||
*/
|
||||
export function executeOnUIThread(func: Function);
|
||||
|
||||
/**
|
||||
* Returns a function wrapper which executes the supplied function on the main thread.
|
||||
* The wrapper behaves like the original function and passes all of its arguments BUT
|
||||
|
@ -9,3 +9,11 @@ export function dispatchToMainThread(func: () => void) {
|
||||
export function isMainThread(): boolean {
|
||||
return android.os.Looper.myLooper() === android.os.Looper.getMainLooper();
|
||||
}
|
||||
|
||||
export function dispatchToUIThread(func: () => void) {
|
||||
return function (func) {
|
||||
if (func) {
|
||||
func();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
6
packages/core/utils/mainthread-helper.d.ts
vendored
6
packages/core/utils/mainthread-helper.d.ts
vendored
@ -8,3 +8,9 @@ export function dispatchToMainThread(func: Function);
|
||||
* @returns Boolean value indicating whether the current thread is the main thread
|
||||
*/
|
||||
export function isMainThread(): boolean;
|
||||
|
||||
/**
|
||||
* Dispatches the passed function for execution on the UI thread
|
||||
* @param func The function to execute on the UI thread.
|
||||
*/
|
||||
export function dispatchToUIThread(func: Function);
|
||||
|
@ -5,3 +5,15 @@ export function dispatchToMainThread(func: () => void) {
|
||||
export function isMainThread(): boolean {
|
||||
return NSThread.isMainThread;
|
||||
}
|
||||
|
||||
export function dispatchToUIThread(func: () => void) {
|
||||
const runloop = CFRunLoopGetMain();
|
||||
return function (func) {
|
||||
if (runloop && func) {
|
||||
CFRunLoopPerformBlock(runloop, kCFRunLoopDefaultMode, func);
|
||||
CFRunLoopWakeUp(runloop);
|
||||
} else if (func) {
|
||||
func();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import * as types from './types';
|
||||
import { dispatchToMainThread, isMainThread } from './mainthread-helper';
|
||||
import { dispatchToMainThread, dispatchToUIThread, isMainThread } from './mainthread-helper';
|
||||
import { sanitizeModuleName } from '../ui/builder/module-name-sanitizer';
|
||||
import * as layout from './layout-helper';
|
||||
|
||||
@ -125,6 +125,10 @@ export function executeOnMainThread(func: Function) {
|
||||
}
|
||||
}
|
||||
|
||||
export function executeOnUIThread(func: Function) {
|
||||
dispatchToUIThread(func);
|
||||
}
|
||||
|
||||
export function mainThreadify(func: Function): (...args: any[]) => void {
|
||||
return function (...args) {
|
||||
const argsToPass = args;
|
||||
|
Reference in New Issue
Block a user