Skip to content

Commit e0a57bb

Browse files
authored
fix: Make shaka.util.Lazy work with null (#8267)
Due to usage of `==` instead of `===` Lazy utility does not work correctly with null generators, despite of what is stated in documentation.
1 parent d2f3657 commit e0a57bb

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/util/lazy.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ shaka.util.Lazy = class {
2828

2929
/** @return {T} */
3030
value() {
31-
if (this.value_ == undefined) {
31+
if (this.value_ === undefined) {
3232
// Compiler complains about unknown fields without this cast.
3333
this.value_ = /** @type {*} */ (this.gen_());
3434
goog.asserts.assert(
35-
this.value_ != undefined, 'Unable to create lazy value');
35+
this.value_ !== undefined, 'Unable to create lazy value');
3636
}
3737
return this.value_;
3838
}

test/util/lazy_unit.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*! @license
2+
* Shaka Player
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
describe('Lazy', () => {
8+
it('returns the same object each time', () => {
9+
const generator = jasmine.createSpy('generator').and.returnValue({});
10+
const lazy = new shaka.util.Lazy(shaka.test.Util.spyFunc(generator));
11+
const value = lazy.value();
12+
const value2 = lazy.value();
13+
expect(value).toBe(value2);
14+
expect(generator).toHaveBeenCalledTimes(1);
15+
});
16+
17+
it('works correctly for primitive value', () => {
18+
const generator = jasmine.createSpy('generator').and.returnValue(7);
19+
const lazy = new shaka.util.Lazy(shaka.test.Util.spyFunc(generator));
20+
const value = lazy.value();
21+
const value2 = lazy.value();
22+
expect(value).toBe(value2);
23+
expect(generator).toHaveBeenCalledTimes(1);
24+
});
25+
26+
it('works correctly for null', () => {
27+
const generator = jasmine.createSpy('generator').and.returnValue(null);
28+
const lazy = new shaka.util.Lazy(shaka.test.Util.spyFunc(generator));
29+
const value = lazy.value();
30+
expect(value).toBe(null);
31+
expect(generator).toHaveBeenCalledTimes(1);
32+
});
33+
});

0 commit comments

Comments
 (0)