diff --git a/helpers/LazyVal.ts b/helpers/LazyVal.ts new file mode 100644 index 0000000000000000000000000000000000000000..cd97f6887478913592407f5907262d9240eba564 --- /dev/null +++ b/helpers/LazyVal.ts @@ -0,0 +1,26 @@ +class LazyVal<T> { + private val: T = null; + + constructor(private valLoader: () => Promise<T>) {} + + get value(): Promise<T> { + return new Promise<T>(async (resolve) => { + if (this.val === null) { + this.val = await this.valLoader(); + } + + resolve(this.val); + }); + } + + reset() { + this.val = null; + } + + get isValueLoaded(): boolean { + return this.val !== null; + } +} + + +export default LazyVal;