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.
/*---------------------------------------------------------------------------------------------
devToolsLogger.ts ×24
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IObservable, IObservableWithChange, IObserver, IReaderWithStore } from '../base.js';
import { DebugNameData } from '../debugName.js';
import { assertFn, BugIndicatingError, DisposableStore, IDisposable, markAsDisposed, onBugIndicatingError, trackDisposable } from '../commonFacade/deps.js';
import { getLogger } from '../logging/logging.js';
import { IChangeTracker } from '../changeTracker.js';
import { DebugLocation } from '../debugLocation.js';
export const enum AutorunState {
/**
* A dependency could have changed.
* We need to explicitly ask them if at least one dependency changed.
*/
dependenciesMightHaveChanged = 1,
/**
* A dependency changed and we need to recompute.
*/
stale = 2,
upToDate = 3,
}
function autorunStateToString(state: AutorunState): string {
switch (state) {
case AutorunState.dependenciesMightHaveChanged: return 'dependenciesMightHaveChanged';
case AutorunState.stale: return 'stale';
case AutorunState.upToDate: return 'upToDate';
default: return '<unknown>';
}
}
export class AutorunObserver<TChangeSummary = any> implements IObserver, IReaderWithStore, IDisposable {
private _state = AutorunState.stale;
private _updateCount = 0;
private _disposed = false;
private _dependencies = new Set<IObservable<any>>();
private _dependenciesToBeRemoved = new Set<IObservable<any>>();
private _changeSummary: TChangeSummary | undefined;
private _isRunning = false;
private _iteration = 0;
public get debugName(): string {
return this._debugNameData.getDebugName(this) ?? '(anonymous)';
}
constructor(
public readonly _runFn: (reader: IReaderWithStore, changeSummary: TChangeSummary) => void,
private readonly _changeTracker: IChangeTracker<TChangeSummary> | undefined,
debugLocation: DebugLocation
) {
this._changeSummary = this._changeTracker?.createChangeSummary(undefined);
getLogger()?.handleAutorunCreated(this, debugLocation);
this._run();
trackDisposable(this);
}
public dispose(): void {
}
for (const o of this._dependencies) {
o.removeObserver(this); // Warning: external call!
}
this._dependencies.clear();
if (this._store !== undefined) {
}
}
getLogger()?.handleAutorunDisposed(this);
markAsDisposed(this);
}
private _run() {
this._dependenciesToBeRemoved = this._dependencies;
this._dependencies = emptySet;
this._state = AutorunState.upToDate;
try {
if (!this._disposed) {
getLogger()?.handleAutorunStarted(this);
const changeSummary = this._changeSummary!;
const delayedStore = this._delayedStore;
if (delayedStore !== undefined) {
}
this._isRunning = true;
if (this._changeTracker) {
this._changeSummary = this._changeTracker.createChangeSummary(changeSummary); // Warning: external call!
}
this._store = undefined;
}
this._runFn(this, changeSummary); // Warning: external call!
} catch (e) {
this._isRunning = false;
if (delayedStore !== undefined) {
}
}
} finally {
if (!this._disposed) {
getLogger()?.handleAutorunFinished(this);
}
// We don't want our observed observables to think that they are (not even temporarily) not being observed.
// Thus, we only unsubscribe from observables that are definitely not read anymore.
for (const o of this._dependenciesToBeRemoved) {
}
}
}
public toString(): string {
return `Autorun<${this.debugName}>`;
}
// IObserver implementation
public beginUpdate(_observable: IObservable<any>): void {
this._checkIterations();
this._state = AutorunState.dependenciesMightHaveChanged;
}
this._updateCount++;
}
public endUpdate(_observable: IObservable<any>): void {
if (this._updateCount === 1) {
this._iteration = 1;
do {
if (this._checkIterations()) {
return;
}
for (const d of this._dependencies) {
d.reportChanges(); // Warning: external call!
if (this._state as AutorunState === AutorunState.stale) {
break;
}
}
this._iteration++;
if (this._state !== AutorunState.upToDate) {
}
}
} finally {
this._updateCount--;
}
assertFn(() => this._updateCount >= 0);
}
public handlePossibleChange(observable: IObservable<any>): void {
if (this._state === AutorunState.upToDate && this._isDependency(observable)) {
autorunImpl.ts ×2
this._checkIterations();
this._state = AutorunState.dependenciesMightHaveChanged;
}
public handleChange<T, TChange>(observable: IObservableWithChange<T, TChange>, change: TChange): void {
getLogger()?.handleAutorunDependencyChanged(this, observable, change);
try {
// Warning: external call!
const shouldReact = this._changeTracker ? this._changeTracker.handleChange({
change,
// eslint-disable-next-line local/code-no-any-casts
didChange: (o): this is any => o === observable as any,
if (shouldReact) {
this._checkIterations();
this._state = AutorunState.stale;
}
} catch (e) {
onBugIndicatingError(e);
}
}
private _isDependency(observable: IObservableWithChange<any, any>): boolean {
return this._dependencies.has(observable) && !this._dependenciesToBeRemoved.has(observable);
autorunImpl.ts ×5
}
// IReader implementation
private _ensureNoRunning(): void {
if (!this._isRunning) { throw new BugIndicatingError('The reader object cannot be used outside its compute function!'); }
autorunImpl.ts ×15
}
public readObservable<T>(observable: IObservable<T>): T {
// In case the run action disposes the autorun
if (this._disposed) {
}
observable.addObserver(this); // warning: external call!
const value = observable.get(); // warning: external call!
this._dependencies.add(observable);
this._dependenciesToBeRemoved.delete(observable);
return value;
}
private _store: DisposableStore | undefined = undefined;
get store(): DisposableStore {
if (this._disposed) {
throw new BugIndicatingError('Cannot access store after dispose');
}
if (this._store === undefined) {
this._store = new DisposableStore();
}
return this._store;
}
private _delayedStore: DisposableStore | undefined = undefined;
get delayedStore(): DisposableStore {
if (this._disposed) {
throw new BugIndicatingError('Cannot access store after dispose');
}
if (this._delayedStore === undefined) {
this._delayedStore = new DisposableStore();
}
return this._delayedStore;
}
public debugGetState() {
return {
isRunning: this._isRunning,
updateCount: this._updateCount,
dependencies: this._dependencies,
state: this._state,
stateStr: autorunStateToString(this._state),
};
}
public debugRerun(): void {
if (!this._isRunning) {
this._run();
} else {
this._state = AutorunState.stale;
}
}
private _checkIterations(): boolean {
onBugIndicatingError(new BugIndicatingError(`Autorun '${this.debugName}' is stuck in an infinite update loop.`));
return true;
}
}