Debian Codegraph @

node/@xterm/xterm/src/vs/base/common/lazy.ts

raw
1 /*---------------------------------------------------------------------------------------------
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 export class Lazy<T> {
7
8 private _didRun: boolean = false;
9 private _value?: T;
10 private _error: Error | undefined;
11
12 constructor(
13 private readonly executor: () => T,
14 ) { }
15
16 /**
17 * True if the lazy value has been resolved.
18 */
19 get hasValue() { return this._didRun; }
20
21 /**
22 * Get the wrapped value.
23 *
24 * This will force evaluation of the lazy value if it has not been resolved yet. Lazy values are only
25 * resolved once. `getValue` will re-throw exceptions that are hit while resolving the value
26 */
27 get value(): T {
28 if (!this._didRun) {
29 try {
30 this._value = this.executor();
31 } catch (err) {
32 this._error = err;
33 } finally {
34 this._didRun = true;
35 }
36 }
37 if (this._error) {
38 throw this._error;
39 }
40 return this._value!;
41 }
42
43 /**
44 * Get the wrapped value without forcing evaluation.
45 */
46 get rawValue(): T | undefined { return this._value; }
47 }