Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit c4f3be0

Browse files
committed
Use symbols for nodes path.
Fixes #1001, #1002 & #1003. The path property on a node is very important as it serve as an identifier to know if a node is expanded, and what its properties are. Currently, this path was a simple string which could led to 2 nodes having the same path, which could have unwanted effect on user interaction. To avoid this, the path is now a symbol, which has the advantage of being unique. Because of this change, we needed to adapt our code so we always have the same path reference in the state. With this patch, we'll always dispatch the nodePropertiesLoaded action, even if the node don't have any properties. We'll store null in the state, so we can then check if a node was properly handled. As a result, we change the shouldComponentUpdate of the component to reflect this new behavior. In order to have always the same reference, we always add created node to the cacheNodes property, which we can do because of the change in loadedProperties. This should also give us some performance improvements as we don't have to recreate some nodes twice. We take this as an opportunity to change the signature of the createNode function to be more explicit (it now expect an object of options), and we also create the path of the node from its parent when the path option is not supplied.
1 parent b6819f6 commit c4f3be0

File tree

3 files changed

+163
-146
lines changed

3 files changed

+163
-146
lines changed

packages/devtools-reps/src/object-inspector/actions.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ function nodeExpand(
3939
type: "NODE_EXPAND",
4040
data: {node}
4141
});
42-
dispatch(nodeLoadProperties(node, actor, loadedProperties, createObjectClient));
42+
43+
if (!loadedProperties.has(node.path)) {
44+
dispatch(nodeLoadProperties(node, actor, loadedProperties, createObjectClient));
45+
}
4346
};
4447
}
4548

@@ -70,9 +73,7 @@ function nodeLoadProperties(
7073
try {
7174
const properties =
7275
await loadItemProperties(item, createObjectClient, loadedProperties);
73-
if (Object.keys(properties).length > 0) {
74-
dispatch(nodePropertiesLoaded(item, actor, properties));
75-
}
76+
dispatch(nodePropertiesLoaded(item, actor, properties));
7677
} catch (e) {
7778
console.error(e);
7879
}

packages/devtools-reps/src/object-inspector/component.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,19 @@ class ObjectInspector extends Component {
113113
return true;
114114
}
115115

116-
return expandedPaths.size !== nextProps.expandedPaths.size
117-
|| loadedProperties.size !== nextProps.loadedProperties.size
118-
|| [...expandedPaths].some(key => !nextProps.expandedPaths.has(key));
116+
// We should update if:
117+
// - there are new loaded loaded properties
118+
// - OR the expanded paths number changed, and all of them have properties loaded
119+
// - OR the expanded paths number did not changed, but old and new sets differ
120+
return loadedProperties.size !== nextProps.loadedProperties.size
121+
|| (
122+
expandedPaths.size !== nextProps.expandedPaths.size &&
123+
[...nextProps.expandedPaths].every(path => nextProps.loadedProperties.has(path))
124+
)
125+
|| (
126+
expandedPaths.size === nextProps.expandedPaths.size &&
127+
[...nextProps.expandedPaths].some(key => !expandedPaths.has(key))
128+
);
119129
}
120130

121131
componentWillUnmount() {
@@ -151,7 +161,9 @@ class ObjectInspector extends Component {
151161
}
152162

153163
getNodeKey(item: Node) : string {
154-
return item.path || JSON.stringify(item);
164+
return item.path && typeof item.path.toString === "function"
165+
? item.path.toString()
166+
: JSON.stringify(item);
155167
}
156168

157169
setExpanded(item: Node, expand: boolean) {
@@ -426,7 +438,7 @@ class ObjectInspector extends Component {
426438
autoExpandDepth,
427439
disabledFocus,
428440

429-
isExpanded: item => expandedPaths && expandedPaths.has(this.getNodeKey(item)),
441+
isExpanded: item => expandedPaths && expandedPaths.has(item.path),
430442
isExpandable: item => nodeIsPrimitive(item) === false,
431443
focused: focusedItem,
432444

0 commit comments

Comments
 (0)