refact(utils): Check current thread and dispatch only if needed

This commit is contained in:
Martin Bektchiev
2019-05-08 14:22:06 +03:00
parent c60f74d4eb
commit a2ef6cbb4b
5 changed files with 29 additions and 5 deletions

View File

@@ -18,7 +18,6 @@ export function test_releaseNativeObject_canBeCalledWithNativeObject() {
} }
}; };
export function test_executeOnMainThread_Works(done: Function) { export function test_executeOnMainThread_Works(done: Function) {
utils.executeOnMainThread(() => { utils.executeOnMainThread(() => {
try { try {
@@ -30,6 +29,17 @@ export function test_executeOnMainThread_Works(done: Function) {
}); });
} }
export function test_dispatchToMainThread_Works(done: Function) {
utils.dispatchToMainThread(() => {
try {
TKUnit.assertTrue(utils.isMainThread());
done();
} catch (e) {
done(e);
}
});
}
export function test_mainThreadify_PassesArgs(done: Function) { export function test_mainThreadify_PassesArgs(done: Function) {
const expectedN = 434; const expectedN = 434;
const expectedB = true; const expectedB = true;
@@ -49,7 +59,6 @@ export function test_mainThreadify_PassesArgs(done: Function) {
f(expectedN, expectedB, expectedS); f(expectedN, expectedB, expectedS);
} }
function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() { function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() {
let deallocated = false; let deallocated = false;
const obj = new ((<any>NSObject).extend({ const obj = new ((<any>NSObject).extend({

View File

@@ -1,5 +1,5 @@
import * as types from "./types"; import * as types from "./types";
import { executeOnMainThread } from "./utils" import { dispatchToMainThread, isMainThread } from "./utils"
export const RESOURCE_PREFIX = "res://"; export const RESOURCE_PREFIX = "res://";
export const FILE_PREFIX = "file:///"; export const FILE_PREFIX = "file:///";
@@ -156,6 +156,14 @@ export function eliminateDuplicates(arr: Array<any>): Array<any> {
return Array.from(new Set(arr)); return Array.from(new Set(arr));
} }
export function executeOnMainThread(func: Function) {
if (isMainThread()) {
return func();
} else {
dispatchToMainThread(func);
}
}
export function mainThreadify(func: Function): (...args: any[]) => void { export function mainThreadify(func: Function): (...args: any[]) => void {
return function () { return function () {
const argsToPass = arguments; const argsToPass = arguments;

View File

@@ -373,7 +373,7 @@ Please ensure you have your manifest correctly configured with the FileProvider.
} }
} }
export function executeOnMainThread(func: () => void) { export function dispatchToMainThread(func: () => void) {
new android.os.Handler(android.os.Looper.getMainLooper()) new android.os.Handler(android.os.Looper.getMainLooper())
.post(new java.lang.Runnable({ .post(new java.lang.Runnable({
run: func run: func

View File

@@ -273,6 +273,13 @@ export function releaseNativeObject(object: any /*java.lang.Object | NSObject*/)
* Dispatches the passed function for execution on the main thread * Dispatches the passed function for execution on the main thread
* @param func The function to execute on the main thread. * @param func The function to execute on the main thread.
*/ */
export function dispatchToMainThread(func: Function);
/**
* Checks if the current thread is the main thread. Directly calls the passed function
* if it is, or dispatches it to the main thread otherwise.
* @param func The function to execute on the main thread.
*/
export function executeOnMainThread(func: Function); export function executeOnMainThread(func: Function);
/** /**

View File

@@ -159,7 +159,7 @@ export function openUrl(location: string): boolean {
return false; return false;
} }
export function executeOnMainThread(func: () => void) { export function dispatchToMainThread(func: () => void) {
NSOperationQueue.mainQueue.addOperationWithBlock(func); NSOperationQueue.mainQueue.addOperationWithBlock(func);
} }