forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlazy.js
44 lines (37 loc) · 986 Bytes
/
lazy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*! @license
* Shaka Player
* 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;
}
};