Path: blob/main/src/vs/workbench/services/extensions/common/lazyPromise.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CancellationError, onUnexpectedError } from '../../../../base/common/errors.js';67export class LazyPromise implements Promise<any> {89private _actual: Promise<any> | null;10private _actualOk: ((value?: any) => any) | null;11private _actualErr: ((err?: any) => any) | null;1213private _hasValue: boolean;14private _value: any;1516protected _hasErr: boolean;17protected _err: any;1819constructor() {20this._actual = null;21this._actualOk = null;22this._actualErr = null;23this._hasValue = false;24this._value = null;25this._hasErr = false;26this._err = null;27}2829get [Symbol.toStringTag](): string {30return this.toString();31}3233private _ensureActual(): Promise<any> {34if (!this._actual) {35this._actual = new Promise<any>((c, e) => {36this._actualOk = c;37this._actualErr = e;3839if (this._hasValue) {40this._actualOk(this._value);41}4243if (this._hasErr) {44this._actualErr(this._err);45}46});47}48return this._actual;49}5051public resolveOk(value: any): void {52if (this._hasValue || this._hasErr) {53return;54}5556this._hasValue = true;57this._value = value;5859if (this._actual) {60this._actualOk!(value);61}62}6364public resolveErr(err: any): void {65if (this._hasValue || this._hasErr) {66return;67}6869this._hasErr = true;70this._err = err;7172if (this._actual) {73this._actualErr!(err);74} else {75// If nobody's listening at this point, it is safe to assume they never will,76// since resolving this promise is always "async"77onUnexpectedError(err);78}79}8081public then(success: any, error: any): any {82return this._ensureActual().then(success, error);83}8485public catch(error: any): any {86return this._ensureActual().then(undefined, error);87}8889public finally(callback: () => void): any {90return this._ensureActual().finally(callback);91}92}9394export class CanceledLazyPromise extends LazyPromise {95constructor() {96super();97this._hasErr = true;98this._err = new CancellationError();99}100}101102103