Is there a hot equivalent of Observable.Interval

If I do the following:

var obs = 
    Observable
    .Interval(TimeSpan.FromSeconds(1))
    .Select(x => "A" + x.ToString());

obs
.Subscribe(x => Console.WriteLine("From first: " + x.ToString()));

Observable
.Timer(TimeSpan.FromSeconds(3))
.SelectMany(_ => obs)
.Subscribe(x => Console.WriteLine("From second: " + x.ToString()));

      

I will get this in 4 seconds:

From first:  A0
From first:  A1
From first:  A2
From second: A0
From first:  A3

      

Is there a hot equivalent Observable.Interval

that will produce this:

From first:  A0
From first:  A1
From first:  A2
From second: A3
From first:  A3

      

+3


source to share


1 answer


using the Publish () and Connect () functions you will be able to disable hot hot recording.

var published = Observable
   .Interval(...)
   .Select(...)
   .Publish();

var connectionSubscription = published.Connect();
var observerSubscription = published.Subscribe(...);

      



It's worth noting that the sequence will be hot after the call to Connect (). You can subscribe before Connect (), but make sure you call it at some point or you won't see anything. There are several alternatives to Connect (), for example. RefCount () is so worth google. It's also worth noting that Publish () returns an IConnectableObservable, which provides a call to Connect ().

+4


source







All Articles