Skip to content

Commit

Permalink
Fix DisplayObject destroyed flag and event (pixijs#8297)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljherrmann committed May 3, 2022
1 parent a520915 commit b0bfad4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/display/src/DisplayObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ export abstract class DisplayObject extends EventEmitter
*/

/**
* Fired when this DisplayObject is destroyed.
* Fired when this DisplayObject is destroyed. This event is emitted once
* destroy is finished.
*
* @instance
* @event destroyed
Expand Down Expand Up @@ -714,8 +715,7 @@ export abstract class DisplayObject extends EventEmitter
{
this.parent.removeChild(this);
}
this.emit('destroyed');
this.removeAllListeners();
this._destroyed = true;
this.transform = null;

this.parent = null;
Expand All @@ -730,7 +730,8 @@ export abstract class DisplayObject extends EventEmitter
this.interactive = false;
this.interactiveChildren = false;

this._destroyed = true;
this.emit('destroyed');
this.removeAllListeners();
}

/**
Expand Down
24 changes: 24 additions & 0 deletions packages/display/test/DisplayObject.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,29 @@ describe('DisplayObject', function ()

expect(listener.calledOnce).to.be.true;
});

it('should trigger destroyed listeners once destruction is complete', function ()
{
let listenerCallCount = 0;
const child = new DisplayObject();
const container = new Container();

child.on('destroyed', () =>
{
listenerCallCount++;
expect(child.destroyed).to.be.true;
expect(child.parent).to.be.null;
});

container.addChild(child);
container.removeChild(child);

expect(listenerCallCount).to.equal(0);

container.addChild(child);
child.destroy();

expect(listenerCallCount).to.equal(1);
});
});
});

0 comments on commit b0bfad4

Please sign in to comment.