diff --git a/package.json b/package.json index ce76d2f..2a6c8dd 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "async": "^0.9.0", "brfs": "^1.4.0", - "eventemitter3": "^0.1.6", + "eventemitter3": "^1.0.0", "object-assign": "^2.0.0", "pixi-spine": "^1.0.1", "resource-loader": "^1.4.0" diff --git a/package.json b/package.json index ce76d2f..2a6c8dd 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "async": "^0.9.0", "brfs": "^1.4.0", - "eventemitter3": "^0.1.6", + "eventemitter3": "^1.0.0", "object-assign": "^2.0.0", "pixi-spine": "^1.0.1", "resource-loader": "^1.4.0" diff --git a/src/ticker/Ticker.js b/src/ticker/Ticker.js index 151a894..69de61a 100644 --- a/src/ticker/Ticker.js +++ b/src/ticker/Ticker.js @@ -4,20 +4,6 @@ TICK = 'tick'; /** - * Yes, this is accessing an internal module:eventemitter3 api. - * Ugly, but calling module:eventemitter3.EventEmitter#listeners - * does a bit too much for what this is for. - * This is simple enough to keep track of and contribute - * back to the eventemitter3 project in the near future. - * - * @private - */ -function hasListeners(emitter) -{ - return !!(emitter._events && emitter._events[TICK]); -} - -/** * A Ticker class that runs an update loop that other objects listen to. * This class is composed around an EventEmitter object to add listeners * meant for execution on the next requested animation frame. @@ -46,14 +32,13 @@ if (_this.started) { + // Invoke listeners now _this.update(time); - } - // Check here because listeners could have side effects - // and may have modified state during frame execution. - // A new frame may have been requested or listeners removed. - if (_this.started && _this._requestId === null && hasListeners(_this._emitter)) - { - _this._requestId = requestAnimationFrame(_this._tick); + // Listener side effects may have modified ticker state. + if (_this.started && _this._requestId === null && _this._emitter.listeners(TICK, true)) + { + _this._requestId = requestAnimationFrame(_this._tick); + } } }; /** @@ -198,7 +183,7 @@ */ Ticker.prototype._requestIfNeeded = function _requestIfNeeded() { - if (this._requestId === null && hasListeners(this._emitter)) + if (this._requestId === null && this._emitter.listeners(TICK, true)) { // ensure callbacks get correct delta this.lastTime = performance.now(); @@ -247,6 +232,8 @@ * internal 'tick' event. It checks if the emitter has listeners, * and if so it requests a new animation frame at this point. * + * @param fn {Function} The listener function to be added for updates + * @param [context] {Function} The listener context * @returns {PIXI.ticker.Ticker} this */ Ticker.prototype.add = function add(fn, context) @@ -263,6 +250,8 @@ * internal 'tick' event. It checks if the emitter has listeners, * and if so it requests a new animation frame at this point. * + * @param fn {Function} The listener function to be added for one update + * @param [context] {Function} The listener context * @returns {PIXI.ticker.Ticker} this */ Ticker.prototype.addOnce = function addOnce(fn, context) @@ -279,13 +268,15 @@ * It checks if the emitter has listeners for 'tick' event. * If it does, then it cancels the animation frame. * + * @param [fn] {Function} The listener function to be removed + * @param [context] {Function} The listener context to be removed * @returns {PIXI.ticker.Ticker} this */ -Ticker.prototype.remove = function remove(fn, once) +Ticker.prototype.remove = function remove(fn, context) { - this._emitter.off(TICK, fn, once); + this._emitter.off(TICK, fn, context); - if (!hasListeners(this._emitter)) + if (!this._emitter.listeners(TICK, true)) { this._cancelIfNeeded(); } diff --git a/package.json b/package.json index ce76d2f..2a6c8dd 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "async": "^0.9.0", "brfs": "^1.4.0", - "eventemitter3": "^0.1.6", + "eventemitter3": "^1.0.0", "object-assign": "^2.0.0", "pixi-spine": "^1.0.1", "resource-loader": "^1.4.0" diff --git a/src/ticker/Ticker.js b/src/ticker/Ticker.js index 151a894..69de61a 100644 --- a/src/ticker/Ticker.js +++ b/src/ticker/Ticker.js @@ -4,20 +4,6 @@ TICK = 'tick'; /** - * Yes, this is accessing an internal module:eventemitter3 api. - * Ugly, but calling module:eventemitter3.EventEmitter#listeners - * does a bit too much for what this is for. - * This is simple enough to keep track of and contribute - * back to the eventemitter3 project in the near future. - * - * @private - */ -function hasListeners(emitter) -{ - return !!(emitter._events && emitter._events[TICK]); -} - -/** * A Ticker class that runs an update loop that other objects listen to. * This class is composed around an EventEmitter object to add listeners * meant for execution on the next requested animation frame. @@ -46,14 +32,13 @@ if (_this.started) { + // Invoke listeners now _this.update(time); - } - // Check here because listeners could have side effects - // and may have modified state during frame execution. - // A new frame may have been requested or listeners removed. - if (_this.started && _this._requestId === null && hasListeners(_this._emitter)) - { - _this._requestId = requestAnimationFrame(_this._tick); + // Listener side effects may have modified ticker state. + if (_this.started && _this._requestId === null && _this._emitter.listeners(TICK, true)) + { + _this._requestId = requestAnimationFrame(_this._tick); + } } }; /** @@ -198,7 +183,7 @@ */ Ticker.prototype._requestIfNeeded = function _requestIfNeeded() { - if (this._requestId === null && hasListeners(this._emitter)) + if (this._requestId === null && this._emitter.listeners(TICK, true)) { // ensure callbacks get correct delta this.lastTime = performance.now(); @@ -247,6 +232,8 @@ * internal 'tick' event. It checks if the emitter has listeners, * and if so it requests a new animation frame at this point. * + * @param fn {Function} The listener function to be added for updates + * @param [context] {Function} The listener context * @returns {PIXI.ticker.Ticker} this */ Ticker.prototype.add = function add(fn, context) @@ -263,6 +250,8 @@ * internal 'tick' event. It checks if the emitter has listeners, * and if so it requests a new animation frame at this point. * + * @param fn {Function} The listener function to be added for one update + * @param [context] {Function} The listener context * @returns {PIXI.ticker.Ticker} this */ Ticker.prototype.addOnce = function addOnce(fn, context) @@ -279,13 +268,15 @@ * It checks if the emitter has listeners for 'tick' event. * If it does, then it cancels the animation frame. * + * @param [fn] {Function} The listener function to be removed + * @param [context] {Function} The listener context to be removed * @returns {PIXI.ticker.Ticker} this */ -Ticker.prototype.remove = function remove(fn, once) +Ticker.prototype.remove = function remove(fn, context) { - this._emitter.off(TICK, fn, once); + this._emitter.off(TICK, fn, context); - if (!hasListeners(this._emitter)) + if (!this._emitter.listeners(TICK, true)) { this._cancelIfNeeded(); } diff --git a/src/ticker/index.js b/src/ticker/index.js index 188dab2..6547969 100644 --- a/src/ticker/index.js +++ b/src/ticker/index.js @@ -30,7 +30,7 @@ * var stage = new PIXI.Container(); * var interactionManager = PIXI.interaction.InteractionManager(renderer); * document.body.appendChild(renderer.view); - * ticker.add(function () { + * ticker.add(function (time) { * renderer.render(stage); * }); * @@ -38,12 +38,12 @@ * // Or you can just update it manually. * ticker.autoStart = false; * ticker.stop(); - * function animate() { + * function animate(time) { * ticker.update(time); * renderer.render(stage); * requestAnimationFrame(animate); * } - * animate(); + * animate(performance.now()); * * @type {PIXI.ticker.Ticker} * @memberof PIXI.ticker