1
>
/*---------------------------------------------------------------------------------------------
lazy.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
enum LazyValueState {
7
>
Uninitialized,
8
>
Running,
9
>
Completed,
10
>
}
11
>
12
>
export class Lazy<T> {
13
>
14
>
private _state = LazyValueState.Uninitialized;
15
>
private _value?: T;
16
>
private _error: Error | undefined;
17
>
18
>
constructor(
19
private readonly executor: () => T,
20
) { }
22
>
/**
23
>
* True if the lazy value has been resolved.
24
>
*/
25
>
get hasValue(): boolean { return this._state === LazyValueState.Completed; }
26
>
27
>
/**
28
>
* Get the wrapped value.
29
>
*
30
>
* This will force evaluation of the lazy value if it has not been resolved yet. Lazy values are only
31
>
* resolved once. `getValue` will re-throw exceptions that are hit while resolving the value
32
>
*/
33
>
get value(): T {
34
if (this._state === LazyValueState.Uninitialized) {
35
this._state = LazyValueState.Running;