src/vs/base/common/observableInternal/reactions/autorunImpl.ts

282 LOC · 244 covered · 38 uncovered · 65 ranges · 6771 concepts · 16 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 { IObservable, IObservableWithChange, IObserver, IReaderWithStore } from '../base.js';
7 > import { DebugNameData } from '../debugName.js';
8 > import { assertFn, BugIndicatingError, DisposableStore, IDisposable, markAsDisposed, onBugIndicatingError, trackDisposable } from '../commonFacade/deps.js';
9 > import { getLogger } from '../logging/logging.js';
10 > import { IChangeTracker } from '../changeTracker.js';
11 > import { DebugLocation } from '../debugLocation.js';
12 >
13 > export const enum AutorunState {
14 > /**
15 > * A dependency could have changed.
16 > * We need to explicitly ask them if at least one dependency changed.
17 > */
18 > dependenciesMightHaveChanged = 1,
19 >
20 > /**
21 > * A dependency changed and we need to recompute.
22 > */
23 > stale = 2,
24 > upToDate = 3,
25 > }
26 >
27 function autorunStateToString(state: AutorunState): string {
28 switch (state) {
29 case AutorunState.dependenciesMightHaveChanged: return 'dependenciesMightHaveChanged';
30 case AutorunState.stale: return 'stale';
31 case AutorunState.upToDate: return 'upToDate';
32 default: return '<unknown>';
33 }
34 }
36 > export class AutorunObserver<TChangeSummary = any> implements IObserver, IReaderWithStore, IDisposable {
37 > private _state = AutorunState.stale;
38 > private _updateCount = 0;
39 > private _disposed = false;
40 > private _dependencies = new Set<IObservable<any>>();
41 > private _dependenciesToBeRemoved = new Set<IObservable<any>>();
42 > private _changeSummary: TChangeSummary | undefined;
43 > private _isRunning = false;
44 > private _iteration = 0;
45 >
46 > public get debugName(): string {
47 > return this._debugNameData.getDebugName(this) ?? '(anonymous)';
48 > }
49 >
50 > constructor(
51 > public readonly _debugNameData: DebugNameData, autorunImpl.ts ×15
52 > public readonly _runFn: (reader: IReaderWithStore, changeSummary: TChangeSummary) => void,
53 > private readonly _changeTracker: IChangeTracker<TChangeSummary> | undefined,
54 > debugLocation: DebugLocation
55 > ) {
56 > this._changeSummary = this._changeTracker?.createChangeSummary(undefined);
57 > getLogger()?.handleAutorunCreated(this, debugLocation);
58 > this._run();
59 >
60 > trackDisposable(this);
61 > }
63 > public dispose(): void {
64 > if (this._disposed) { autorunImpl.ts ×15
65 > return; autorunImpl.ts ×1
66 > }
67 > this._disposed = true; autorunImpl.ts ×15
68 > for (const o of this._dependencies) {
69 > o.removeObserver(this); // Warning: external call!
70 > }
71 > this._dependencies.clear();
72 >
73 > if (this._store !== undefined) {
74 > this._store.dispose(); autorunImpl.ts ×3
75 > }
76 > if (this._delayedStore !== undefined) { autorunImpl.ts ×15
77 > this._delayedStore.dispose(); autorunImpl.ts ×3
78 > }
80 > getLogger()?.handleAutorunDisposed(this);
81 > markAsDisposed(this);
82 > }
84 > private _run() {
85 > const emptySet = this._dependenciesToBeRemoved; autorunImpl.ts ×15
86 > this._dependenciesToBeRemoved = this._dependencies;
87 > this._dependencies = emptySet;
88 >
89 > this._state = AutorunState.upToDate;
90 >
91 > try {
92 > if (!this._disposed) {
93 > getLogger()?.handleAutorunStarted(this);
94 > const changeSummary = this._changeSummary!;
95 > const delayedStore = this._delayedStore;
96 > if (delayedStore !== undefined) {
97 > this._delayedStore = undefined; autorunImpl.ts ×2
98 > }
99 > try { autorunImpl.ts ×15
100 > this._isRunning = true;
101 > if (this._changeTracker) {
102 > this._changeTracker.beforeUpdate?.(this, changeSummary); autorunImpl.ts ×2
103 > this._changeSummary = this._changeTracker.createChangeSummary(changeSummary); // Warning: external call!
104 > }
105 > if (this._store !== undefined) { autorunImpl.ts ×15
106 > this._store.dispose(); autorunImpl.ts ×1
107 > this._store = undefined;
108 > }
110 > this._runFn(this, changeSummary); // Warning: external call!
111 > } catch (e) {
112 > onBugIndicatingError(e); errors.ts ×1
113 > } finally { autorunImpl.ts ×15
114 > this._isRunning = false;
115 > if (delayedStore !== undefined) {
116 > delayedStore.dispose(); autorunImpl.ts ×2
117 > }
119 > }
120 > } finally {
121 > if (!this._disposed) {
122 > getLogger()?.handleAutorunFinished(this);
123 > }
124 > // We don't want our observed observables to think that they are (not even temporarily) not being observed.
125 > // Thus, we only unsubscribe from observables that are definitely not read anymore.
126 > for (const o of this._dependenciesToBeRemoved) {
127 > o.removeObserver(this); // Warning: external call! autorunImpl.ts ×1
128 > }
129 > this._dependenciesToBeRemoved.clear(); autorunImpl.ts ×15
130 > }
131 > }
133 > public toString(): string {
134 return `Autorun<${this.debugName}>`;
135 }
137 > // IObserver implementation
138 > public beginUpdate(_observable: IObservable<any>): void {
139 > if (this._state === AutorunState.upToDate) { autorunImpl.ts ×7
140 > this._checkIterations();
141 > this._state = AutorunState.dependenciesMightHaveChanged;
142 > }
143 > this._updateCount++;
144 > }
146 > public endUpdate(_observable: IObservable<any>): void {
147 > try { autorunImpl.ts ×7
148 > if (this._updateCount === 1) {
149 > this._iteration = 1;
150 > do {
151 > if (this._checkIterations()) {
152 return;
153 }
154 > if (this._state === AutorunState.dependenciesMightHaveChanged) { autorunImpl.ts ×7
155 > this._state = AutorunState.upToDate; autorunImpl.ts ×2
156 > for (const d of this._dependencies) {
157 > d.reportChanges(); // Warning: external call!
158 > if (this._state as AutorunState === AutorunState.stale) {
159 > // The other dependencies will refresh on demand autorunImpl.ts ×1
160 > break;
161 > }
163 > }
165 > this._iteration++;
166 > if (this._state !== AutorunState.upToDate) {
167 > this._run(); // Warning: indirect external call! autorunImpl.ts ×5
168 > }
169 > } while (this._state !== AutorunState.upToDate); autorunImpl.ts ×7
170 > }
171 > } finally {
172 > this._updateCount--;
173 > }
174 >
175 > assertFn(() => this._updateCount >= 0);
176 > }
178 > public handlePossibleChange(observable: IObservable<any>): void {
179 > if (this._state === AutorunState.upToDate && this._isDependency(observable)) { autorunImpl.ts ×2
180 this._checkIterations();
181 this._state = AutorunState.dependenciesMightHaveChanged;
182 }
185 > public handleChange<T, TChange>(observable: IObservableWithChange<T, TChange>, change: TChange): void {
186 > if (this._isDependency(observable)) { autorunImpl.ts ×5
187 > getLogger()?.handleAutorunDependencyChanged(this, observable, change);
188 > try {
189 > // Warning: external call!
190 > const shouldReact = this._changeTracker ? this._changeTracker.handleChange({
191 > changedObservable: observable, autorunImpl.ts ×2
192 > change,
193 > // eslint-disable-next-line local/code-no-any-casts
194 > didChange: (o): this is any => o === observable as any,
195 > }, this._changeSummary!) : true; autorunImpl.ts ×5
196 > if (shouldReact) {
197 > this._checkIterations();
198 > this._state = AutorunState.stale;
199 > }
200 > } catch (e) {
201 onBugIndicatingError(e);
202 }
204 > }
206 > private _isDependency(observable: IObservableWithChange<any, any>): boolean {
207 > return this._dependencies.has(observable) && !this._dependenciesToBeRemoved.has(observable); autorunImpl.ts ×5
208 > }
210 > // IReader implementation
211 >
212 > private _ensureNoRunning(): void {
213 > if (!this._isRunning) { throw new BugIndicatingError('The reader object cannot be used outside its compute function!'); } autorunImpl.ts ×15
214 > }
216 > public readObservable<T>(observable: IObservable<T>): T {
217 > this._ensureNoRunning(); autorunImpl.ts ×15
218 >
219 > // In case the run action disposes the autorun
220 > if (this._disposed) {
221 > return observable.get(); // warning: external call! autorunImpl.ts ×1
222 > }
224 > observable.addObserver(this); // warning: external call!
225 > const value = observable.get(); // warning: external call!
226 > this._dependencies.add(observable);
227 > this._dependenciesToBeRemoved.delete(observable);
228 > return value;
229 > }
231 > private _store: DisposableStore | undefined = undefined;
232 > get store(): DisposableStore {
233 > this._ensureNoRunning(); autorunImpl.ts ×3
234 > if (this._disposed) {
235 throw new BugIndicatingError('Cannot access store after dispose');
236 }
238 > if (this._store === undefined) {
239 > this._store = new DisposableStore();
240 > }
241 > return this._store;
242 > }
244 > private _delayedStore: DisposableStore | undefined = undefined;
245 > get delayedStore(): DisposableStore {
246 > this._ensureNoRunning(); autorunImpl.ts ×3
247 > if (this._disposed) {
248 throw new BugIndicatingError('Cannot access store after dispose');
249 }
251 > if (this._delayedStore === undefined) {
252 > this._delayedStore = new DisposableStore();
253 > }
254 > return this._delayedStore;
255 > }
257 > public debugGetState() {
258 return {
259 isRunning: this._isRunning,
260 updateCount: this._updateCount,
261 dependencies: this._dependencies,
262 state: this._state,
263 stateStr: autorunStateToString(this._state),
264 };
265 }
267 > public debugRerun(): void {
268 if (!this._isRunning) {
269 this._run();
270 } else {
271 this._state = AutorunState.stale;
272 }
273 }
275 > private _checkIterations(): boolean {
276 > if (this._iteration > 100) { autorunImpl.ts ×7
277 onBugIndicatingError(new BugIndicatingError(`Autorun '${this.debugName}' is stuck in an infinite update loop.`));
278 return true;
279 }
280 > return false; autorunImpl.ts ×7
281 > }