Files
shaka-player/lib/util/lazy.js
T
Joey Parrish 3d29160309 Remove extraneous exports
These exports are not needed by the application.  Some of them may
have been added to work around the extern generator's lack of support
for partially-exported classes.

Change-Id: Iaf142397f31bd927bf942499a79da595f77361d5
2020-04-27 16:58:14 +00:00

44 lines
970 B
JavaScript

/** @license
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Lazy');
goog.require('goog.asserts');
/**
* @summary
* This contains a single value that is lazily generated when it is first
* requested. This can store any value except "undefined".
*
* @template T
*/
shaka.util.Lazy = class {
/** @param {function():T} gen */
constructor(gen) {
/** @private {function():T} */
this.gen_ = gen;
/** @private {T|undefined} */
this.value_ = undefined;
}
/** @return {T} */
value() {
if (this.value_ == undefined) {
// Compiler complains about unknown fields without this cast.
this.value_ = /** @type {*} */ (this.gen_());
goog.asserts.assert(
this.value_ != undefined, 'Unable to create lazy value');
}
return this.value_;
}
/** Resets the value of the lazy function, so it has to be remade. */
reset() {
this.value_ = undefined;
}
};