I used 101 samples of Rx Framework ( http://rxwiki.wikidot.com/101samples#toc47 ) last example and created a class like below and usage like in the test function.
private void Test()
{
var order = new Order();
order.ObservableOrder.Subscribe(
ord => Console.WriteLine("Order progress "), // subscribe to onnext event
ex => Console.WriteLine("Paid error " + ex.Message), // subscribe to error event
() => Console.WriteLine("Paid oncompleted ") // subscribe to completed event
); // Subscribe
order.Start();
}
public class Order
{
private readonly Subject<Order> _subject = new Subject<Order>();
public IObservable<Order> ObservableOrder { get { return _subject.AsObservable(); } }
public void RaiseError() {
_subject.OnError(new Exception("test exception"));
}
public void RaiseCompleted() {
_subject.OnCompleted();
}
public void RaiseProgress() {
_subject.OnNext(this);
}
public void Start() {
for (int i = 0; i < 5; i++)
this.RaiseProgress();
this.RaiseError(); // either error is raised
this.RaiseCompleted(); // or completed is raised
}
}
My requirement is how to write a base class ( or some kind of factory ) to make it easy to use this for 20-30 classes which does similar things ( like they need to send progress, oncompleted and on error events ) Also how can I also send some data back to on progress and oncompleted events ( like we send exception into onerror event )