diff --git a/src/observable.js b/src/observable.js index f0d416ff0e8f..0d5df667843a 100644 --- a/src/observable.js +++ b/src/observable.js @@ -51,11 +51,9 @@ export class Observable { * @param {function(TYPE)} handler Observer's instance. */ remove(handler) { - for (let i = 0; i < this.handlers_.length; i++) { - if (handler == this.handlers_[i]) { - this.handlers_.splice(i, 1); - break; - } + const index = this.handlers_.indexOf(handler); + if (index > -1) { + this.handlers_.splice(index, 1); } } @@ -64,9 +62,11 @@ export class Observable { * @param {TYPE} event */ fire(event) { - this.handlers_.forEach(handler => { + const handlers = this.handlers_; + for (let i = 0; i < handlers.length; i++) { + const handler = handlers[i]; handler(event); - }); + } } /** diff --git a/src/pass.js b/src/pass.js index fcb5b263cb8d..9901fca664cf 100644 --- a/src/pass.js +++ b/src/pass.js @@ -45,6 +45,9 @@ export class Pass { /** @private {boolean} */ this.running_ = false; + + /** @private @const */ + this.boundPass_ = () => this.pass_(); } /** @@ -75,31 +78,34 @@ export class Pass { // execution. delay = 10; } + const nextTime = timer.now() + delay; - // Schedule anew if nothing is scheduled currently of if the new time is + // Schedule anew if nothing is scheduled currently or if the new time is // sooner then previously requested. - if (this.scheduled_ == -1 || nextTime - this.nextTime_ < -10) { - if (this.scheduled_ != -1) { - timer.cancel(this.scheduled_); - } + if (!this.isPending() || nextTime - this.nextTime_ < -10) { + this.cancel(); this.nextTime_ = nextTime; - this.scheduled_ = timer.delay(() => { - this.scheduled_ = -1; - this.nextTime_ = 0; - this.running_ = true; - this.handler_(); - this.running_ = false; - }, delay); + this.scheduled_ = timer.delay(this.boundPass_, delay); + return true; } + return false; } + pass_() { + this.scheduled_ = -1; + this.nextTime_ = 0; + this.running_ = true; + this.handler_(); + this.running_ = false; + } + /** * Cancels the pending pass if any. */ cancel() { - if (this.scheduled_ != -1) { + if (this.isPending()) { timer.cancel(this.scheduled_); this.scheduled_ = -1; }