Skip to content

Commit f2121b4

Browse files
committed
Use child-attached/child-detached events to update SceneGraph when an entity is created by a component (fix #756)
1 parent 3534957 commit f2121b4

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/components/scenegraph/SceneGraph.js

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export default class SceneGraph extends React.Component {
3030
selectedIndex: -1
3131
};
3232

33+
this.rebuildEntityOptions = debounce(
34+
this.rebuildEntityOptions.bind(this),
35+
0
36+
);
37+
3338
this.updateFilteredEntities = debounce(
3439
this.updateFilteredEntities.bind(this),
3540
100
@@ -42,19 +47,31 @@ export default class SceneGraph extends React.Component {
4247
}
4348
};
4449

50+
onChildAttachedDetached = (event) => {
51+
if (this.includeInSceneGraph(event.detail.el)) {
52+
this.rebuildEntityOptions();
53+
}
54+
};
55+
4556
componentDidMount() {
4657
this.rebuildEntityOptions();
4758
Events.on('entityidchange', this.rebuildEntityOptions);
48-
Events.on('entitycreated', this.rebuildEntityOptions);
49-
Events.on('entityclone', this.rebuildEntityOptions);
5059
Events.on('entityupdate', this.onEntityUpdate);
60+
document.addEventListener('child-attached', this.onChildAttachedDetached);
61+
document.addEventListener('child-detached', this.onChildAttachedDetached);
5162
}
5263

5364
componentWillUnmount() {
5465
Events.off('entityidchange', this.rebuildEntityOptions);
55-
Events.off('entitycreated', this.rebuildEntityOptions);
56-
Events.off('entityclone', this.rebuildEntityOptions);
5766
Events.off('entityupdate', this.onEntityUpdate);
67+
document.removeEventListener(
68+
'child-attached',
69+
this.onChildAttachedDetached
70+
);
71+
document.removeEventListener(
72+
'child-detached',
73+
this.onChildAttachedDetached
74+
);
5875
}
5976

6077
/**
@@ -100,10 +117,19 @@ export default class SceneGraph extends React.Component {
100117
}
101118
};
102119

120+
includeInSceneGraph = (element) => {
121+
return !(
122+
element.dataset.isInspector ||
123+
!element.isEntity ||
124+
element.isInspector ||
125+
'aframeInspector' in element.dataset
126+
);
127+
};
128+
103129
rebuildEntityOptions = () => {
104130
const entities = [{ depth: 0, entity: this.props.scene }];
105131

106-
function treeIterate(element, depth) {
132+
const treeIterate = (element, depth) => {
107133
if (!element) {
108134
return;
109135
}
@@ -112,12 +138,7 @@ export default class SceneGraph extends React.Component {
112138
for (let i = 0; i < element.children.length; i++) {
113139
let entity = element.children[i];
114140

115-
if (
116-
entity.dataset.isInspector ||
117-
!entity.isEntity ||
118-
entity.isInspector ||
119-
'aframeInspector' in entity.dataset
120-
) {
141+
if (!this.includeInSceneGraph(entity)) {
121142
continue;
122143
}
123144

@@ -129,7 +150,7 @@ export default class SceneGraph extends React.Component {
129150

130151
treeIterate(entity, depth);
131152
}
132-
}
153+
};
133154
treeIterate(this.props.scene, 0);
134155

135156
this.setState({

0 commit comments

Comments
 (0)