Match-id-094551ccf405a03b88a0a038c149dac31ae48e12
This commit is contained in:
parent
18a5f36604
commit
d31bbc937d
|
@ -1,4 +1,4 @@
|
||||||
import parseTreeRoot, { deleteVNode, queryVNode } from '../parser/parseVNode';
|
import parseTreeRoot, { clearVNode, queryVNode } from '../parser/parseVNode';
|
||||||
import { packagePayload, checkMessage } from './../utils/transferTool';
|
import { packagePayload, checkMessage } from './../utils/transferTool';
|
||||||
import {
|
import {
|
||||||
RequestAllVNodeTreeInfos,
|
RequestAllVNodeTreeInfos,
|
||||||
|
@ -12,6 +12,32 @@ import { VNode } from './../../../horizon/src/renderer/vnode/VNode';
|
||||||
import { ClassComponent } from '../../../horizon/src/renderer/vnode/VNodeTags';
|
import { ClassComponent } from '../../../horizon/src/renderer/vnode/VNodeTags';
|
||||||
import { parseAttr } from '../parser/parseAttr';
|
import { parseAttr } from '../parser/parseAttr';
|
||||||
|
|
||||||
|
const roots = [];
|
||||||
|
|
||||||
|
function addIfNotInclude(treeRoot: VNode) {
|
||||||
|
if (!roots.includes(treeRoot)) {
|
||||||
|
roots.push(treeRoot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function send() {
|
||||||
|
const result = roots.reduce((pre, current) => {
|
||||||
|
const info = parseTreeRoot(current);
|
||||||
|
pre.push(info);
|
||||||
|
return pre;
|
||||||
|
}, []);
|
||||||
|
postMessage(AllVNodeTreesInfos, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function deleteVNode(vNode: VNode) {
|
||||||
|
// 开发工具中保存了 vNode 的引用,在清理 VNode 的时候需要一并删除
|
||||||
|
clearVNode(vNode);
|
||||||
|
const index = roots.indexOf(vNode);
|
||||||
|
if (index !== -1) {
|
||||||
|
roots.splice(index, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function postMessage(type: string, data) {
|
function postMessage(type: string, data) {
|
||||||
window.postMessage(packagePayload({
|
window.postMessage(packagePayload({
|
||||||
type: type,
|
type: type,
|
||||||
|
@ -19,7 +45,19 @@ function postMessage(type: string, data) {
|
||||||
}, DevToolHook), '*');
|
}, DevToolHook), '*');
|
||||||
}
|
}
|
||||||
|
|
||||||
const roots = [];
|
function parseCompAttrs(id: number) {
|
||||||
|
const vNode: VNode = queryVNode(id);
|
||||||
|
const tag = vNode.tag;
|
||||||
|
if (tag === ClassComponent) {
|
||||||
|
const { props, state } = vNode;
|
||||||
|
const parsedProps = parseAttr(props);
|
||||||
|
const parsedState = parseAttr(state);
|
||||||
|
postMessage(ComponentAttrs, {
|
||||||
|
parsedProps,
|
||||||
|
parsedState,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function injectHook() {
|
function injectHook() {
|
||||||
if (window.__HORIZON_DEV_HOOK__) {
|
if (window.__HORIZON_DEV_HOOK__) {
|
||||||
|
@ -28,27 +66,9 @@ function injectHook() {
|
||||||
Object.defineProperty(window, '__HORIZON_DEV_HOOK__', {
|
Object.defineProperty(window, '__HORIZON_DEV_HOOK__', {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
value: {
|
value: {
|
||||||
addIfNotInclude: function (treeRoot: VNode) {
|
addIfNotInclude,
|
||||||
if (!roots.includes(treeRoot)) {
|
send,
|
||||||
roots.push(treeRoot);
|
deleteVNode,
|
||||||
}
|
|
||||||
},
|
|
||||||
send: function () {
|
|
||||||
const result = roots.reduce((pre, current) => {
|
|
||||||
const info = parseTreeRoot(current);
|
|
||||||
pre.push(info);
|
|
||||||
return pre;
|
|
||||||
}, []);
|
|
||||||
postMessage(AllVNodeTreesInfos, result);
|
|
||||||
},
|
|
||||||
delete: function (vNode: VNode) {
|
|
||||||
// 开发工具中保存了 vNode 的引用,在清理 VNode 的时候需要一并删除
|
|
||||||
deleteVNode(vNode);
|
|
||||||
const index = roots.indexOf(vNode);
|
|
||||||
if (index !== -1) {
|
|
||||||
roots.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
window.addEventListener('message', function (event) {
|
window.addEventListener('message', function (event) {
|
||||||
|
@ -61,26 +81,12 @@ function injectHook() {
|
||||||
const { payload } = request;
|
const { payload } = request;
|
||||||
const { type, data } = payload;
|
const { type, data } = payload;
|
||||||
if (type === RequestAllVNodeTreeInfos) {
|
if (type === RequestAllVNodeTreeInfos) {
|
||||||
const result = roots.reduce((pre, current) => {
|
send();
|
||||||
const info = parseTreeRoot(current);
|
|
||||||
pre.push(info);
|
|
||||||
return pre;
|
|
||||||
}, []);
|
|
||||||
postMessage(AllVNodeTreesInfos, result);
|
|
||||||
} else if (type === RequestComponentAttrs) {
|
} else if (type === RequestComponentAttrs) {
|
||||||
const vNode: VNode = queryVNode(data);
|
parseCompAttrs(data);
|
||||||
const tag = vNode.tag;
|
|
||||||
if (tag === ClassComponent) {
|
|
||||||
const { props, state } = vNode;
|
|
||||||
const parsedProps = parseAttr(props);
|
|
||||||
const parsedState = parseAttr(state);
|
|
||||||
postMessage(ComponentAttrs, {
|
|
||||||
parsedProps,
|
|
||||||
parsedState,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
injectHook();
|
injectHook();
|
||||||
|
|
|
@ -61,7 +61,7 @@ export function queryVNode(id: number) {
|
||||||
return IdToVNodeMap.get(id);
|
return IdToVNodeMap.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function deleteVNode(vNode: VNode) {
|
export function clearVNode(vNode: VNode) {
|
||||||
if (VNodeToIdMap.has(vNode)) {
|
if (VNodeToIdMap.has(vNode)) {
|
||||||
const id = VNodeToIdMap.get(vNode);
|
const id = VNodeToIdMap.get(vNode);
|
||||||
VNodeToIdMap.delete(vNode);
|
VNodeToIdMap.delete(vNode);
|
||||||
|
|
Loading…
Reference in New Issue