Files
shaka-player/lib/util/switch_history.js
T
Joey Parrish 562a2d567b chore: Strictly require jsdoc
This enables the eslint rule requiring jsdocs on all class
declarations, function declarations, and methods.

Unfortunately, there are two problems with this:

1. We don't use class _declarations_, we use class _expressions_,
which are not covered by this rule.  So it does not enforce jsdoc at
the class level.
2. We tend to document a class at the class-level, rather than at the
constructor.  But a constructor counts as a method for eslint, so it
requires docs on the constructor.  There is no way to configure it to
make an exception for trivial constructors.

So for all trivial (no-argument) constructors, we add empty jsdocs:
  /** */
  constructor() {

This was quicker and easier than setting up some alternative plugin in
eslint to make an exception for us.

The good news is that this rule caught several undocumented parameters
and places where the jsdoc comment was malformed.  So fixing those
also improves the compiler's ability to enforce types.

Change-Id: Icbc46ed690c94e53d354648a883119524f8fca45
2021-01-09 02:00:31 +00:00

114 lines
2.5 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.SwitchHistory');
/**
* This class is used to track changes in variant and text selections. This
* class will make sure that redundant switches are not recorded in the history.
*
* @final
*/
shaka.util.SwitchHistory = class {
/** */
constructor() {
/** @private {?shaka.extern.Variant} */
this.currentVariant_ = null;
/** @private {?shaka.extern.Stream} */
this.currentText_ = null;
/** @private {!Array.<shaka.extern.TrackChoice>} */
this.history_ = [];
}
/**
* Update the history to show that we are currently playing |newVariant|. If
* we are already playing |newVariant|, this update will be ignored.
*
* @param {shaka.extern.Variant} newVariant
* @param {boolean} fromAdaptation
*/
updateCurrentVariant(newVariant, fromAdaptation) {
if (this.currentVariant_ == newVariant) {
return;
}
this.currentVariant_ = newVariant;
this.history_.push({
timestamp: this.getNowInSeconds_(),
id: newVariant.id,
type: 'variant',
fromAdaptation: fromAdaptation,
bandwidth: newVariant.bandwidth,
});
}
/**
* Update the history to show that we are currently playing |newText|. If we
* are already playing |newText|, this update will be ignored.
*
* @param {shaka.extern.Stream} newText
* @param {boolean} fromAdaptation
*/
updateCurrentText(newText, fromAdaptation) {
if (this.currentText_ == newText) {
return;
}
this.currentText_ = newText;
this.history_.push({
timestamp: this.getNowInSeconds_(),
id: newText.id,
type: 'text',
fromAdaptation: fromAdaptation,
bandwidth: null,
});
}
/**
* Get a copy of the switch history. This will make sure to expose no internal
* references.
*
* @return {!Array.<shaka.extern.TrackChoice>}
*/
getCopy() {
const copy = [];
for (const entry of this.history_) {
copy.push(this.clone_(entry));
}
return copy;
}
/**
* Get the system time in seconds.
*
* @return {number}
* @private
*/
getNowInSeconds_() {
return Date.now() / 1000;
}
/**
* @param {shaka.extern.TrackChoice} entry
* @return {shaka.extern.TrackChoice}
* @private
*/
clone_(entry) {
return {
timestamp: entry.timestamp,
id: entry.id,
type: entry.type,
fromAdaptation: entry.fromAdaptation,
bandwidth: entry.bandwidth,
};
}
};