1
+ import { Disposable } from 'vscode' ;
2
+
1
3
/**
2
4
* A wrapper around a value of type `T` that can be subscribed to whenever the
3
5
* underlying value changes.
@@ -24,46 +26,9 @@ export class Observable<T> {
24
26
* changes.
25
27
* @returns a function that unregisters the listener when called.
26
28
*/
27
- public observe ( fn : ( arg : T ) => void ) : ( ) => void {
29
+ public observe ( fn : ( arg : T ) => void ) : Disposable {
28
30
this . _listeners . add ( fn ) ;
29
31
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 ) } ;
68
33
}
69
34
}
0 commit comments