| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f51bb11 commit c60f74d
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,6 +18,38 @@ export function test_releaseNativeObject_canBeCalledWithNativeObject() { | |||
| 18 | 18 | } | |
| 19 | 19 | }; | |
| 20 | 20 | ||
| 21 | + | ||
| 22 | + export function test_executeOnMainThread_Works(done: Function) { | ||
| 23 | + utils.executeOnMainThread(() => { | ||
| 24 | + try { | ||
| 25 | + TKUnit.assertTrue(utils.isMainThread()); | ||
| 26 | + done(); | ||
| 27 | + } catch (e) { | ||
| 28 | + done(e); | ||
| 29 | + } | ||
| 30 | + }); | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | + export function test_mainThreadify_PassesArgs(done: Function) { | ||
| 34 | + const expectedN = 434; | ||
| 35 | + const expectedB = true; | ||
| 36 | + const expectedS = "string"; | ||
| 37 | + const f = utils.mainThreadify(function (n: number, b: boolean, s: string) { | ||
| 38 | + try { | ||
| 39 | + TKUnit.assertTrue(utils.isMainThread()); | ||
| 40 | + TKUnit.assertEqual(n, expectedN); | ||
| 41 | + TKUnit.assertEqual(b, expectedB); | ||
| 42 | + TKUnit.assertEqual(s, expectedS); | ||
| 43 | + done(); | ||
| 44 | + } catch (e) { | ||
| 45 | + done(e); | ||
| 46 | + } | ||
| 47 | + }); | ||
| 48 | + | ||
| 49 | + f(expectedN, expectedB, expectedS); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + | ||
| 21 | 53 | function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() { | |
| 22 | 54 | let deallocated = false; | |
| 23 | 55 | const obj = new ((<any>NSObject).extend({ | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,7 @@ import { getNodeById } from "./dom-node"; | |||
| 2 | 2 | ||
| 3 | 3 | // Needed for typings only | |
| 4 | 4 | import { ViewBase } from "../ui/core/view-base"; | |
| 5 | + import { mainThreadify } from "../utils/utils"; | ||
| 5 | 6 | ||
| 6 | 7 | // Use lazy requires for core modules | |
| 7 | 8 | const frameTopmost = () => require("../ui/frame").topmost(); | |
@@ -11,7 +12,7 @@ function unsetViewValue(view, name) { | |||
| 11 | 12 | if (!unsetValue) { | |
| 12 | 13 | unsetValue = require("../ui/core/properties").unsetValue; | |
| 13 | 14 | } | |
| 14 | - | ||
| 15 | + | ||
| 15 | 16 | view[name] = unsetValue; | |
| 16 | 17 | } | |
| 17 | 18 | ||
@@ -30,10 +31,10 @@ export function getDocument() { | |||
| 30 | 31 | if (!topMostFrame) { | |
| 31 | 32 | return undefined; | |
| 32 | 33 | } | |
| 33 | - | ||
| 34 | + | ||
| 34 | 35 | try { | |
| 35 | 36 | topMostFrame.ensureDomNode(); | |
| 36 | - | ||
| 37 | + | ||
| 37 | 38 | } catch (e) { | |
| 38 | 39 | console.log("ERROR in getDocument(): " + e); | |
| 39 | 40 | } | |
@@ -49,7 +50,7 @@ export function getComputedStylesForNode(nodeId): Array<{ name: string, value: s | |||
| 49 | 50 | return []; | |
| 50 | 51 | } | |
| 51 | 52 | ||
| 52 | - export function removeNode(nodeId) { | ||
| 53 | + export const removeNode = mainThreadify(function removeNode(nodeId) { | ||
| 53 | 54 | const view = getViewById(nodeId); | |
| 54 | 55 | if (view) { | |
| 55 | 56 | // Avoid importing layout and content view | |
@@ -63,9 +64,9 @@ export function removeNode(nodeId) { | |||
| 63 | 64 | console.log("Can't remove child from " + parent); | |
| 64 | 65 | } | |
| 65 | 66 | } | |
| 66 | - } | ||
| 67 | + }); | ||
| 67 | 68 | ||
| 68 | - export function setAttributeAsText(nodeId, text, name) { | ||
| 69 | + export const setAttributeAsText = mainThreadify(function setAttributeAsText(nodeId, text, name) { | ||
| 69 | 70 | const view = getViewById(nodeId); | |
| 70 | 71 | if (view) { | |
| 71 | 72 | // attribute is registered for the view instance | |
@@ -93,4 +94,4 @@ export function setAttributeAsText(nodeId, text, name) { | |||
| 93 | 94 | ||
| 94 | 95 | view.domNode.loadAttributes(); | |
| 95 | 96 | } | |
| 96 | - } | ||
| 97 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | import * as types from "./types"; | |
| 2 | + import { executeOnMainThread } from "./utils" | ||
| 2 | 3 | ||
| 3 | 4 | export const RESOURCE_PREFIX = "res://"; | |
| 4 | 5 | export const FILE_PREFIX = "file:///"; | |
@@ -154,3 +155,10 @@ export function hasDuplicates(arr: Array<any>): boolean { | |||
| 154 | 155 | export function eliminateDuplicates(arr: Array<any>): Array<any> { | |
| 155 | 156 | return Array.from(new Set(arr)); | |
| 156 | 157 | } | |
| 158 | + | ||
| 159 | + export function mainThreadify(func: Function): (...args: any[]) => void { | ||
| 160 | + return function () { | ||
| 161 | + const argsToPass = arguments; | ||
| 162 | + executeOnMainThread(() => func.apply(this, argsToPass)); | ||
| 163 | + } | ||
| 164 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -372,3 +372,14 @@ Please ensure you have your manifest correctly configured with the FileProvider. | |||
| 372 | 372 | return false; | |
| 373 | 373 | } | |
| 374 | 374 | } | |
| 375 | + | ||
| 376 | + export function executeOnMainThread(func: () => void) { | ||
| 377 | + new android.os.Handler(android.os.Looper.getMainLooper()) | ||
| 378 | + .post(new java.lang.Runnable({ | ||
| 379 | + run: func | ||
| 380 | + })); | ||
| 381 | + } | ||
| 382 | + | ||
| 383 | + export function isMainThread(): Boolean { | ||
| 384 | + return android.os.Looper.myLooper() === android.os.Looper.getMainLooper(); | ||
| 385 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -269,6 +269,26 @@ export function GC(); | |||
| 269 | 269 | */ | |
| 270 | 270 | export function releaseNativeObject(object: any /*java.lang.Object | NSObject*/); | |
| 271 | 271 | ||
| 272 | + /** | ||
| 273 | + * Dispatches the passed function for execution on the main thread | ||
| 274 | + * @param func The function to execute on the main thread. | ||
| 275 | + */ | ||
| 276 | + export function executeOnMainThread(func: Function); | ||
| 277 | + | ||
| 278 | + /** | ||
| 279 | + * Returns a function wrapper which executes the supplied function on the main thread. | ||
| 280 | + * The wrapper behaves like the original function and passes all of its arguments BUT | ||
| 281 | + * discards its return value. | ||
| 282 | + * @param func The function to execute on the main thread | ||
| 283 | + * @returns The wrapper function which schedules execution to the main thread | ||
| 284 | + */ | ||
| 285 | + export function mainThreadify(func: Function): (...args: any[]) => void | ||
| 286 | + | ||
| 287 | + /** | ||
| 288 | + * @returns Boolean value indicating whether the current thread is the main thread | ||
| 289 | + */ | ||
| 290 | + export function isMainThread(): boolean | ||
| 291 | + | ||
| 272 | 292 | /** | |
| 273 | 293 | * Returns true if the specified path points to a resource or local file. | |
| 274 | 294 | * @param path The path. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -159,6 +159,14 @@ export function openUrl(location: string): boolean { | |||
| 159 | 159 | return false; | |
| 160 | 160 | } | |
| 161 | 161 | ||
| 162 | + export function executeOnMainThread(func: () => void) { | ||
| 163 | + NSOperationQueue.mainQueue.addOperationWithBlock(func); | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + export function isMainThread(): Boolean { | ||
| 167 | + return NSThread.isMainThread; | ||
| 168 | + } | ||
| 169 | + | ||
| 162 | 170 | class UIDocumentInteractionControllerDelegateImpl extends NSObject implements UIDocumentInteractionControllerDelegate { | |
| 163 | 171 | public static ObjCProtocols = [UIDocumentInteractionControllerDelegate]; | |
| 164 | 172 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments