mirror of
https://github.com/neocities/neocities.git
synced 2025-05-09 16:28:29 +02:00
37 lines
1.2 KiB
JavaScript
37 lines
1.2 KiB
JavaScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
export class Lazy {
|
|
constructor(executor) {
|
|
this.executor = executor;
|
|
this._didRun = false;
|
|
}
|
|
/**
|
|
* Get the wrapped value.
|
|
*
|
|
* This will force evaluation of the lazy value if it has not been resolved yet. Lazy values are only
|
|
* resolved once. `getValue` will re-throw exceptions that are hit while resolving the value
|
|
*/
|
|
get value() {
|
|
if (!this._didRun) {
|
|
try {
|
|
this._value = this.executor();
|
|
}
|
|
catch (err) {
|
|
this._error = err;
|
|
}
|
|
finally {
|
|
this._didRun = true;
|
|
}
|
|
}
|
|
if (this._error) {
|
|
throw this._error;
|
|
}
|
|
return this._value;
|
|
}
|
|
/**
|
|
* Get the wrapped value without forcing evaluation.
|
|
*/
|
|
get rawValue() { return this._value; }
|
|
}
|