Skip to content

Commit

Permalink
Cleanup debug model.
Browse files Browse the repository at this point in the history
  • Loading branch information
hediet committed Jul 25, 2024
1 parent 770b9b1 commit b85c4fc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/common/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function groupBy<K extends string | number | symbol, V>(data: V[], groupF
return result;
}

export function diffSets<T>(before: Set<T>, after: Set<T>): { removed: T[]; added: T[] } {
export function diffSets<T>(before: ReadonlySet<T>, after: ReadonlySet<T>): { removed: T[]; added: T[] } {
const removed: T[] = [];
const added: T[] = [];
for (const element of before) {
Expand Down
28 changes: 28 additions & 0 deletions src/vs/base/common/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*--------------------------------------------------------------------------------------------*/

import { CancellationToken } from 'vs/base/common/cancellation';
import { diffSets } from 'vs/base/common/collections';
import { onUnexpectedError } from 'vs/base/common/errors';
import { createSingleCallFunction } from 'vs/base/common/functional';
import { combinedDisposable, Disposable, DisposableMap, DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
Expand Down Expand Up @@ -1779,3 +1780,30 @@ class ConstValueWithChangeEvent<T> implements IValueWithChangeEvent<T> {

constructor(readonly value: T) { }
}

/**
* @param handleItem Is called for each item in the set (but only the first time the item is seen in the set).
* The returned disposable is disposed if the item is no longer in the set.
*/
export function trackSetChanges<T>(getData: () => ReadonlySet<T>, onDidChangeData: Event<unknown>, handleItem: (d: T) => IDisposable): IDisposable {
const map = new DisposableMap<T, IDisposable>();
let oldData = new Set(getData());
for (const d of oldData) {
map.set(d, handleItem(d));
}

const store = new DisposableStore();
store.add(onDidChangeData(() => {
const newData = getData();
const diff = diffSets(oldData, newData);
for (const r of diff.removed) {
map.deleteAndDispose(r);
}
for (const a of diff.added) {
map.set(a, handleItem(a));
}
oldData = new Set(newData);
}));
store.add(map);
return store;
}
21 changes: 8 additions & 13 deletions src/vs/workbench/contrib/debug/common/debugModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { findLastIdx } from 'vs/base/common/arraysFind';
import { DeferredPromise, RunOnceScheduler } from 'vs/base/common/async';
import { VSBuffer, decodeBase64, encodeBase64 } from 'vs/base/common/buffer';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { Emitter, Event } from 'vs/base/common/event';
import { Emitter, Event, trackSetChanges } from 'vs/base/common/event';
import { stringHash } from 'vs/base/common/hash';
import { Disposable, DisposableMap, IDisposable } from 'vs/base/common/lifecycle';
import { Disposable } from 'vs/base/common/lifecycle';
import { mixin } from 'vs/base/common/objects';
import { autorun } from 'vs/base/common/observable';
import * as resources from 'vs/base/common/resources';
Expand Down Expand Up @@ -1416,7 +1416,6 @@ export class DebugModel extends Disposable implements IDebugModel {
private exceptionBreakpoints!: ExceptionBreakpoint[];
private dataBreakpoints!: DataBreakpoint[];
private watchExpressions!: Expression[];
private watchExpressionChangeListeners: DisposableMap<string, IDisposable> = this._register(new DisposableMap());
private instructionBreakpoints: InstructionBreakpoint[];

constructor(
Expand All @@ -1440,12 +1439,14 @@ export class DebugModel extends Disposable implements IDebugModel {
this._onDidChangeWatchExpressions.fire(undefined);
}));

this._register(trackSetChanges(
() => new Set(this.watchExpressions),
this.onDidChangeWatchExpressions,
(we) => we.onDidChangeValue((e) => this._onDidChangeWatchExpressionValue.fire(e)))
);

this.instructionBreakpoints = [];
this.sessions = [];

for (const we of this.watchExpressions) {
this.watchExpressionChangeListeners.set(we.getId(), we.onDidChangeValue((e) => this._onDidChangeWatchExpressionValue.fire(e)));
}
}

getId(): string {
Expand Down Expand Up @@ -2019,7 +2020,6 @@ export class DebugModel extends Disposable implements IDebugModel {

addWatchExpression(name?: string): IExpression {
const we = new Expression(name || '');
this.watchExpressionChangeListeners.set(we.getId(), we.onDidChangeValue((e) => this._onDidChangeWatchExpressionValue.fire(e)));
this.watchExpressions.push(we);
this._onDidChangeWatchExpressions.fire(we);

Expand All @@ -2037,11 +2037,6 @@ export class DebugModel extends Disposable implements IDebugModel {
removeWatchExpressions(id: string | null = null): void {
this.watchExpressions = id ? this.watchExpressions.filter(we => we.getId() !== id) : [];
this._onDidChangeWatchExpressions.fire(undefined);
if (!id) {
this.watchExpressionChangeListeners.clearAndDisposeAll();
return;
}
this.watchExpressionChangeListeners.deleteAndDispose(id);
}

moveWatchExpression(id: string, position: number): void {
Expand Down

0 comments on commit b85c4fc

Please sign in to comment.