From 69fef3dbf4e0e9cfc9756a3831042c7eb596c447 Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Tue, 13 Jun 2017 20:26:40 +0200 Subject: [PATCH] Ensure start and end of stabilization progress events is sent (#3165) Fix for #3106 Event `stabilizationProgress` is now always sent for iteration 0 and for the very last stabilization iteration. --- lib/network/modules/PhysicsEngine.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/network/modules/PhysicsEngine.js b/lib/network/modules/PhysicsEngine.js index 742e13d3..5f176a9d 100644 --- a/lib/network/modules/PhysicsEngine.js +++ b/lib/network/modules/PhysicsEngine.js @@ -638,20 +638,31 @@ class PhysicsEngine { * @private */ _stabilizationBatch() { + var self = this; + var running = () => (self.stabilized === false && self.stabilizationIterations < self.targetIterations); + var sendProgress = () => { + self.body.emitter.emit('stabilizationProgress', { + iterations: self.stabilizationIterations, + total: self.targetIterations + }); + }; + // this is here to ensure that there is at least one start event. if (this.startedStabilization === false) { this.body.emitter.emit('startStabilizing'); this.startedStabilization = true; + sendProgress(); } var count = 0; - while (this.stabilized === false && count < this.options.stabilization.updateInterval && this.stabilizationIterations < this.targetIterations) { + while (running() && count < this.options.stabilization.updateInterval) { this.physicsTick(); count++; } - if (this.stabilized === false && this.stabilizationIterations < this.targetIterations) { - this.body.emitter.emit('stabilizationProgress', {iterations: this.stabilizationIterations, total: this.targetIterations}); + sendProgress(); + + if (running()) { setTimeout(this._stabilizationBatch.bind(this),0); } else {