Path: blob/main/extensions/copilot/src/util/vs/base/common/lazy.ts
13405 views
//!!! DO NOT modify, this file was COPIED from 'microsoft/vscode'12/*---------------------------------------------------------------------------------------------3* Copyright (c) Microsoft Corporation. All rights reserved.4* Licensed under the MIT License. See License.txt in the project root for license information.5*--------------------------------------------------------------------------------------------*/67enum LazyValueState {8Uninitialized,9Running,10Completed,11}1213export class Lazy<T> {1415private _state = LazyValueState.Uninitialized;16private _value?: T;17private _error: Error | undefined;1819constructor(20private readonly executor: () => T,21) { }2223/**24* True if the lazy value has been resolved.25*/26get hasValue(): boolean { return this._state === LazyValueState.Completed; }2728/**29* Get the wrapped value.30*31* This will force evaluation of the lazy value if it has not been resolved yet. Lazy values are only32* resolved once. `getValue` will re-throw exceptions that are hit while resolving the value33*/34get value(): T {35if (this._state === LazyValueState.Uninitialized) {36this._state = LazyValueState.Running;37try {38this._value = this.executor();39} catch (err) {40this._error = err;41} finally {42this._state = LazyValueState.Completed;43}44} else if (this._state === LazyValueState.Running) {45throw new Error('Cannot read the value of a lazy that is being initialized');46}4748if (this._error) {49throw this._error;50}51return this._value!;52}5354/**55* Get the wrapped value without forcing evaluation.56*/57get rawValue(): T | undefined { return this._value; }58}596061