feat: added Utils.executeOnUIThread

This commit is contained in:
Nathan Walker
2022-04-25 08:36:20 -07:00
parent 74e42fcb8b
commit 36a55dac7f
7 changed files with 41 additions and 3 deletions

View File

@ -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;

View File

@ -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,

View File

@ -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

View File

@ -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();
}
};
}

View File

@ -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);

View File

@ -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();
}
};
}

View File

@ -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;