From 0710ff875df7a83ff58a3a28ceb63c43ad40c61b Mon Sep 17 00:00:00 2001 From: cha0s Date: Fri, 3 May 2019 01:22:15 -0500 Subject: [PATCH] fix: only tick emitter on client and wait to remove tick handler until after destroy finishes --- packages/graphics/traits/emitter.trait.js | 25 +++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/packages/graphics/traits/emitter.trait.js b/packages/graphics/traits/emitter.trait.js index a57f33e..1dd8666 100644 --- a/packages/graphics/traits/emitter.trait.js +++ b/packages/graphics/traits/emitter.trait.js @@ -9,14 +9,10 @@ export class Emitter extends decorate(Trait) { initialize() { this.emitters = {}; - this.ticker = new Ticker(1 / 10); - this.ticker.on('tick', (elapsed) => { - this.updateFrequency(); - for (const key in this.emitters) { - const emitter = this.emitters[key]; - emitter.tick(elapsed); - } - }); + if (AVOCADO_CLIENT) { + this.ticker = new Ticker(1 / 10); + this.ticker.on('tick', this.onTick, this); + } } static addEmitter(emitter) { @@ -46,6 +42,14 @@ export class Emitter extends decorate(Trait) { this._emitters.splice(index, 1); } + onTick(elapsed) { + this.updateFrequency(); + for (const key in this.emitters) { + const emitter = this.emitters[key]; + emitter.tick(elapsed); + } + } + updateFrequency() { const particleCount = this.constructor.particleCount(); const updatesPerSecond = Math.max(15, (60 - (particleCount / 10))); @@ -61,6 +65,7 @@ export class Emitter extends decorate(Trait) { const emitter = this.emitters[key]; this.ticker.tick(elapsed); if (!emitter.hasParticles()) { + this.ticker.off('tick', this.onTick); emitter.destroy(); this.constructor.removeEmitter(emitter); delete this.emitters[key]; @@ -105,7 +110,9 @@ export class Emitter extends decorate(Trait) { } tick(elapsed) { - this.ticker.tick(elapsed); + if (AVOCADO_CLIENT) { + this.ticker.tick(elapsed); + } } }