Skip to content

Commit 0fe1806

Browse files
author
Alexander Vakrilov
authored
fix: Require core modules used for inspector lazily (NativeScript#4977)
1 parent f7a3a36 commit 0fe1806

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

apps/app/ui-tests-app/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import * as application from "tns-core-modules/application";
1+
console.log("####### ------ APP MODULES START ")
2+
3+
import * as application from "tns-core-modules/application";
24
import * as trace from "tns-core-modules/trace";
35
trace.enable();
46
trace.setCategories(trace.categories.concat(

tns-core-modules/debugger/devtools-elements.common.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
import { unsetValue } from "../ui/core/properties";
2-
import { ViewBase } from "../ui/core/view-base";
3-
import { topmost } from "../ui/frame";
41
import { getNodeById } from "./dom-node";
52

3+
// Needed for typings only
4+
import { ViewBase } from "../ui/core/view-base";
5+
6+
// Use lazy requires for core modules
7+
const frameTopmost = () => { return require("../ui/frame").topmost(); };
8+
9+
let unsetValue;
10+
function unsetViewValue(view, name) {
11+
if (!unsetValue) {
12+
unsetValue = require("../ui/core/properties").unsetValue;
13+
}
14+
15+
view[name] = unsetValue;
16+
}
17+
618
function getViewById(nodeId: number): ViewBase {
719
const node = getNodeById(nodeId);
820
let view;
@@ -14,13 +26,17 @@ function getViewById(nodeId: number): ViewBase {
1426
}
1527

1628
export function getDocument() {
17-
const topMostFrame = topmost();
18-
topMostFrame.ensureDomNode();
19-
29+
const topMostFrame = frameTopmost();
30+
try {
31+
topMostFrame.ensureDomNode();
32+
33+
} catch (e) {
34+
console.log("ERROR in getDocument(): " + e);
35+
}
2036
return topMostFrame.domNode.toObject();
2137
}
2238

23-
export function getComputedStylesForNode(nodeId): Array<{ name: string, value: string}> {
39+
export function getComputedStylesForNode(nodeId): Array<{ name: string, value: string }> {
2440
const view = getViewById(nodeId);
2541
if (view) {
2642
return view.domNode.getComputedProperties();
@@ -60,15 +76,15 @@ export function setAttributeAsText(nodeId, text, name) {
6076

6177
// if attr name is being replaced with another
6278
if (name !== attrName && hasOriginalAttribute) {
63-
view[name] = unsetValue;
79+
unsetViewValue(view, name);
6480
view[attrName] = attrValue;
6581
} else {
6682
view[hasOriginalAttribute ? name : attrName] = attrValue;
6783
}
6884
}
6985
} else {
7086
// delete attribute
71-
view[name] = unsetValue;
87+
unsetViewValue(view, name);
7288
}
7389

7490
view.domNode.loadAttributes();

tns-core-modules/debugger/dom-node.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { getSetProperties, getComputedCssValues } from "../ui/core/properties";
2-
import { PercentLength } from "../ui/styling/style-properties";
3-
import { ViewBase } from "../ui/core/view";
4-
import { Color } from "../color";
51
import { CSSComputedStyleProperty } from "./css-agent";
62
import { InspectorEvents } from "./devtools-elements";
73

4+
// Needed for typings only
5+
import { ViewBase } from "../ui/core/view";
6+
87
const registeredDomNodes = {};
98
const ELEMENT_NODE_TYPE = 1;
109
const ROOT_NODE_TYPE = 9;
@@ -36,12 +35,19 @@ const propertyBlacklist = [
3635
"nativeView"
3736
];
3837

39-
let inspectorFrontendInstance: any;
38+
function lazy<T>(action: () => T): () => T {
39+
let _value: T;
40+
return () => _value || (_value = action());
41+
}
42+
const percentLengthToStringLazy = lazy<(length) => string>(() => require("../ui/styling/style-properties").PercentLength.convertToString);
43+
const getSetPropertiesLazy = lazy<(view: ViewBase) => [string, any][]>(() => require("../ui/core/properties").getSetProperties);
44+
const getComputedCssValuesLazy = lazy<(view: ViewBase) => [string, any][]>(() => require("../ui/core/properties").getComputedCssValues);
4045

4146
export function registerInspectorEvents(inspector: InspectorEvents) {
4247
inspectorFrontendInstance = inspector;
4348
}
4449

50+
let inspectorFrontendInstance: any;
4551
function notifyInspector(callback: (inspector: InspectorEvents) => void) {
4652
if (inspectorFrontendInstance) {
4753
callback(inspectorFrontendInstance);
@@ -51,10 +57,8 @@ function notifyInspector(callback: (inspector: InspectorEvents) => void) {
5157
function valueToString(value: any): string {
5258
if (typeof value === "undefined" || value === null) {
5359
return "";
54-
} else if (value instanceof Color) {
55-
return value.toString();
5660
} else if (typeof value === "object" && value.unit) {
57-
return PercentLength.convertToString(value);
61+
return percentLengthToStringLazy()(value);
5862
} else {
5963
return value + "";
6064
}
@@ -112,7 +116,7 @@ export class DOMNode {
112116

113117
public loadAttributes() {
114118
this.attributes = [];
115-
getSetProperties(this.viewRef.get())
119+
getSetPropertiesLazy()(this.viewRef.get())
116120
.filter(propertyFilter)
117121
.forEach(pair => this.attributes.push(pair[0], pair[1] + ""));
118122

@@ -182,7 +186,7 @@ export class DOMNode {
182186
return [];
183187
}
184188

185-
const result = getComputedCssValues(view)
189+
const result = getComputedCssValuesLazy()(view)
186190
.filter(pair => pair[0][0] !== "_")
187191
.map((pair) => {
188192
return {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
console.log("Loading inspector modules...");
12
require("./globals/decorators");
23
require("./debugger/webinspector-network");
34
require("./debugger/webinspector-dom");
45
require("./debugger/webinspector-css");
6+
console.log("Finished loading inspector modules.");

0 commit comments

Comments
 (0)