Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# System.Reactive version of RxJava's doOnSubscribe?

In RxJava I tend to use Observable.doOnSubscribe to log when an observable is subscribed (to know when some work to create\fetch data is happening) to and found it useful to catch mistakes on when certain heavy work is invoked.

The Do() operator does seem to provide doOnNext(), doOnError(), doOnCompleted() RxJava functionality however uniess I'm missing it, it doesn't seem to provide functionality similar to doOnSubscribe().

I could add logging to the create\fetch data code however often this could be an Observable sourced via a 3rd party library and thus not as convenient vs having an operator such as RxJava's doOnSubscribe() it seems.

Am I missing the C# version of doOnSubscribe() or is there an alternative that would solve my needs?

like image 322
Jimmy Alexander Avatar asked May 02 '17 18:05

Jimmy Alexander


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

What is the mean of AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high. 60 Motel Units. All Units A/C, Heat, Cable TV.

Is AC the same as AC?

In the air conditioning industry, the term HVAC is often used instead of AC. HVAC refers to heating, ventilation, and air conditioning, whereas AC simply refers to air conditioning. AC is generally used when referring to systems that are designed to cool the air in your home.


1 Answers

Just use Observable.Defer():

var someObservable = ...;
var newObservable = Observable.Defer(() =>
{
    Console.WriteLine("subscribed!");
    return someObservable;
});

You can make your own extension if you wish:

public static IObservable<T> DoOnSubscribe(this IObservable<T> source, Action action)
{
    return Observable.Defer(() =>
    {
        action();
        return source;
    });
}
like image 105
Brandon Avatar answered Jan 01 '23 09:01

Brandon