Refactor how native container ID is stored
Dan Abramov committed
Apr 23, 2016 at 22:27 UTC
829558b8b031f7906866949a1f9428154b49ed74
2 files changed
+15
-25
src/isomorphic/devtools/ReactComponentTreeDevtool.js
+14
-24
@@ -14,12 +14,12 @@
14
var invariant = require('invariant');
15
16
var unmountedContainerIDs = [];
17
-var allChildIDsByContainerID = {};
17
var tree = {};
18
19
function updateTree(id, update) {
20
if (!tree[id]) {
21
tree[id] = {
22
+ nativeContainerID: null,
23
parentID: null,
24
ownerID: null,
25
text: null,
@@ -33,18 +33,11 @@ function updateTree(id, update) {
33
34
function purgeTree(id) {
35
var item = tree[id];
36
- if (!item) {
37
- return;
36
+ if (item) {
37
+ var {childIDs} = item;
38
+ delete tree[id];
39
+ childIDs.forEach(purgeTree);
40
}
39
-
40
- var {childIDs, containerID} = item;
41
- delete tree[id];
42
-
43
- if (containerID) {
44
- allChildIDsByContainerID[containerID] = allChildIDsByContainerID[containerID]
45
- .filter(childID => childID !== id);
46
- }
47
- childIDs.forEach(purgeTree);
41
}
42
43
var ReactComponentTreeDevtool = {
@@ -114,27 +107,24 @@ var ReactComponentTreeDevtool = {
107
updateTree(id, item => item.text = text);
108
},
109
117
- onMountComponent(id, containerID) {
118
- if (!allChildIDsByContainerID[containerID]) {
119
- allChildIDsByContainerID[containerID] = [];
120
- }
121
- allChildIDsByContainerID[containerID].push(id);
122
- updateTree(id, item => item.containerID = containerID);
110
+ onMountComponent(id, nativeContainerID) {
111
+ updateTree(id, item => item.nativeContainerID = nativeContainerID);
112
},
113
114
onUnmountComponent(id) {
115
purgeTree(id);
116
},
117
129
- onUnmountNativeContainer(containerID) {
130
- unmountedContainerIDs.push(containerID);
118
+ onUnmountNativeContainer(nativeContainerID) {
119
+ unmountedContainerIDs.push(nativeContainerID);
120
},
121
133
- purgeUnmountedContainers() {
134
- unmountedContainerIDs.forEach(containerID => {
135
- allChildIDsByContainerID[containerID].forEach(purgeTree);
136
- });
122
+ purgeComponentsFromUnmountedContainers() {
123
+ var unmountedIDs = Object.keys(tree).filter(id =>
124
+ unmountedContainerIDs.indexOf(tree[id].nativeContainerID) !== -1
125
+ );
126
unmountedContainerIDs = [];
127
+ unmountedIDs.forEach(purgeTree);
128
},
129
130
isComposite(id) {
src/isomorphic/devtools/__tests__/ReactComponentTreeDevtool-test.js
+1
-1
@@ -101,7 +101,7 @@ describe('ReactComponentTreeDevtool', () => {
101
expect(
102
getTree(rootInstance._debugID, includeOwner).children[0]
103
).toEqual(expectedTree);
104
- ReactComponentTreeDevtool.purgeUnmountedContainers();
104
+ ReactComponentTreeDevtool.purgeComponentsFromUnmountedContainers();
105
expect(getRegisteredDisplayNames()).toEqual([]);
106
});
107
}