mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
refact(utils): Check current thread and dispatch only if needed
This commit is contained in:
@@ -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({
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
7
tns-core-modules/utils/utils.d.ts
vendored
7
tns-core-modules/utils/utils.d.ts
vendored
@@ -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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user