mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-02 19:12:40 +08:00
feat: added Utils.executeOnUIThread
This commit is contained in:
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