diff --git a/tests/app/utils/utils-tests.ts b/tests/app/utils/utils-tests.ts index 3dba797a2..ac9dd2ab0 100644 --- a/tests/app/utils/utils-tests.ts +++ b/tests/app/utils/utils-tests.ts @@ -18,7 +18,6 @@ export function test_releaseNativeObject_canBeCalledWithNativeObject() { } }; - export function test_executeOnMainThread_Works(done: Function) { utils.executeOnMainThread(() => { 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) { const expectedN = 434; const expectedB = true; @@ -49,7 +59,6 @@ export function test_mainThreadify_PassesArgs(done: Function) { f(expectedN, expectedB, expectedS); } - function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() { let deallocated = false; const obj = new ((NSObject).extend({ diff --git a/tns-core-modules/utils/utils-common.ts b/tns-core-modules/utils/utils-common.ts index d94022b0f..de5df72af 100644 --- a/tns-core-modules/utils/utils-common.ts +++ b/tns-core-modules/utils/utils-common.ts @@ -1,5 +1,5 @@ import * as types from "./types"; -import { executeOnMainThread } from "./utils" +import { dispatchToMainThread, isMainThread } from "./utils" export const RESOURCE_PREFIX = "res://"; export const FILE_PREFIX = "file:///"; @@ -156,6 +156,14 @@ export function eliminateDuplicates(arr: Array): Array { 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 { return function () { const argsToPass = arguments; diff --git a/tns-core-modules/utils/utils.android.ts b/tns-core-modules/utils/utils.android.ts index 196dedcda..bd7d9b854 100644 --- a/tns-core-modules/utils/utils.android.ts +++ b/tns-core-modules/utils/utils.android.ts @@ -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()) .post(new java.lang.Runnable({ run: func diff --git a/tns-core-modules/utils/utils.d.ts b/tns-core-modules/utils/utils.d.ts index 308eefc0b..05d9e08d5 100644 --- a/tns-core-modules/utils/utils.d.ts +++ b/tns-core-modules/utils/utils.d.ts @@ -273,6 +273,13 @@ export function releaseNativeObject(object: any /*java.lang.Object | NSObject*/) * Dispatches the passed function for execution 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); /** diff --git a/tns-core-modules/utils/utils.ios.ts b/tns-core-modules/utils/utils.ios.ts index 95ccbe318..494a03a15 100644 --- a/tns-core-modules/utils/utils.ios.ts +++ b/tns-core-modules/utils/utils.ios.ts @@ -159,7 +159,7 @@ export function openUrl(location: string): boolean { return false; } -export function executeOnMainThread(func: () => void) { +export function dispatchToMainThread(func: () => void) { NSOperationQueue.mainQueue.addOperationWithBlock(func); }