From 7aed770871598003ebe40445909e9bcc477a8d43 Mon Sep 17 00:00:00 2001 From: Martin Bektchiev Date: Wed, 8 May 2019 17:28:29 +0300 Subject: [PATCH] refact(utils): Resolve circular dependency without duplicating any code --- tns-core-modules/utils/mainthread-helper.android.ts | 10 ++++++++++ tns-core-modules/utils/mainthread-helper.d.ts | 10 ++++++++++ tns-core-modules/utils/mainthread-helper.ios.ts | 7 +++++++ tns-core-modules/utils/utils-common.ts | 4 +++- tns-core-modules/utils/utils.android.ts | 11 ----------- tns-core-modules/utils/utils.d.ts | 13 ++----------- tns-core-modules/utils/utils.ios.ts | 8 -------- 7 files changed, 32 insertions(+), 31 deletions(-) create mode 100644 tns-core-modules/utils/mainthread-helper.android.ts create mode 100644 tns-core-modules/utils/mainthread-helper.d.ts create mode 100644 tns-core-modules/utils/mainthread-helper.ios.ts diff --git a/tns-core-modules/utils/mainthread-helper.android.ts b/tns-core-modules/utils/mainthread-helper.android.ts new file mode 100644 index 000000000..1ef05eaa5 --- /dev/null +++ b/tns-core-modules/utils/mainthread-helper.android.ts @@ -0,0 +1,10 @@ +export function dispatchToMainThread(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(); +} diff --git a/tns-core-modules/utils/mainthread-helper.d.ts b/tns-core-modules/utils/mainthread-helper.d.ts new file mode 100644 index 000000000..46ff0142d --- /dev/null +++ b/tns-core-modules/utils/mainthread-helper.d.ts @@ -0,0 +1,10 @@ +/** + * 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); + +/** + * @returns Boolean value indicating whether the current thread is the main thread + */ +export function isMainThread(): boolean diff --git a/tns-core-modules/utils/mainthread-helper.ios.ts b/tns-core-modules/utils/mainthread-helper.ios.ts new file mode 100644 index 000000000..01ed3bb17 --- /dev/null +++ b/tns-core-modules/utils/mainthread-helper.ios.ts @@ -0,0 +1,7 @@ +export function dispatchToMainThread(func: () => void) { + NSOperationQueue.mainQueue.addOperationWithBlock(func); +} + +export function isMainThread(): Boolean { + return NSThread.isMainThread; +} diff --git a/tns-core-modules/utils/utils-common.ts b/tns-core-modules/utils/utils-common.ts index de5df72af..6141d8fe5 100644 --- a/tns-core-modules/utils/utils-common.ts +++ b/tns-core-modules/utils/utils-common.ts @@ -1,5 +1,7 @@ import * as types from "./types"; -import { dispatchToMainThread, isMainThread } from "./utils" +import { dispatchToMainThread, isMainThread } from "./mainthread-helper" + +export * from "./mainthread-helper" export const RESOURCE_PREFIX = "res://"; export const FILE_PREFIX = "file:///"; diff --git a/tns-core-modules/utils/utils.android.ts b/tns-core-modules/utils/utils.android.ts index bd7d9b854..b050042f9 100644 --- a/tns-core-modules/utils/utils.android.ts +++ b/tns-core-modules/utils/utils.android.ts @@ -372,14 +372,3 @@ Please ensure you have your manifest correctly configured with the FileProvider. return false; } } - -export function dispatchToMainThread(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(); -} diff --git a/tns-core-modules/utils/utils.d.ts b/tns-core-modules/utils/utils.d.ts index 05d9e08d5..08a1d80c6 100644 --- a/tns-core-modules/utils/utils.d.ts +++ b/tns-core-modules/utils/utils.d.ts @@ -4,6 +4,8 @@ import { dip, px } from "../ui/core/view"; +export * from "./mainthread-helper" + export const RESOURCE_PREFIX: string; export const FILE_PREFIX: string; @@ -269,12 +271,6 @@ 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 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. @@ -291,11 +287,6 @@ export function executeOnMainThread(func: Function); */ 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. diff --git a/tns-core-modules/utils/utils.ios.ts b/tns-core-modules/utils/utils.ios.ts index 494a03a15..941782f16 100644 --- a/tns-core-modules/utils/utils.ios.ts +++ b/tns-core-modules/utils/utils.ios.ts @@ -159,14 +159,6 @@ export function openUrl(location: string): boolean { return false; } -export function dispatchToMainThread(func: () => void) { - NSOperationQueue.mainQueue.addOperationWithBlock(func); -} - -export function isMainThread(): Boolean { - return NSThread.isMainThread; -} - class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { public static ObjCProtocols = [UIDocumentInteractionControllerDelegate];