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

101 LOC · 60 covered · 41 uncovered · 15 ranges · 6771 concepts · 6 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 >
6 > import { IReader, IObservable } from '../base.js';
7 > import { DebugOwner, DebugNameData } from '../debugName.js';
8 > import { CancellationError, CancellationToken, CancellationTokenSource } from '../commonFacade/cancellation.js';
9 > import { strictEquals } from '../commonFacade/deps.js';
10 > import { autorun } from '../reactions/autorun.js';
11 > import { Derived } from '../observables/derivedImpl.js';
12 > import { DebugLocation } from '../debugLocation.js';
13 >
14 > /**
15 > * Resolves the promise when the observables state matches the predicate.
16 > */
17 > export function waitForState<T>(observable: IObservable<T | null | undefined>): Promise<T>;
18 > export function waitForState<T, TState extends T>(observable: IObservable<T>, predicate: (state: T) => state is TState, isError?: (state: T) => boolean | unknown | undefined, cancellationToken?: CancellationToken): Promise<TState>;
19 > export function waitForState<T>(observable: IObservable<T>, predicate: (state: T) => boolean, isError?: (state: T) => boolean | unknown | undefined, cancellationToken?: CancellationToken): Promise<T>;
20 > export function waitForState<T>(observable: IObservable<T>, predicate?: (state: T) => boolean, isError?: (state: T) => boolean | unknown | undefined, cancellationToken?: CancellationToken): Promise<T> {
21 > if (!predicate) { utilsCancellation.ts ×8
22 predicate = state => state !== null && state !== undefined;
23 }
24 > return new Promise((resolve, reject) => { utilsCancellation.ts ×8
25 > let isImmediateRun = true;
26 > let shouldDispose = false;
27 > const stateObs = observable.map(state => {
28 > /** @description waitForState.state */
29 > return {
30 > isFinished: predicate(state),
31 > error: isError ? isError(state) : false,
32 > state
33 > };
34 > });
35 > const d = autorun(reader => {
36 > /** @description waitForState */
37 > const { isFinished, error, state } = stateObs.read(reader);
38 > if (isFinished || error) {
39 > if (isImmediateRun) {
40 > // The variable `d` is not initialized yet utilsCancellation.ts ×2
41 > shouldDispose = true;
42 > } else { utilsCancellation.ts ×8
43 > d.dispose(); utilsCancellation.ts ×1
44 > }
45 > if (error) { utilsCancellation.ts ×8
46 > reject(error === true ? state : error); utilsCancellation.ts ×1
47 > } else { utilsCancellation.ts ×8
48 > resolve(state); utilsCancellation.ts ×1
49 > }
51 > });
52 > if (cancellationToken) {
53 const dc = cancellationToken.onCancellationRequested(() => {
54 d.dispose();
55 dc.dispose();
56 reject(new CancellationError());
57 });
58 if (cancellationToken.isCancellationRequested) {
59 d.dispose();
60 dc.dispose();
61 reject(new CancellationError());
62 return;
63 }
64 }
65 > isImmediateRun = false; utilsCancellation.ts ×8
66 > if (shouldDispose) {
67 > d.dispose(); utilsCancellation.ts ×2
68 > }
70 > }
72 > export function derivedWithCancellationToken<T>(computeFn: (reader: IReader, cancellationToken: CancellationToken) => T): IObservable<T>;
73 > export function derivedWithCancellationToken<T>(owner: object, computeFn: (reader: IReader, cancellationToken: CancellationToken) => T): IObservable<T>;
74 > export function derivedWithCancellationToken<T>(computeFnOrOwner: ((reader: IReader, cancellationToken: CancellationToken) => T) | object, computeFnOrUndefined?: ((reader: IReader, cancellationToken: CancellationToken) => T)): IObservable<T> {
75 let computeFn: (reader: IReader, store: CancellationToken) => T;
76 let owner: DebugOwner;
77 if (computeFnOrUndefined === undefined) {
78 // eslint-disable-next-line local/code-no-any-casts
79 computeFn = computeFnOrOwner as any;
80 owner = undefined;
81 } else {
82 owner = computeFnOrOwner;
83 // eslint-disable-next-line local/code-no-any-casts
84 computeFn = computeFnOrUndefined as any;
85 }
86
87 let cancellationTokenSource: CancellationTokenSource | undefined = undefined;
88 return new Derived(
89 new DebugNameData(owner, undefined, computeFn),
90 r => {
91 if (cancellationTokenSource) {
92 cancellationTokenSource.dispose(true);
93 }
94 cancellationTokenSource = new CancellationTokenSource();
95 return computeFn(r, cancellationTokenSource.token);
96 }, undefined,
97 () => cancellationTokenSource?.dispose(),
98 strictEquals,
99 DebugLocation.ofCaller()
100 );
101 }