diff --git a/packages/display/src/DisplayObject.ts b/packages/display/src/DisplayObject.ts index d9a8269069..85dcff6207 100644 --- a/packages/display/src/DisplayObject.ts +++ b/packages/display/src/DisplayObject.ts @@ -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 @@ -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; @@ -730,7 +730,8 @@ export abstract class DisplayObject extends EventEmitter this.interactive = false; this.interactiveChildren = false; - this._destroyed = true; + this.emit('destroyed'); + this.removeAllListeners(); } /** diff --git a/packages/display/test/DisplayObject.tests.ts b/packages/display/test/DisplayObject.tests.ts index a4df3c6c1a..b609451b85 100755 --- a/packages/display/test/DisplayObject.tests.ts +++ b/packages/display/test/DisplayObject.tests.ts @@ -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); + }); }); });