Skip to content

Commit 89a9017

Browse files
authored
Uses comments to set observable names to reduce code size (microsoft#189349)
1 parent 728b9a0 commit 89a9017

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+495
-323
lines changed

src/vs/base/common/observable.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,43 @@ export {
1010
ISettable,
1111
ISettableObservable,
1212
ITransaction,
13+
IChangeContext,
14+
IChangeTracker,
1315
observableValue,
16+
disposableObservableValue,
1417
transaction,
15-
} from 'vs/base/common/observableImpl/base';
16-
export { derived } from 'vs/base/common/observableImpl/derived';
18+
subtransaction,
19+
} from 'vs/base/common/observableInternal/base';
20+
export {
21+
derived,
22+
derivedOpts,
23+
derivedHandleChanges,
24+
derivedWithStore,
25+
} from 'vs/base/common/observableInternal/derived';
1726
export {
1827
autorun,
1928
autorunDelta,
2029
autorunHandleChanges,
2130
autorunWithStore,
22-
} from 'vs/base/common/observableImpl/autorun';
23-
export * from 'vs/base/common/observableImpl/utils';
31+
autorunOpts,
32+
autorunWithStoreHandleChanges,
33+
} from 'vs/base/common/observableInternal/autorun';
34+
export {
35+
IObservableSignal,
36+
constObservable,
37+
debouncedObservable,
38+
derivedObservableWithCache,
39+
derivedObservableWithWritableCache,
40+
keepAlive,
41+
observableFromEvent,
42+
observableFromPromise,
43+
observableSignal,
44+
observableSignalFromEvent,
45+
waitForState,
46+
wasEventTriggeredRecently,
47+
} from 'vs/base/common/observableInternal/utils';
2448

25-
import { ConsoleObservableLogger, setLogger } from 'vs/base/common/observableImpl/logging';
49+
import { ConsoleObservableLogger, setLogger } from 'vs/base/common/observableInternal/logging';
2650

2751
const enableLogging = false;
2852
if (enableLogging) {

src/vs/base/common/observableImpl/autorun.ts renamed to src/vs/base/common/observableInternal/autorun.ts

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,40 @@
55

66
import { assertFn } from 'vs/base/common/assert';
77
import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
8-
import { IReader, IObservable, IObserver, IChangeContext } from 'vs/base/common/observableImpl/base';
9-
import { getLogger } from 'vs/base/common/observableImpl/logging';
8+
import { IReader, IObservable, IObserver, IChangeContext, getFunctionName } from 'vs/base/common/observableInternal/base';
9+
import { getLogger } from 'vs/base/common/observableInternal/logging';
1010

11-
export function autorun(debugName: string, fn: (reader: IReader) => void): IDisposable {
12-
return new AutorunObserver(debugName, fn, undefined, undefined);
11+
export function autorunOpts(options: { debugName: string | (() => string | undefined) }, fn: (reader: IReader) => void): IDisposable {
12+
return new AutorunObserver(options.debugName, fn, undefined, undefined);
13+
}
14+
15+
export function autorun(fn: (reader: IReader) => void): IDisposable {
16+
return new AutorunObserver(undefined, fn, undefined, undefined);
1317
}
1418

1519
export function autorunHandleChanges<TChangeSummary>(
16-
debugName: string,
1720
options: {
21+
debugName?: string | (() => string | undefined);
1822
createEmptyChangeSummary?: () => TChangeSummary;
1923
handleChange: (context: IChangeContext, changeSummary: TChangeSummary) => boolean;
2024
},
2125
fn: (reader: IReader, changeSummary: TChangeSummary) => void
2226
): IDisposable {
23-
return new AutorunObserver(debugName, fn, options.createEmptyChangeSummary, options.handleChange);
24-
}
25-
26-
// TODO@hediet rename to autorunWithStore
27-
export function autorunWithStore2(
28-
debugName: string,
29-
fn: (reader: IReader, store: DisposableStore) => void,
30-
): IDisposable {
31-
return autorunWithStore(fn, debugName);
27+
return new AutorunObserver(options.debugName, fn, options.createEmptyChangeSummary, options.handleChange);
3228
}
3329

3430
export function autorunWithStoreHandleChanges<TChangeSummary>(
35-
debugName: string,
3631
options: {
32+
debugName?: string | (() => string | undefined);
3733
createEmptyChangeSummary?: () => TChangeSummary;
3834
handleChange: (context: IChangeContext, changeSummary: TChangeSummary) => boolean;
3935
},
4036
fn: (reader: IReader, changeSummary: TChangeSummary, store: DisposableStore) => void
4137
): IDisposable {
4238
const store = new DisposableStore();
4339
const disposable = autorunHandleChanges(
44-
debugName,
4540
{
41+
debugName: options.debugName ?? (() => getFunctionName(fn)),
4642
createEmptyChangeSummary: options.createEmptyChangeSummary,
4743
handleChange: options.handleChange,
4844
},
@@ -57,14 +53,12 @@ export function autorunWithStoreHandleChanges<TChangeSummary>(
5753
});
5854
}
5955

60-
// TODO@hediet deprecate, rename to autorunWithStoreEx
61-
export function autorunWithStore(
62-
fn: (reader: IReader, store: DisposableStore) => void,
63-
debugName: string
64-
): IDisposable {
56+
export function autorunWithStore(fn: (reader: IReader, store: DisposableStore) => void): IDisposable {
6557
const store = new DisposableStore();
66-
const disposable = autorun(
67-
debugName,
58+
const disposable = autorunOpts(
59+
{
60+
debugName: () => getFunctionName(fn) || '(anonymous)',
61+
},
6862
reader => {
6963
store.clear();
7064
fn(reader, store);
@@ -98,8 +92,22 @@ export class AutorunObserver<TChangeSummary = any> implements IObserver, IReader
9892
private dependenciesToBeRemoved = new Set<IObservable<any>>();
9993
private changeSummary: TChangeSummary | undefined;
10094

95+
public get debugName(): string {
96+
if (typeof this._debugName === 'string') {
97+
return this._debugName;
98+
}
99+
if (typeof this._debugName === 'function') {
100+
const name = this._debugName();
101+
if (name !== undefined) { return name; }
102+
}
103+
const name = getFunctionName(this.runFn);
104+
if (name !== undefined) { return name; }
105+
106+
return '(anonymous)';
107+
}
108+
101109
constructor(
102-
public readonly debugName: string,
110+
private readonly _debugName: string | (() => string | undefined) | undefined,
103111
private readonly runFn: (reader: IReader, changeSummary: TChangeSummary) => void,
104112
private readonly createChangeSummary: (() => TChangeSummary) | undefined,
105113
private readonly _handleChange: ((context: IChangeContext, summary: TChangeSummary) => boolean) | undefined,
@@ -216,13 +224,13 @@ export class AutorunObserver<TChangeSummary = any> implements IObserver, IReader
216224
export namespace autorun {
217225
export const Observer = AutorunObserver;
218226
}
227+
219228
export function autorunDelta<T>(
220-
name: string,
221229
observable: IObservable<T>,
222230
handler: (args: { lastValue: T | undefined; newValue: T }) => void
223231
): IDisposable {
224232
let _lastValue: T | undefined;
225-
return autorun(name, (reader) => {
233+
return autorunOpts({ debugName: () => getFunctionName(handler) }, (reader) => {
226234
const newValue = observable.read(reader);
227235
const lastValue = _lastValue;
228236
_lastValue = newValue;

src/vs/base/common/observableImpl/base.ts renamed to src/vs/base/common/observableInternal/base.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import { IDisposable } from 'vs/base/common/lifecycle';
7-
import type { derived } from 'vs/base/common/observableImpl/derived';
8-
import { getLogger } from 'vs/base/common/observableImpl/logging';
7+
import type { derived } from 'vs/base/common/observableInternal/derived';
8+
import { getLogger } from 'vs/base/common/observableInternal/logging';
99

1010
/**
1111
* Represents an observable value.
@@ -162,11 +162,11 @@ export abstract class ConvenientObservable<T, TChange> implements IObservable<T,
162162
/** @sealed */
163163
public map<TNew>(fn: (value: T, reader: IReader) => TNew): IObservable<TNew> {
164164
return _derived(
165+
(reader) => fn(this.read(reader), reader),
165166
() => {
166167
const name = getFunctionName(fn);
167168
return name !== undefined ? name : `${this.debugName} (mapped)`;
168169
},
169-
(reader) => fn(this.read(reader), reader)
170170
);
171171
}
172172

src/vs/base/common/observableImpl/derived.ts renamed to src/vs/base/common/observableInternal/derived.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55

66
import { BugIndicatingError } from 'vs/base/common/errors';
77
import { DisposableStore } from 'vs/base/common/lifecycle';
8-
import { IReader, IObservable, BaseObservable, IObserver, _setDerived, IChangeContext } from 'vs/base/common/observableImpl/base';
9-
import { getLogger } from 'vs/base/common/observableImpl/logging';
8+
import { IReader, IObservable, BaseObservable, IObserver, _setDerived, IChangeContext, getFunctionName } from 'vs/base/common/observableInternal/base';
9+
import { getLogger } from 'vs/base/common/observableInternal/logging';
1010

11-
export function derived<T>(debugName: string | (() => string), computeFn: (reader: IReader) => T): IObservable<T> {
11+
export function derived<T>(computeFn: (reader: IReader) => T, debugName?: string | (() => string)): IObservable<T> {
1212
return new Derived(debugName, computeFn, undefined, undefined, undefined);
1313
}
1414

15+
export function derivedOpts<T>(options: { debugName?: string | (() => string) }, computeFn: (reader: IReader) => T): IObservable<T> {
16+
return new Derived(options.debugName, computeFn, undefined, undefined, undefined);
17+
}
18+
1519
export function derivedHandleChanges<T, TChangeSummary>(
1620
debugName: string | (() => string),
1721
options: {
@@ -63,11 +67,14 @@ export class Derived<T, TChangeSummary = any> extends BaseObservable<T, void> im
6367
private changeSummary: TChangeSummary | undefined = undefined;
6468

6569
public override get debugName(): string {
70+
if (!this._debugName) {
71+
return getFunctionName(this.computeFn) || '(anonymous)';
72+
}
6673
return typeof this._debugName === 'function' ? this._debugName() : this._debugName;
6774
}
6875

6976
constructor(
70-
private readonly _debugName: string | (() => string),
77+
private readonly _debugName: string | (() => string) | undefined,
7178
private readonly computeFn: (reader: IReader, changeSummary: TChangeSummary) => T,
7279
private readonly createChangeSummary: (() => TChangeSummary) | undefined,
7380
private readonly _handleChange: ((context: IChangeContext, summary: TChangeSummary) => boolean) | undefined,

src/vs/base/common/observableImpl/logging.ts renamed to src/vs/base/common/observableInternal/logging.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { AutorunObserver } from 'vs/base/common/observableImpl/autorun';
7-
import { IObservable, ObservableValue, TransactionImpl } from 'vs/base/common/observableImpl/base';
8-
import { Derived } from 'vs/base/common/observableImpl/derived';
9-
import { FromEventObservable } from 'vs/base/common/observableImpl/utils';
6+
import { AutorunObserver } from 'vs/base/common/observableInternal/autorun';
7+
import { IObservable, ObservableValue, TransactionImpl } from 'vs/base/common/observableInternal/base';
8+
import { Derived } from 'vs/base/common/observableInternal/derived';
9+
import { FromEventObservable } from 'vs/base/common/observableInternal/utils';
1010

1111
let globalObservableLogger: IObservableLogger | undefined;
1212

src/vs/base/common/observableImpl/utils.ts renamed to src/vs/base/common/observableInternal/utils.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55

66
import { Event } from 'vs/base/common/event';
77
import { DisposableStore, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
8-
import { autorun } from 'vs/base/common/observableImpl/autorun';
9-
import { BaseObservable, ConvenientObservable, IObservable, IObserver, IReader, ITransaction, getFunctionName, observableValue, transaction } from 'vs/base/common/observableImpl/base';
10-
import { derived } from 'vs/base/common/observableImpl/derived';
11-
import { getLogger } from 'vs/base/common/observableImpl/logging';
8+
import { autorun } from 'vs/base/common/observableInternal/autorun';
9+
import { BaseObservable, ConvenientObservable, IObservable, IObserver, IReader, ITransaction, getFunctionName, observableValue, transaction } from 'vs/base/common/observableInternal/base';
10+
import { derived } from 'vs/base/common/observableInternal/derived';
11+
import { getLogger } from 'vs/base/common/observableInternal/logging';
1212

13+
/**
14+
* Represents an efficient observable whose value never changes.
15+
*/
1316
export function constObservable<T>(value: T): IObservable<T> {
1417
return new ConstObservable(value);
1518
}
@@ -53,7 +56,8 @@ export function waitForState<T>(observable: IObservable<T>, predicate: (state: T
5356
return new Promise(resolve => {
5457
let didRun = false;
5558
let shouldDispose = false;
56-
const d = autorun('waitForState', reader => {
59+
const d = autorun(reader => {
60+
/** @description waitForState */
5761
const currentState = observable.read(reader);
5862
if (predicate(currentState)) {
5963
if (!didRun) {
@@ -200,6 +204,8 @@ class FromEventObservableSignal extends BaseObservable<void> {
200204

201205
/**
202206
* Creates a signal that can be triggered to invalidate observers.
207+
* Signals don't have a value - when they are triggered they indicate a change.
208+
* However, signals can carry a delta that is passed to observers.
203209
*/
204210
export function observableSignal<TDelta = void>(
205211
debugName: string
@@ -242,7 +248,8 @@ export function debouncedObservable<T>(observable: IObservable<T>, debounceMs: n
242248

243249
let timeout: any = undefined;
244250

245-
disposableStore.add(autorun('debounce', reader => {
251+
disposableStore.add(autorun(reader => {
252+
/** @description debounce */
246253
const value = observable.read(reader);
247254

248255
if (timeout) {
@@ -327,21 +334,21 @@ class KeepAliveObserver implements IObserver {
327334

328335
export function derivedObservableWithCache<T>(name: string, computeFn: (reader: IReader, lastValue: T | undefined) => T): IObservable<T> {
329336
let lastValue: T | undefined = undefined;
330-
const observable = derived(name, reader => {
337+
const observable = derived(reader => {
331338
lastValue = computeFn(reader, lastValue);
332339
return lastValue;
333-
});
340+
}, name);
334341
return observable;
335342
}
336343

337344
export function derivedObservableWithWritableCache<T>(name: string, computeFn: (reader: IReader, lastValue: T | undefined) => T): IObservable<T> & { clearCache(transaction: ITransaction): void } {
338345
let lastValue: T | undefined = undefined;
339346
const counter = observableValue('derivedObservableWithWritableCache.counter', 0);
340-
const observable = derived(name, reader => {
347+
const observable = derived(reader => {
341348
counter.read(reader);
342349
lastValue = computeFn(reader, lastValue);
343350
return lastValue;
344-
});
351+
}, name);
345352
return Object.assign(observable, {
346353
clearCache: (transaction: ITransaction) => {
347354
lastValue = undefined;

0 commit comments

Comments
 (0)