take the following as an example:
var ob = Observable.Interval(TimeSpan.FromSeconds(1)).StartWith(500).Replay(1).RefCount();
What I'm trying to achieve here is to obtain the value of the latest item in the sequence at any given time "synchronously". Which means extensions like FirstAsync can't make it up for me.
The StartWith and Replay bit ensures that there will always be a value, and the RefCount bit is necessary in my actual code to detect when I can do some disposal actions.
So to simulate this "any given time" part, let's try getting the latest value after 5 seconds:
Observable.Timer(TimeSpan.FromSeconds(5)).Subscribe(x =>
{
// Try to get latest value from "ob" here.
});
So with a 5 second delay, I need to get the value 5 out of the sequence and these are what I have tried so far with no success:
ob.First()- returns 500ob.Latest().Take(1)- same as aboveob.MostRecent(-1).First()- same as aboveob.MostRecent(-1)- gives me anIEnumerable<long>full of "500"ob.Last()- never returns because it's waiting for the sequence to complete which it never willob.Latest().Last()- same as aboveob.ToTask().Result- same as aboveob.ToEnumerable()- same as aboveob.MostRecent().Last()same as above
It seems there's not much resources around that people can actually do this. The closest I can find is this: "Rx: operator for getting first and most recent value from an Observable stream", but it is not a synchronous call after all (still using a subscription) so it doesn't work for me.
Does any body know if this is actually doable?
StartWithandReplay(1)could also be swapped withPublish(500)giving youBehaviourSubject<T>semantics - Lee Campbell