fix(devtools-ios): Ensure UI modifications run on main thread

Modifications to the UI can only be made from the main thread.
Since {N} 5.3.0 all debugger protocol messages are processed
by the worker thread that receives them in iOS.

refs #7219, https://github.com/NativeScript/ios-runtime/pull/1101
This commit is contained in:
Martin Bektchiev
2019-05-08 13:56:45 +03:00
parent f51bb119c2
commit c60f74d4eb
6 changed files with 87 additions and 7 deletions

View File

@@ -1,4 +1,5 @@
import * as types from "./types";
import { executeOnMainThread } from "./utils"
export const RESOURCE_PREFIX = "res://";
export const FILE_PREFIX = "file:///";
@@ -154,3 +155,10 @@ export function hasDuplicates(arr: Array<any>): boolean {
export function eliminateDuplicates(arr: Array<any>): Array<any> {
return Array.from(new Set(arr));
}
export function mainThreadify(func: Function): (...args: any[]) => void {
return function () {
const argsToPass = arguments;
executeOnMainThread(() => func.apply(this, argsToPass));
}
}

View File

@@ -372,3 +372,14 @@ Please ensure you have your manifest correctly configured with the FileProvider.
return false;
}
}
export function executeOnMainThread(func: () => void) {
new android.os.Handler(android.os.Looper.getMainLooper())
.post(new java.lang.Runnable({
run: func
}));
}
export function isMainThread(): Boolean {
return android.os.Looper.myLooper() === android.os.Looper.getMainLooper();
}

View File

@@ -269,6 +269,26 @@ export function GC();
*/
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 executeOnMainThread(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
* discards its return value.
* @param func The function to execute on the main thread
* @returns The wrapper function which schedules execution to the main thread
*/
export function mainThreadify(func: Function): (...args: any[]) => void
/**
* @returns Boolean value indicating whether the current thread is the main thread
*/
export function isMainThread(): boolean
/**
* Returns true if the specified path points to a resource or local file.
* @param path The path.

View File

@@ -159,6 +159,14 @@ export function openUrl(location: string): boolean {
return false;
}
export function executeOnMainThread(func: () => void) {
NSOperationQueue.mainQueue.addOperationWithBlock(func);
}
export function isMainThread(): Boolean {
return NSThread.isMainThread;
}
class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate {
public static ObjCProtocols = [UIDocumentInteractionControllerDelegate];