Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { checkForRls, ensureToolchain, rustupUpdate } from './rustup';
import { startSpinner, stopSpinner } from './spinner';
import { activateTaskProvider, Execution, runRlsCommand } from './tasks';
import { withWsl } from './utils/child_process';
import { Observable, Observer } from './utils/observable';
import { Observable } from './utils/observable';
import { nearestParentWorkspace } from './utils/workspace';
import { uriWindowsToWsl, uriWslToWindows } from './utils/wslpath';

Expand Down Expand Up @@ -91,7 +91,7 @@ export async function deactivate() {
}

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

function onDidChangeActiveTextEditor(editor: TextEditor | undefined) {
if (!editor || !editor.document) {
Expand All @@ -116,7 +116,10 @@ function onDidChangeActiveTextEditor(editor: TextEditor | undefined) {
}
};

progressObserver.bind(activeWorkspace.progress, updateProgress);
if (progressObserver) {
progressObserver.dispose();
}
progressObserver = activeWorkspace.progress.observe(updateProgress);
// Update UI ourselves immediately and don't wait for value update callbacks
updateProgress(activeWorkspace.progress.value);
}
Expand Down
43 changes: 4 additions & 39 deletions src/utils/observable.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Disposable } from 'vscode';

/**
* A wrapper around a value of type `T` that can be subscribed to whenever the
* underlying value changes.
Expand All @@ -24,46 +26,9 @@ export class Observable<T> {
* changes.
* @returns a function that unregisters the listener when called.
*/
public observe(fn: (arg: T) => void): () => void {
public observe(fn: (arg: T) => void): Disposable {
this._listeners.add(fn);

return () => this._listeners.delete(fn);
}
}

/**
* Capable of observing an `Observable<T>` type.
*
* Convenient when using a single observer that potentially binds multiple times
* to different observables, where it automatically unregisters from previous
* observables.
*/
// tslint:disable-next-line: max-classes-per-file
export class Observer<T> {
private _observable?: Observable<T>;
private _stopObserving?: () => void;
/** Returns the current value of a bound observable, if there is one. */
get value() {
return this._observable && this._observable.value;
}
/**
* Binds to an observable value, along with the provided listener that's
* called whenever the underlying value changes.
*/
public bind(observable: Observable<T>, handler: (arg: T) => void) {
this.stop();

this._observable = observable;
this._stopObserving = observable.observe(handler);
}
/** Unbinds from the observable, deregistering the previously bound callback. */
public stop() {
if (this._stopObserving) {
this._stopObserving();
delete this._stopObserving;
}
if (this._observable) {
delete this._observable;
}
return { dispose: () => this._listeners.delete(fn) };
}
}