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

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