@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
27 lines (26 loc) • 559 B
JavaScript
export class Lazy {
#val = null;
#fn = null;
constructor(val) {
if (typeof val === 'function') {
this.#fn = val;
}
else if (val) {
this.#val = val;
}
}
get resolved() {
return this.#val;
}
resolve(opts) {
if (this.#val)
return this.#val;
if (!this.#fn)
throw new Error('Lazy@resolve: No initializer provided');
this.#val = this.#fn(opts);
return this.#val;
}
clear() {
this.#val = null;
}
}