mirror of
https://github.com/shaka-project/shaka-player.git
synced 2026-06-24 17:35:10 +03:00
9e180712f4
Updated the shaka.util.Timer class to ES6. In this upgrade, the implementation was slightly changed to make better use of arrow-functions. Documentation for the class was expanded on to better document how multiple calls to start would work. To ensure consistency, both "schedule*" methods were merged into a single "start" method. Issue #1157 Change-Id: Iae86cae4d9cb751f0985ef20c371c0023c40bd53
107 lines
2.8 KiB
JavaScript
107 lines
2.8 KiB
JavaScript
/**
|
|
* @license
|
|
* Copyright 2016 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
goog.provide('shaka.util.Timer');
|
|
|
|
|
|
/**
|
|
* A timer allows a single function to be executed at a later time or at
|
|
* regular intervals.
|
|
*
|
|
* @final
|
|
* @export
|
|
*/
|
|
shaka.util.Timer = class {
|
|
/**
|
|
* Create a new timer. A timer is committed to a single callback function.
|
|
* While there is no technical reason to do this, it is far easier to
|
|
* understand and use timers when they are connected to one functional idea.
|
|
*
|
|
* @param {function()} onTick
|
|
*/
|
|
constructor(onTick) {
|
|
/**
|
|
* Each time our timer "does work", we call that a "tick". The name comes
|
|
* from old analog clocks.
|
|
*
|
|
* @private {function()}
|
|
*/
|
|
this.onTick_ = onTick;
|
|
|
|
/**
|
|
* When we schedule a timeout, it will give us an id for it. We can use this
|
|
* id to cancel it anytime before it executes.
|
|
*
|
|
* @private {?number}
|
|
*/
|
|
this.pendingTickId_ = null;
|
|
}
|
|
|
|
/**
|
|
* Start the timer. The timer will tick in |delayInSeconds|. If
|
|
* |repeating| is |true|, the timer will keep ticking until |stop| is
|
|
* called.
|
|
*
|
|
* Calling |start| on a running timer will override any previous calls to
|
|
* |start|.
|
|
*
|
|
* @param {number} delayInSeconds
|
|
* @param {boolean} repeating
|
|
* @export
|
|
*/
|
|
start(delayInSeconds, repeating) {
|
|
/** @type {number} */
|
|
const delayInMs = delayInSeconds * 1000;
|
|
|
|
/**
|
|
* Wrap our |onTick_| callback with a function that will handle rescheduling
|
|
* the callback.
|
|
*
|
|
* @type {function()}
|
|
*/
|
|
const tick = () => {
|
|
this.onTick_();
|
|
|
|
this.pendingTickId_ = repeating ?
|
|
setTimeout(tick, delayInMs) :
|
|
null;
|
|
};
|
|
|
|
// Later calls to |start| override earlier calls to |start|, so cancel any
|
|
// pending ticks.
|
|
if (this.pendingTickId_ != null) {
|
|
clearTimeout(this.pendingTickId_);
|
|
}
|
|
|
|
this.pendingTickId_ = setTimeout(tick, delayInMs);
|
|
}
|
|
|
|
/**
|
|
* Stop the timer and cancel any pending ticks. The timer is still usable
|
|
* after calling |stop|.
|
|
*
|
|
* @export
|
|
*/
|
|
stop() {
|
|
if (this.pendingTickId_ != null) {
|
|
clearTimeout(this.pendingTickId_);
|
|
}
|
|
|
|
this.pendingTickId_ = null;
|
|
}
|
|
};
|