Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Improvements for debugger.html source tree
diff --git a/src/components/PrimaryPanes/SourcesTree.js b/src/components/PrimaryPanes/SourcesTree.js
index eadd8041..79576b1e 100644
--- a/src/components/PrimaryPanes/SourcesTree.js
+++ b/src/components/PrimaryPanes/SourcesTree.js
@@ -242,7 +242,6 @@ class SourcesTree extends Component {
onContextMenu={e => this.onContextMenu(e, item)}
>
- {arrow}
- {icon}
+ {nodeHasChildren(item) ? arrow : null}
<span className="label"> {item.name} </span>
</div>
);
diff --git a/src/utils/sources-tree/addToTree.js b/src/utils/sources-tree/addToTree.js
index c3ab0b11..1e514edf 100644
--- a/src/utils/sources-tree/addToTree.js
+++ b/src/utils/sources-tree/addToTree.js
@@ -15,7 +15,17 @@ import type { SourceRecord } from "../../reducers/types";
function createNodeInTree(part: string, path: string, tree: Node) {
const node = createNode(part, path, []);
// we are modifying the tree
- tree.contents = [...tree.contents, node];
+ const clone = tree.contents.slice(0);
+ clone.push(node);
+ tree.contents = clone;
+
+ let treeNameIndex = nodeNamesIndex.get(tree);
+ if (!treeNameIndex) {
+ treeNameIndex = Object.create(null);
+ nodeNamesIndex.set(tree, treeNameIndex);
+ }
+ treeNameIndex[part] = node;
+
return node;
}
@@ -33,7 +43,8 @@ function findOrCreateNode(
index: number,
url: Object
) {
- const child = subTree.contents.find(c => c.name === part);
+ const namesIndex = nodeNamesIndex.get(subTree);
+ const child = namesIndex ? namesIndex[part] : undefined;
// we create and enter the new node
if (!child) {
@@ -116,3 +127,5 @@ export function addToTree(
const finalNode = traverseTree(url, tree);
finalNode.contents = addSourceToNode(finalNode, url, source);
}
+
+let nodeNamesIndex = new WeakMap();
diff --git a/src/utils/sources-tree/sortTree.js b/src/utils/sources-tree/sortTree.js
index 48475aee..503c7aad 100644
--- a/src/utils/sources-tree/sortTree.js
+++ b/src/utils/sources-tree/sortTree.js
@@ -1,4 +1,19 @@
import { nodeHasChildren, isExactUrlMatch } from "./utils";
+import { parse } from "url";
+
+function parseHost(debuggeeUrl) {
+ let { host } = parse(debuggeeUrl);
+ if (!host) {
+ return null;
+ }
+ return host.replace(/^www\./, "");
+}
+
+function isExactUrlMatch0(pathPart: string, debuggeeHost: string) {
+ return pathPart.indexOf("www.") === 0 ?
+ debuggeeHost === pathPart.substr(4) :
+ debuggeeHost === pathPart;
+}
/**
* Look at the nodes in the source tree, and determine the index of where to
@@ -7,9 +22,14 @@ import { nodeHasChildren, isExactUrlMatch } from "./utils";
* @static
*/
export function sortEntireTree(tree, debuggeeUrl = "") {
+ const debuggeeHost = parseHost(debuggeeUrl);
+ return sortEntireTree0(tree, debuggeeHost);
+}
+
+function sortEntireTree0(tree, debuggeeHost = null) {
if (nodeHasChildren(tree)) {
- const contents = sortTree(tree, debuggeeUrl).map(subtree =>
- sortEntireTree(subtree)
+ const contents = sortTree0(tree, debuggeeHost).map(subtree =>
+ sortEntireTree0(subtree)
);
return { ...tree, contents };
}
@@ -23,6 +43,11 @@ export function sortEntireTree(tree, debuggeeUrl = "") {
* @static
*/
export function sortTree(tree, debuggeeUrl = "") {
+ const debuggeeHost = parseHost(debuggeeUrl);
+ return sortTree0(debuggeeHost, debuggeeHost);
+}
+
+function sortTree0(tree, debuggeeHost) {
return tree.contents.sort((previousNode, currentNode) => {
const currentNodeIsDir = nodeHasChildren(currentNode);
const previousNodeIsDir = nodeHasChildren(previousNode);
@@ -30,9 +55,9 @@ export function sortTree(tree, debuggeeUrl = "") {
return 1;
} else if (previousNode.name === "(index)") {
return -1;
- } else if (isExactUrlMatch(currentNode.name, debuggeeUrl)) {
+ } else if (debuggeeHost !== null && isExactUrlMatch0(currentNode.name, debuggeeHost)) {
return 1;
- } else if (isExactUrlMatch(previousNode.name, debuggeeUrl)) {
+ } else if (debuggeeHost !== null && isExactUrlMatch0(previousNode.name, debuggeeHost)) {
return -1;
// If neither is the case, continue to compare alphabetically
} else if (previousNodeIsDir && !currentNodeIsDir) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.