src/vs/base/common/observableInternal/transaction.ts
113 LOC · 68 covered · 45 uncovered · 19 ranges · 6771 concepts · 5 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 { handleBugIndicatingErrorRecovery, IObservable, IObserver, ITransaction } from './base.js';
import { getFunctionName } from './debugName.js';
import { getLogger } from './logging/logging.js';
/**
* Starts a transaction in which many observables can be changed at once.
* {@link fn} should start with a JS Doc using `@description` to give the transaction a debug name.
* Reaction run on demand or when the transaction ends.
*/
export function transaction(fn: (tx: ITransaction) => void, getDebugName?: () => string): void {
try {
fn(tx);
} finally {
tx.finish();
}
}
export function globalTransaction(fn: (tx: ITransaction) => void) {
if (_globalTransaction) {
fn(_globalTransaction);
} else {
const tx = new TransactionImpl(fn, undefined);
_globalTransaction = tx;
try {
fn(tx);
} finally {
tx.finish(); // During finish, more actions might be added to the transaction.
// Which is why we only clear the global transaction after finish.
_globalTransaction = undefined;
}
}
}
export async function asyncTransaction(fn: (tx: ITransaction) => Promise<void>, getDebugName?: () => string): Promise<void> {
const tx = new TransactionImpl(fn, getDebugName);
try {
await fn(tx);
} finally {
tx.finish();
}
}
* Allows to chain transactions.
*/
export function subtransaction(tx: ITransaction | undefined, fn: (tx: ITransaction) => void, getDebugName?: () => string): void {
transaction(fn, getDebugName);
} else {
fn(tx);
}
private _updatingObservers: { observer: IObserver; observable: IObservable<any> }[] | null = [];
constructor(public readonly _fn: Function, private readonly _getDebugName?: () => string) {
}
public getDebugName(): string | undefined {
if (this._getDebugName) {
return this._getDebugName();
}
return getFunctionName(this._fn);
}
public updateObserver(observer: IObserver, observable: IObservable<any>): void {
// This happens when a transaction is used in a callback or async function.
// If an async transaction is used, make sure the promise awaits all users of the transaction (e.g. no race).
handleBugIndicatingErrorRecovery('Transaction already finished!');
// Error recovery
transaction(tx => {
tx.updateObserver(observer, observable);
});
return;
}
// When this gets called while finish is active, they will still get considered
this._updatingObservers.push({ observer, observable });
observer.beginUpdate(observable);
}
public finish(): void {
if (!updatingObservers) {
handleBugIndicatingErrorRecovery('transaction.finish() has already been called!');
return;
}
for (let i = 0; i < updatingObservers.length; i++) {
observer.endUpdate(observable);
}
this._updatingObservers = null;
getLogger()?.handleEndTransaction(this);
}
public debugGetUpdatingObservers() {
return this._updatingObservers;
}