1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00
Files
browser/libs/common/src/platform/misc/lazy.ts
2024-09-13 09:09:45 +02:00

21 lines
451 B
TypeScript

const NoValue = Symbol("NoValue");
export class Lazy<T> {
private _value: T | typeof NoValue = NoValue;
constructor(private readonly factory: () => T) {}
/**
* Resolves the factory and returns the result. Guaranteed to resolve the value only once.
*
* @returns The value produced by your factory.
*/
get(): T {
if (this._value === NoValue) {
return (this._value = this.factory());
}
return this._value;
}
}