What is the cleanest way to perform a side-effect when the last subscription for a RxJS Observable is disposed? This may occur before the Observable has terminated.
Let's say that I need a function returning an Observable
that emits changes to a resource. I'd like to perform a cleanup action when all subscriptions have been disposed.
var observable = streamResourceChanges(resource);
var subscription1 = observable.subscribe(observer1);
var subscription2 = observable.subscribe(observer2);
// ...
subscription1.dispose(); // Does not perform the cleanup
subscription2.dispose(); // Performs the cleanup
The only way I've found to define a subscription disposal action is to use Rx.Observable.create
. The last disposal can be handled by sharing a subscription, for example with Observable.prototype.singleInstance()
.
For example:
function streamResourceChanges(resource) {
return Rx.Observable.create(function(observer) {
// Subscribe the observer for resource changes...
// Return a cleanup function
return function() {
// Perform cleanup here...
console.log("Cleanup performed!");
};
}).singleInstance();
}
Is there a neater way to define a side-effect for subscription disposal, similar to doOnNext
, doOnCompleted
or doOnError
?
var withCleanup = withoutCleanup.doOnDispose(function() {
// Perform cleanup here...
});
using
method sounds like a valid alternative forcreate
! Thefinally
callback runs only when the Observable terminates, which may not necessarily be the case when the last subscription is disposed. Sorry I wasn't very specific about this; I edited my question. – Thanos