Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/services/extensions/common/lazyPromise.ts
3296 views
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
import { CancellationError, onUnexpectedError } from '../../../../base/common/errors.js';
7
8
export class LazyPromise implements Promise<any> {
9
10
private _actual: Promise<any> | null;
11
private _actualOk: ((value?: any) => any) | null;
12
private _actualErr: ((err?: any) => any) | null;
13
14
private _hasValue: boolean;
15
private _value: any;
16
17
protected _hasErr: boolean;
18
protected _err: any;
19
20
constructor() {
21
this._actual = null;
22
this._actualOk = null;
23
this._actualErr = null;
24
this._hasValue = false;
25
this._value = null;
26
this._hasErr = false;
27
this._err = null;
28
}
29
30
get [Symbol.toStringTag](): string {
31
return this.toString();
32
}
33
34
private _ensureActual(): Promise<any> {
35
if (!this._actual) {
36
this._actual = new Promise<any>((c, e) => {
37
this._actualOk = c;
38
this._actualErr = e;
39
40
if (this._hasValue) {
41
this._actualOk(this._value);
42
}
43
44
if (this._hasErr) {
45
this._actualErr(this._err);
46
}
47
});
48
}
49
return this._actual;
50
}
51
52
public resolveOk(value: any): void {
53
if (this._hasValue || this._hasErr) {
54
return;
55
}
56
57
this._hasValue = true;
58
this._value = value;
59
60
if (this._actual) {
61
this._actualOk!(value);
62
}
63
}
64
65
public resolveErr(err: any): void {
66
if (this._hasValue || this._hasErr) {
67
return;
68
}
69
70
this._hasErr = true;
71
this._err = err;
72
73
if (this._actual) {
74
this._actualErr!(err);
75
} else {
76
// If nobody's listening at this point, it is safe to assume they never will,
77
// since resolving this promise is always "async"
78
onUnexpectedError(err);
79
}
80
}
81
82
public then(success: any, error: any): any {
83
return this._ensureActual().then(success, error);
84
}
85
86
public catch(error: any): any {
87
return this._ensureActual().then(undefined, error);
88
}
89
90
public finally(callback: () => void): any {
91
return this._ensureActual().finally(callback);
92
}
93
}
94
95
export class CanceledLazyPromise extends LazyPromise {
96
constructor() {
97
super();
98
this._hasErr = true;
99
this._err = new CancellationError();
100
}
101
}
102
103