Merge branch 'master' into vchimev/app-root-frame-hmr-local-styles

This commit is contained in:
Svetoslav
2019-05-09 16:47:05 +02:00
committed by GitHub
11 changed files with 123 additions and 11 deletions

View File

@ -19,7 +19,6 @@
"devDependencies": {
"@types/chai": "~4.1.7",
"@types/mocha": "~5.2.5",
"@types/node": "~10.12.18",
"babel-traverse": "6.26.0",
"babel-types": "6.26.0",
"babylon": "6.18.0",

View File

@ -19,14 +19,13 @@
"devDependencies": {
"@types/chai": "~4.1.7",
"@types/mocha": "~5.2.5",
"@types/node": "^7.0.5",
"mocha": "~5.2.0",
"mochawesome": "~3.1.2",
"nativescript-dev-appium": "next",
"nativescript-dev-typescript": "next",
"nativescript-dev-webpack": "next",
"tns-platform-declarations": "next",
"rimraf": "^2.6.2",
"tns-platform-declarations": "next",
"typescript": "^3.1.6"
},
"scripts": {

View File

@ -19,7 +19,6 @@
"devDependencies": {
"@types/chai": "~4.1.7",
"@types/mocha": "~5.2.5",
"@types/node": "^7.0.5",
"mocha": "~5.2.0",
"mochawesome": "~3.1.2",
"nativescript-dev-appium": "next",

View File

@ -1,9 +1,19 @@
import * as imageCacheModule from "tns-core-modules/ui/image-cache";
import * as imageSource from "tns-core-modules/image-source";
import * as types from "tns-core-modules/utils/types";
import { device } from "tns-core-modules/platform";
import lazy from "tns-core-modules/utils/lazy";
import * as TKUnit from "../../TKUnit";
const sdkVersion = lazy(() => parseInt(device.sdkVersion));
export const test_ImageCache_ValidUrl = function() {
// see https://github.com/NativeScript/NativeScript/issues/6643
if (sdkVersion() < 20) {
return;
}
const cache = new imageCacheModule.Cache();
cache.maxRequests = 5;

View File

@ -18,6 +18,47 @@ export function test_releaseNativeObject_canBeCalledWithNativeObject() {
}
};
export function test_executeOnMainThread_Works(done: Function) {
utils.executeOnMainThread(() => {
try {
TKUnit.assertTrue(utils.isMainThread());
done();
} catch (e) {
done(e);
}
});
}
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;
const expectedS = "string";
const f = utils.mainThreadify(function (n: number, b: boolean, s: string) {
try {
TKUnit.assertTrue(utils.isMainThread());
TKUnit.assertEqual(n, expectedN);
TKUnit.assertEqual(b, expectedB);
TKUnit.assertEqual(s, expectedS);
done();
} catch (e) {
done(e);
}
});
f(expectedN, expectedB, expectedS);
}
function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() {
let deallocated = false;
const obj = new ((<any>NSObject).extend({

View File

@ -2,6 +2,7 @@ import { getNodeById } from "./dom-node";
// Needed for typings only
import { ViewBase } from "../ui/core/view-base";
import { mainThreadify } from "../utils/utils";
// Use lazy requires for core modules
const frameTopmost = () => require("../ui/frame").topmost();
@ -11,7 +12,7 @@ function unsetViewValue(view, name) {
if (!unsetValue) {
unsetValue = require("../ui/core/properties").unsetValue;
}
view[name] = unsetValue;
}
@ -30,10 +31,10 @@ export function getDocument() {
if (!topMostFrame) {
return undefined;
}
try {
topMostFrame.ensureDomNode();
} catch (e) {
console.log("ERROR in getDocument(): " + e);
}
@ -49,7 +50,7 @@ export function getComputedStylesForNode(nodeId): Array<{ name: string, value: s
return [];
}
export function removeNode(nodeId) {
export const removeNode = mainThreadify(function removeNode(nodeId) {
const view = getViewById(nodeId);
if (view) {
// Avoid importing layout and content view
@ -63,9 +64,9 @@ export function removeNode(nodeId) {
console.log("Can't remove child from " + parent);
}
}
}
});
export function setAttributeAsText(nodeId, text, name) {
export const setAttributeAsText = mainThreadify(function setAttributeAsText(nodeId, text, name) {
const view = getViewById(nodeId);
if (view) {
// attribute is registered for the view instance
@ -93,4 +94,4 @@ export function setAttributeAsText(nodeId, text, name) {
view.domNode.loadAttributes();
}
}
});

View File

@ -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();
}

View File

@ -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

View File

@ -0,0 +1,7 @@
export function dispatchToMainThread(func: () => void) {
NSOperationQueue.mainQueue.addOperationWithBlock(func);
}
export function isMainThread(): Boolean {
return NSThread.isMainThread;
}

View File

@ -1,4 +1,7 @@
import * as types from "./types";
import { dispatchToMainThread, isMainThread } from "./mainthread-helper"
export * from "./mainthread-helper"
export const RESOURCE_PREFIX = "res://";
export const FILE_PREFIX = "file:///";
@ -154,3 +157,18 @@ export function hasDuplicates(arr: Array<any>): boolean {
export function eliminateDuplicates(arr: Array<any>): Array<any> {
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;
executeOnMainThread(() => func.apply(this, argsToPass));
}
}

View File

@ -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,6 +271,22 @@ export function GC();
*/
export function releaseNativeObject(object: any /*java.lang.Object | NSObject*/);
/**
* 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);
/**
* 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 true if the specified path points to a resource or local file.
* @param path The path.