Skip to content

Commit e5c6903

Browse files
authored
Implement/use GraphNode#remove (#5469)
* Implement/use `GraphNode#remove` * Add unit test
1 parent 6ed5f97 commit e5c6903

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

src/framework/components/sprite/component.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,8 @@ class SpriteComponent extends Component {
596596
this._hideModel();
597597
this._model = null;
598598

599-
if (this._node) {
600-
if (this._node.parent)
601-
this._node.parent.removeChild(this._node);
602-
this._node = null;
603-
}
599+
this._node?.remove();
600+
this._node = null;
604601

605602
if (this._meshInstance) {
606603
// make sure we decrease the ref counts materials and meshes

src/scene/graph-node.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ class GraphNode extends EventHandler {
490490
*/
491491
destroy() {
492492
// Detach from parent
493-
this._parent?.removeChild(this);
493+
this.remove();
494494

495495
// Recursively destroy all children
496496
const children = this._children;
@@ -955,18 +955,21 @@ class GraphNode extends EventHandler {
955955
return this._worldScaleSign;
956956
}
957957

958+
/**
959+
* Remove graph node from current parent.
960+
*/
961+
remove() {
962+
this._parent?.removeChild(this);
963+
}
964+
958965
/**
959966
* Remove graph node from current parent and add as child to new parent.
960967
*
961968
* @param {GraphNode} parent - New parent to attach graph node to.
962969
* @param {number} [index] - The child index where the child node should be placed.
963970
*/
964971
reparent(parent, index) {
965-
const current = this._parent;
966-
967-
if (current)
968-
current.removeChild(this);
969-
972+
this.remove();
970973
if (parent) {
971974
if (index >= 0) {
972975
parent.insertChild(this, index);
@@ -1294,9 +1297,7 @@ class GraphNode extends EventHandler {
12941297
_prepareInsertChild(node) {
12951298

12961299
// remove it from the existing parent
1297-
if (node._parent) {
1298-
node._parent.removeChild(node);
1299-
}
1300+
node.remove();
13001301

13011302
Debug.assert(node !== this, `GraphNode ${node?.name} cannot be a child of itself`);
13021303
Debug.assert(!this.isDescendantOf(node), `GraphNode ${node?.name} cannot add an ancestor as a child`);

test/scene/graph-node.test.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,19 @@ describe('GraphNode', function () {
634634

635635
});
636636

637+
describe('#remove', function () {
638+
639+
it('removes the node from its parent, unparenting it', function () {
640+
const node = new GraphNode();
641+
const child = new GraphNode();
642+
node.addChild(child);
643+
child.remove();
644+
expect(node.children).to.be.an('array').with.lengthOf(0);
645+
expect(child.parent).to.equal(null);
646+
});
647+
648+
});
649+
637650
describe('#removeChild()', function () {
638651

639652
it('removes a child node', function () {

0 commit comments

Comments
 (0)