src/vs/base/common/observableInternal/changeTracker.ts

98 LOC · 61 covered · 37 uncovered · 4 ranges · 6771 concepts · 2 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 { BugIndicatingError } from './commonFacade/deps.js';
7 > import { IObservableWithChange, IReader } from './base.js';
8 >
9 > export interface IChangeTracker<TChangeSummary> {
10 > createChangeSummary(previousChangeSummary: TChangeSummary | undefined): TChangeSummary;
11 > handleChange(ctx: IChangeContext, change: TChangeSummary): boolean;
12 > beforeUpdate?(reader: IReader, change: TChangeSummary): void;
13 > }
14 >
15 > export interface IChangeContext {
16 > readonly changedObservable: IObservableWithChange<any, any>;
17 > readonly change: unknown;
18 >
19 > /**
20 > * Returns if the given observable caused the change.
21 > */
22 > didChange<T, TChange>(observable: IObservableWithChange<T, TChange>): this is { change: TChange };
23 > }
24 >
25 > /**
26 > * Subscribes to and records changes and the last value of the given observables.
27 > * Don't use the key "changes", as it is reserved for the changes array!
28 > */
29 > export function recordChanges<TObs extends Record<any, IObservableWithChange<any, any>>>(obs: TObs):
30 > IChangeTracker<{ [TKey in keyof TObs]: ReturnType<TObs[TKey]['get']> } derivedImpl.ts ×3
31 > & { changes: readonly ({ [TKey in keyof TObs]: { key: TKey; change: TObs[TKey]['TChange'] } }[keyof TObs])[] }> {
32 > return {
33 > createChangeSummary: (_previousChangeSummary) => {
34 > // eslint-disable-next-line local/code-no-any-casts
35 > return {
36 > changes: [],
37 > } as any;
38 > },
39 > handleChange(ctx, changeSummary) {
40 > for (const key in obs) {
41 > if (ctx.didChange(obs[key])) {
42 > // eslint-disable-next-line local/code-no-any-casts
43 > (changeSummary.changes as any).push({ key, change: ctx.change });
44 > }
45 > }
46 > return true;
47 > },
48 > beforeUpdate(reader, changeSummary) {
49 > for (const key in obs) {
50 > if (key === 'changes') {
51 throw new BugIndicatingError('property name "changes" is reserved for change tracking');
52 }
53 > changeSummary[key] = obs[key].read(reader); derivedImpl.ts ×3
54 > }
55 > }
56 > };
57 > }
59 > /**
60 > * Subscribes to and records changes and the last value of the given observables.
61 > * Don't use the key "changes", as it is reserved for the changes array!
62 > */
63 > export function recordChangesLazy<TObs extends Record<any, IObservableWithChange<any, any>>>(getObs: () => TObs):
64 IChangeTracker<{ [TKey in keyof TObs]: ReturnType<TObs[TKey]['get']> }
65 & { changes: readonly ({ [TKey in keyof TObs]: { key: TKey; change: TObs[TKey]['TChange'] } }[keyof TObs])[] }> {
66 let obs: TObs | undefined = undefined;
67 return {
68 createChangeSummary: (_previousChangeSummary) => {
69 // eslint-disable-next-line local/code-no-any-casts
70 return {
71 changes: [],
72 } as any;
73 },
74 handleChange(ctx, changeSummary) {
75 if (!obs) {
76 obs = getObs();
77 }
78 for (const key in obs) {
79 if (ctx.didChange(obs[key])) {
80 // eslint-disable-next-line local/code-no-any-casts
81 (changeSummary.changes as any).push({ key, change: ctx.change });
82 }
83 }
84 return true;
85 },
86 beforeUpdate(reader, changeSummary) {
87 if (!obs) {
88 obs = getObs();
89 }
90 for (const key in obs) {
91 if (key === 'changes') {
92 throw new BugIndicatingError('property name "changes" is reserved for change tracking');
93 }
94 changeSummary[key] = obs[key].read(reader);
95 }
96 }
97 };
98 }