src/vs/base/common/observableInternal/utils/promise.ts

171 LOC · 133 covered · 38 uncovered · 17 ranges · 6771 concepts · 4 introducers · 3467 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- devToolsLogger.ts ×24
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 > import { DisposableStore } from '../../lifecycle.js';
6 > import { IObservable, ISettableObservable } from '../base.js';
7 > import { autorun } from '../reactions/autorun.js';
8 > import { transaction } from '../transaction.js';
9 > import { derived } from '../observables/derived.js';
10 > import { observableValue } from '../observables/observableValue.js';
11 >
12 > export class ObservableLazy<T> {
13 > private readonly _value = observableValue<T | undefined>(this, undefined);
14 >
15 > /**
16 > * The cached value.
17 > * Does not force a computation of the value.
18 > */
19 > public get cachedValue(): IObservable<T | undefined> { return this._value; }
20 >
21 > constructor(private readonly _computeValue: () => T) {
22 }
24 > /**
25 > * Returns the cached value.
26 > * Computes the value if the value has not been cached yet.
27 > */
28 > public getValue(): T {
29 let v = this._value.get();
30 if (!v) {
31 v = this._computeValue();
32 this._value.set(v, undefined);
33 }
34 return v;
35 }
37 >
38 > /**
39 > * A promise whose state is observable.
40 > */
41 > export class ObservablePromise<T> {
42 > public static fromFn<T>(fn: () => Promise<T>): ObservablePromise<T> {
43 > return new ObservablePromise(fn());
44 > }
45 >
46 > public static resolved<T>(value: T): ObservablePromise<T> {
47 > return new ObservablePromise(Promise.resolve(value)); mcpServer.ts ×36
48 > }
50 > private readonly _value = observableValue<PromiseResult<T> | undefined>(this, undefined);
51 >
52 > /**
53 > * The promise that this object wraps.
54 > */
55 > public readonly promise: Promise<T>;
56 >
57 > /**
58 > * The current state of the promise.
59 > * Is `undefined` if the promise didn't resolve yet.
60 > */
61 > public readonly promiseResult: IObservable<PromiseResult<T> | undefined> = this._value;
62 >
63 > constructor(promise: Promise<T>) {
64 > this.promise = promise.then(value => { promise.ts ×3
65 > transaction(tx => {
66 > /** @description onPromiseResolved */
67 > this._value.set(new PromiseResult(value, undefined), tx);
68 > });
69 > return value;
70 > }, error => {
71 > transaction(tx => { mcpServer.ts ×36
72 > /** @description onPromiseRejected */
73 > this._value.set(new PromiseResult<T>(undefined, error), tx);
74 > });
75 > throw error;
76 > }); promise.ts ×3
77 > }
79 > public readonly resolvedValue = derived(this, reader => {
80 > const result = this.promiseResult.read(reader); promise.ts ×1
81 > if (!result) {
82 > return undefined;
83 > }
84 > return result.getDataOrThrow();
86 > }
87 >
88 > export class PromiseResult<T> {
89 > constructor(
90 > /** promise.ts ×3
91 > * The value of the resolved promise.
92 > * Undefined if the promise rejected.
93 > */
94 > public readonly data: T | undefined,
95 >
96 > /**
97 > * The error in case of a rejected promise.
98 > * Undefined if the promise resolved.
99 > */
100 > public readonly error: unknown | undefined,
101 > ) {
102 > }
104 > /**
105 > * Returns the value if the promise resolved, otherwise throws the error.
106 > */
107 > public getDataOrThrow(): T {
108 if (this.error) {
109 throw this.error;
110 }
111 return this.data!;
112 }
114 >
115 > /**
116 > * Tracks a changing {@link ObservablePromise}, exposing the last resolved value
117 > * and whether a newer promise is still pending.
118 > */
119 > export class ObservableResolvedPromise<T> {
120 > private readonly _lastResolved: ISettableObservable<T>;
121 > public readonly lastResolved: IObservable<T>;
122 >
123 > private readonly _isResolving = observableValue<boolean>(this, false);
124 > public readonly isResolving: IObservable<boolean> = this._isResolving;
125 >
126 > private _runningPromise: ObservablePromise<T> | undefined;
127 >
128 > constructor(
129 source: IObservable<ObservablePromise<T>>,
130 initialValue: T,
131 store: DisposableStore,
132 ) {
133 this._lastResolved = observableValue<T>(this, initialValue);
134 this.lastResolved = this._lastResolved;
135
136 store.add(autorun(reader => {
137 const current = source.read(reader);
138 this._runningPromise = current;
139
140 const result = current.promiseResult.read(reader);
141 if (result) {
142 if (current === this._runningPromise) {
143 this._isResolving.set(false, undefined);
144 this._lastResolved.set(result.getDataOrThrow(), undefined);
145 }
146 } else {
147 this._isResolving.set(true, undefined);
148 }
149 }));
150 }
152 >
153 > /**
154 > * A lazy promise whose state is observable.
155 > */
156 > export class ObservableLazyPromise<T> {
157 > private readonly _lazyValue = new ObservableLazy(() => new ObservablePromise(this._computePromise()));
158 >
159 > /**
160 > * Does not enforce evaluation of the promise compute function.
161 > * Is undefined if the promise has not been computed yet.
162 > */
163 > public readonly cachedPromiseResult = derived(this, reader => this._lazyValue.cachedValue.read(reader)?.promiseResult.read(reader));
164 >
165 > constructor(private readonly _computePromise: () => Promise<T>) {
166 }
168 > public getPromise(): Promise<T> {
169 return this._lazyValue.getValue().promise;
170 }