Reputation: 2120
I have a very simple task that I would like to accomplish with rx and I am not sure how to accomplish it. Consider the code below as minimum example.
var subject = new Subject<int>();
subject.OnNext(1);
subject.OnNext(2);
Assert.AreEqual(2, subject.__________());
// > what goes above <
The method should be something exposed only to the IObservable
interface. There are two useful looking candidates that do not do what I hope. The Latest
method will block until the next message which is not the behavior that I am looking for. The MostRecent
documentation looked very promising but failed to deliver what I hoped for.
Upvotes: 1
Views: 301
Reputation: 14350
There's no single method that will do it. Observables need a subscription, which can also take the form of an operator, to propagate values. You need that subscription active when the subject emits (or OnNext
s) the values. There's nothing that caches or stores the values for you.
Here's an example using a raw subscription:
var subject = new Subject<int>();
int last = 0;
using (subject.Subscribe(i => last = i))
{
subject.OnNext(1);
subject.OnNext(2);
}
Assert.AreEqual(2, last);
Upvotes: 2