I have a method void OnAction(Action<Person> callback)
and I wanna create an IObservable<T>
from this using the reactive extensions (Rx).
I have found two methods that could help me: Observable.FromEvent()
and Observable.Start()
:
var observable = Observable.Start(() =>
{
Person person = null;
_mngr.OnAction(p => person = p);
return person;
});
and:
var observable = Observable.FromEvent<Person>(
action => _mngr.OnAction(action), //Add Handler
action => // Remove Handler
{
});
The first one have an closure and I must evaluate if person != null
:
var foo= observable.Where(p =>
{
if(p!=null) //...
});
The second one takes an Action argument that detaches the given event handler from the underlying .NET event... But OnAction method isn't a .NET event.
Both ways works well, but (in my opinion) smells...
So, what is the best way to create an IObservable from OnAction Method?