Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit eb5cdf7

Browse files
authored
Merge pull request #776 from rust-lang/observer-was-too-much
Remove overly clever/unnecessary Observer logic
2 parents 8555126 + 1f66b4a commit eb5cdf7

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

src/extension.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
2929
import { startSpinner, stopSpinner } from './spinner';
3030
import { activateTaskProvider, Execution, runRlsCommand } from './tasks';
3131
import { withWsl } from './utils/child_process';
32-
import { Observable, Observer } from './utils/observable';
32+
import { Observable } from './utils/observable';
3333
import { nearestParentWorkspace } from './utils/workspace';
3434
import { uriWindowsToWsl, uriWslToWindows } from './utils/wslpath';
3535

@@ -91,7 +91,7 @@ export async function deactivate() {
9191
}
9292

9393
/** Tracks dynamically updated progress for the active client workspace for UI purposes. */
94-
const progressObserver: Observer<{ message: string } | null> = new Observer();
94+
let progressObserver: Disposable | undefined;
9595

9696
function onDidChangeActiveTextEditor(editor: TextEditor | undefined) {
9797
if (!editor || !editor.document) {
@@ -116,7 +116,10 @@ function onDidChangeActiveTextEditor(editor: TextEditor | undefined) {
116116
}
117117
};
118118

119-
progressObserver.bind(activeWorkspace.progress, updateProgress);
119+
if (progressObserver) {
120+
progressObserver.dispose();
121+
}
122+
progressObserver = activeWorkspace.progress.observe(updateProgress);
120123
// Update UI ourselves immediately and don't wait for value update callbacks
121124
updateProgress(activeWorkspace.progress.value);
122125
}

src/utils/observable.ts

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Disposable } from 'vscode';
2+
13
/**
24
* A wrapper around a value of type `T` that can be subscribed to whenever the
35
* underlying value changes.
@@ -24,46 +26,9 @@ export class Observable<T> {
2426
* changes.
2527
* @returns a function that unregisters the listener when called.
2628
*/
27-
public observe(fn: (arg: T) => void): () => void {
29+
public observe(fn: (arg: T) => void): Disposable {
2830
this._listeners.add(fn);
2931

30-
return () => this._listeners.delete(fn);
31-
}
32-
}
33-
34-
/**
35-
* Capable of observing an `Observable<T>` type.
36-
*
37-
* Convenient when using a single observer that potentially binds multiple times
38-
* to different observables, where it automatically unregisters from previous
39-
* observables.
40-
*/
41-
// tslint:disable-next-line: max-classes-per-file
42-
export class Observer<T> {
43-
private _observable?: Observable<T>;
44-
private _stopObserving?: () => void;
45-
/** Returns the current value of a bound observable, if there is one. */
46-
get value() {
47-
return this._observable && this._observable.value;
48-
}
49-
/**
50-
* Binds to an observable value, along with the provided listener that's
51-
* called whenever the underlying value changes.
52-
*/
53-
public bind(observable: Observable<T>, handler: (arg: T) => void) {
54-
this.stop();
55-
56-
this._observable = observable;
57-
this._stopObserving = observable.observe(handler);
58-
}
59-
/** Unbinds from the observable, deregistering the previously bound callback. */
60-
public stop() {
61-
if (this._stopObserving) {
62-
this._stopObserving();
63-
delete this._stopObserving;
64-
}
65-
if (this._observable) {
66-
delete this._observable;
67-
}
32+
return { dispose: () => this._listeners.delete(fn) };
6833
}
6934
}

0 commit comments

Comments
 (0)