Files
shaka-player/lib/util/timer.js
T
Joey Parrish bf544fd4c9 Defer DRM events, redesign DRM errors
Wait to send a WRONG_KEYS error until key statuses have propagated.

Also, check for expired keys and fire the EXPIRED error from the key
status handler, not from 'waitingforkeys', which is not yet available
everywhere.  The EXPIRED error will suppress any WRONG_KEYS error that
might otherwise fire when 'waitingforkeys' happens.

Finally, do not trigger track restrictions based on expired keys.  This
means EXPIRED will never be followed by ALL_TRACKS_RESTRICTED.

This provides more deterministic behavior in tests and clearer
diagnostic signals for app developers.

Change-Id: Idd0f91fdd6442125661b9593ecaad0b57b81625c
2016-07-19 17:16:13 +00:00

79 lines
1.9 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 simple cancelable timer.
* @param {Function} callback
* @constructor
* @struct
*/
shaka.util.Timer = function(callback) {
/** @private {?number} */
this.id_ = null;
/** @private {number} */
this.timeoutSeconds_ = 0;
/** @private {Function} */
this.callback_ = (function() {
this.id_ = null;
callback();
}.bind(this));
};
/**
* Cancel the timer, if it's running.
*/
shaka.util.Timer.prototype.cancel = function() {
if (this.id_ != null) {
clearTimeout(this.id_);
this.id_ = null;
}
};
/**
* Schedule the timer, canceling any previous scheduling.
* @param {number} seconds
*/
shaka.util.Timer.prototype.schedule = function(seconds) {
this.cancel();
this.timeoutSeconds_ = seconds;
this.id_ = setTimeout(this.callback_, seconds * 1000);
};
/**
* If the timer is running, reschedule it using the previous scheduled timeout.
* @example
* If scheduled for 5 seconds, and rescheduled 3 seconds later,
* the timer will fire 8 seconds after the original scheduling.
* @example
* If scheduled for 5 seconds, and rescheduled 6 seconds later,
* the timer will already have fired and will not be rescheduled.
*/
shaka.util.Timer.prototype.rescheduleIfRunning = function() {
if (this.id_ != null) {
this.schedule(this.timeoutSeconds_);
}
};