mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 11:42:04 +08:00
fix: Require core modules used for inspector lazily (#4977)
This commit is contained in:

committed by
GitHub

parent
f7a3a36b9c
commit
0fe1806aaf
@ -1,4 +1,6 @@
|
|||||||
import * as application from "tns-core-modules/application";
|
console.log("####### ------ APP MODULES START ")
|
||||||
|
|
||||||
|
import * as application from "tns-core-modules/application";
|
||||||
import * as trace from "tns-core-modules/trace";
|
import * as trace from "tns-core-modules/trace";
|
||||||
trace.enable();
|
trace.enable();
|
||||||
trace.setCategories(trace.categories.concat(
|
trace.setCategories(trace.categories.concat(
|
||||||
|
@ -1,8 +1,20 @@
|
|||||||
import { unsetValue } from "../ui/core/properties";
|
|
||||||
import { ViewBase } from "../ui/core/view-base";
|
|
||||||
import { topmost } from "../ui/frame";
|
|
||||||
import { getNodeById } from "./dom-node";
|
import { getNodeById } from "./dom-node";
|
||||||
|
|
||||||
|
// Needed for typings only
|
||||||
|
import { ViewBase } from "../ui/core/view-base";
|
||||||
|
|
||||||
|
// Use lazy requires for core modules
|
||||||
|
const frameTopmost = () => { return require("../ui/frame").topmost(); };
|
||||||
|
|
||||||
|
let unsetValue;
|
||||||
|
function unsetViewValue(view, name) {
|
||||||
|
if (!unsetValue) {
|
||||||
|
unsetValue = require("../ui/core/properties").unsetValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
view[name] = unsetValue;
|
||||||
|
}
|
||||||
|
|
||||||
function getViewById(nodeId: number): ViewBase {
|
function getViewById(nodeId: number): ViewBase {
|
||||||
const node = getNodeById(nodeId);
|
const node = getNodeById(nodeId);
|
||||||
let view;
|
let view;
|
||||||
@ -14,13 +26,17 @@ function getViewById(nodeId: number): ViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getDocument() {
|
export function getDocument() {
|
||||||
const topMostFrame = topmost();
|
const topMostFrame = frameTopmost();
|
||||||
topMostFrame.ensureDomNode();
|
try {
|
||||||
|
topMostFrame.ensureDomNode();
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
console.log("ERROR in getDocument(): " + e);
|
||||||
|
}
|
||||||
return topMostFrame.domNode.toObject();
|
return topMostFrame.domNode.toObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getComputedStylesForNode(nodeId): Array<{ name: string, value: string}> {
|
export function getComputedStylesForNode(nodeId): Array<{ name: string, value: string }> {
|
||||||
const view = getViewById(nodeId);
|
const view = getViewById(nodeId);
|
||||||
if (view) {
|
if (view) {
|
||||||
return view.domNode.getComputedProperties();
|
return view.domNode.getComputedProperties();
|
||||||
@ -60,7 +76,7 @@ export function setAttributeAsText(nodeId, text, name) {
|
|||||||
|
|
||||||
// if attr name is being replaced with another
|
// if attr name is being replaced with another
|
||||||
if (name !== attrName && hasOriginalAttribute) {
|
if (name !== attrName && hasOriginalAttribute) {
|
||||||
view[name] = unsetValue;
|
unsetViewValue(view, name);
|
||||||
view[attrName] = attrValue;
|
view[attrName] = attrValue;
|
||||||
} else {
|
} else {
|
||||||
view[hasOriginalAttribute ? name : attrName] = attrValue;
|
view[hasOriginalAttribute ? name : attrName] = attrValue;
|
||||||
@ -68,7 +84,7 @@ export function setAttributeAsText(nodeId, text, name) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// delete attribute
|
// delete attribute
|
||||||
view[name] = unsetValue;
|
unsetViewValue(view, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
view.domNode.loadAttributes();
|
view.domNode.loadAttributes();
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
import { getSetProperties, getComputedCssValues } from "../ui/core/properties";
|
|
||||||
import { PercentLength } from "../ui/styling/style-properties";
|
|
||||||
import { ViewBase } from "../ui/core/view";
|
|
||||||
import { Color } from "../color";
|
|
||||||
import { CSSComputedStyleProperty } from "./css-agent";
|
import { CSSComputedStyleProperty } from "./css-agent";
|
||||||
import { InspectorEvents } from "./devtools-elements";
|
import { InspectorEvents } from "./devtools-elements";
|
||||||
|
|
||||||
|
// Needed for typings only
|
||||||
|
import { ViewBase } from "../ui/core/view";
|
||||||
|
|
||||||
const registeredDomNodes = {};
|
const registeredDomNodes = {};
|
||||||
const ELEMENT_NODE_TYPE = 1;
|
const ELEMENT_NODE_TYPE = 1;
|
||||||
const ROOT_NODE_TYPE = 9;
|
const ROOT_NODE_TYPE = 9;
|
||||||
@ -36,12 +35,19 @@ const propertyBlacklist = [
|
|||||||
"nativeView"
|
"nativeView"
|
||||||
];
|
];
|
||||||
|
|
||||||
let inspectorFrontendInstance: any;
|
function lazy<T>(action: () => T): () => T {
|
||||||
|
let _value: T;
|
||||||
|
return () => _value || (_value = action());
|
||||||
|
}
|
||||||
|
const percentLengthToStringLazy = lazy<(length) => string>(() => require("../ui/styling/style-properties").PercentLength.convertToString);
|
||||||
|
const getSetPropertiesLazy = lazy<(view: ViewBase) => [string, any][]>(() => require("../ui/core/properties").getSetProperties);
|
||||||
|
const getComputedCssValuesLazy = lazy<(view: ViewBase) => [string, any][]>(() => require("../ui/core/properties").getComputedCssValues);
|
||||||
|
|
||||||
export function registerInspectorEvents(inspector: InspectorEvents) {
|
export function registerInspectorEvents(inspector: InspectorEvents) {
|
||||||
inspectorFrontendInstance = inspector;
|
inspectorFrontendInstance = inspector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let inspectorFrontendInstance: any;
|
||||||
function notifyInspector(callback: (inspector: InspectorEvents) => void) {
|
function notifyInspector(callback: (inspector: InspectorEvents) => void) {
|
||||||
if (inspectorFrontendInstance) {
|
if (inspectorFrontendInstance) {
|
||||||
callback(inspectorFrontendInstance);
|
callback(inspectorFrontendInstance);
|
||||||
@ -51,10 +57,8 @@ function notifyInspector(callback: (inspector: InspectorEvents) => void) {
|
|||||||
function valueToString(value: any): string {
|
function valueToString(value: any): string {
|
||||||
if (typeof value === "undefined" || value === null) {
|
if (typeof value === "undefined" || value === null) {
|
||||||
return "";
|
return "";
|
||||||
} else if (value instanceof Color) {
|
|
||||||
return value.toString();
|
|
||||||
} else if (typeof value === "object" && value.unit) {
|
} else if (typeof value === "object" && value.unit) {
|
||||||
return PercentLength.convertToString(value);
|
return percentLengthToStringLazy()(value);
|
||||||
} else {
|
} else {
|
||||||
return value + "";
|
return value + "";
|
||||||
}
|
}
|
||||||
@ -112,7 +116,7 @@ export class DOMNode {
|
|||||||
|
|
||||||
public loadAttributes() {
|
public loadAttributes() {
|
||||||
this.attributes = [];
|
this.attributes = [];
|
||||||
getSetProperties(this.viewRef.get())
|
getSetPropertiesLazy()(this.viewRef.get())
|
||||||
.filter(propertyFilter)
|
.filter(propertyFilter)
|
||||||
.forEach(pair => this.attributes.push(pair[0], pair[1] + ""));
|
.forEach(pair => this.attributes.push(pair[0], pair[1] + ""));
|
||||||
|
|
||||||
@ -182,7 +186,7 @@ export class DOMNode {
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = getComputedCssValues(view)
|
const result = getComputedCssValuesLazy()(view)
|
||||||
.filter(pair => pair[0][0] !== "_")
|
.filter(pair => pair[0][0] !== "_")
|
||||||
.map((pair) => {
|
.map((pair) => {
|
||||||
return {
|
return {
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
console.log("Loading inspector modules...");
|
||||||
require("./globals/decorators");
|
require("./globals/decorators");
|
||||||
require("./debugger/webinspector-network");
|
require("./debugger/webinspector-network");
|
||||||
require("./debugger/webinspector-dom");
|
require("./debugger/webinspector-dom");
|
||||||
require("./debugger/webinspector-css");
|
require("./debugger/webinspector-css");
|
||||||
|
console.log("Finished loading inspector modules.");
|
||||||
|
Reference in New Issue
Block a user