From 36a55dac7fd9db4ace98ac0a19286d8cfd0410bf Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Mon, 25 Apr 2022 08:36:20 -0700 Subject: [PATCH] feat: added Utils.executeOnUIThread --- packages/core/index.d.ts | 3 ++- packages/core/index.ts | 3 ++- packages/core/utils/index.d.ts | 6 ++++++ packages/core/utils/mainthread-helper.android.ts | 8 ++++++++ packages/core/utils/mainthread-helper.d.ts | 6 ++++++ packages/core/utils/mainthread-helper.ios.ts | 12 ++++++++++++ packages/core/utils/utils-common.ts | 6 +++++- 7 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/core/index.d.ts b/packages/core/index.d.ts index 9ccf23cb4..57604d5db 100644 --- a/packages/core/index.d.ts +++ b/packages/core/index.d.ts @@ -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; diff --git a/packages/core/index.ts b/packages/core/index.ts index cee10d382..73428e106 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -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, diff --git a/packages/core/utils/index.d.ts b/packages/core/utils/index.d.ts index 2e7993dd2..83f5049f7 100644 --- a/packages/core/utils/index.d.ts +++ b/packages/core/utils/index.d.ts @@ -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 diff --git a/packages/core/utils/mainthread-helper.android.ts b/packages/core/utils/mainthread-helper.android.ts index 35a8bdf9b..d3ee92147 100644 --- a/packages/core/utils/mainthread-helper.android.ts +++ b/packages/core/utils/mainthread-helper.android.ts @@ -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(); + } + }; +} diff --git a/packages/core/utils/mainthread-helper.d.ts b/packages/core/utils/mainthread-helper.d.ts index ed75cecdd..e78cb28c1 100644 --- a/packages/core/utils/mainthread-helper.d.ts +++ b/packages/core/utils/mainthread-helper.d.ts @@ -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); diff --git a/packages/core/utils/mainthread-helper.ios.ts b/packages/core/utils/mainthread-helper.ios.ts index 61bcee65c..e1bef0281 100644 --- a/packages/core/utils/mainthread-helper.ios.ts +++ b/packages/core/utils/mainthread-helper.ios.ts @@ -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(); + } + }; +} diff --git a/packages/core/utils/utils-common.ts b/packages/core/utils/utils-common.ts index f67ebbb2d..4410e6fdb 100644 --- a/packages/core/utils/utils-common.ts +++ b/packages/core/utils/utils-common.ts @@ -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;