mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 08:13:42 +00:00
feat: simplify lazy.ts (#10951)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
|
const NoValue = Symbol("NoValue");
|
||||||
|
|
||||||
export class Lazy<T> {
|
export class Lazy<T> {
|
||||||
private _value: T | undefined = undefined;
|
private _value: T | typeof NoValue = NoValue;
|
||||||
private _isCreated = false;
|
|
||||||
|
|
||||||
constructor(private readonly factory: () => T) {}
|
constructor(private readonly factory: () => T) {}
|
||||||
|
|
||||||
@@ -10,11 +11,10 @@ export class Lazy<T> {
|
|||||||
* @returns The value produced by your factory.
|
* @returns The value produced by your factory.
|
||||||
*/
|
*/
|
||||||
get(): T {
|
get(): T {
|
||||||
if (!this._isCreated) {
|
if (this._value === NoValue) {
|
||||||
this._value = this.factory();
|
return (this._value = this.factory());
|
||||||
this._isCreated = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._value as T;
|
return this._value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user