Is there a C # System.Reactive version of the RxJava doOnSubscribe?
In RxJava, I tend to use Observable.doOnSubscribe to register when an observable is subscribed (to know when any data creation \ fetch work is happening) and found it useful to catch errors when calling certain heavy lifting.
The operator Do()
appears to provide the doOnNext(), doOnError(), doOnCompleted()
functionality of RxJava, but I am still missing it, it does not provide functionality similar to doOnSubscribe()
.
I could add registration to the create \ fetch data code, however often this can be an Observable obtained through a third party library and therefore not as convenient as if it had a statement like RxJava doOnSubscribe()
.
Am I missing the C # version doOnSubscribe()
or is there an alternative that would solve my needs?
source to share
Just use Observable.Defer()
:
var someObservable = ...;
var newObservable = Observable.Defer(() =>
{
Console.WriteLine("subscribed!");
return someObservable;
});
You can make your own number if you like:
public static IObservable<T> DoOnSubscribe(this IObservable<T> source, Action action)
{
return Observable.Defer(() =>
{
action();
return source;
});
}
source to share