public static Func <T1, T2, IObservable <TResult> > FromAsyncPattern <T1, T2, TResult>(Func <T1, T2, AsyncCallback, object, IAsyncResult> begin, Func <IAsyncResult, TResult> end) { return((x, y) => { var subject = new AsyncSubject <TResult>(); try { begin(x, y, iar => { TResult result; try { result = end(iar); } catch (Exception exception) { subject.OnError(exception); return; } subject.OnNext(result); subject.OnCompleted(); }, null); } catch (Exception exception) { return Observable.Throw <TResult>(exception, Scheduler.DefaultSchedulers.AsyncConversions); } return subject.AsObservable(); }); }
public static Func <IObservable <T> > ToAsync <T>(Func <T> function, IScheduler scheduler) { return(() => { var subject = new AsyncSubject <T>(); scheduler.Schedule(() => { var result = default(T); try { result = function(); } catch (Exception exception) { subject.OnError(exception); return; } subject.OnNext(result); subject.OnCompleted(); }); return subject.AsObservable(); }); }
private static IObservable <TResult> ToObservableResult <TResult>(AsyncSubject <TResult> subject, IScheduler scheduler) { if (scheduler != null) { return(subject.ObserveOn(scheduler)); } else { return(subject.AsObservable()); } }
/// <summary> /// <para>Create an Observable that emits the return value of a function-like directive asynchronously</para> /// /// <para>There are a number of ways that programming languages have for obtaining values as /// the result of calculations, with names like functions, futures, actions, callables, /// runnables, and so forth. The operators grouped here under the Start operator category /// make these things behave like Observables so that they can be chained with other /// Observables in an Observable cascade</para> /// </summary> /// <see cref="http://reactivex.io/documentation/operators/start.html"/> public static Func <IObservable <Unit> > ToAsync(this Action action, IScheduler scheduler) { return(() => { var subject = new AsyncSubject <Unit>(); scheduler.Schedule(() => { try { action(); } catch (Exception exception) { subject.OnError(exception); return; } subject.OnNext(Unit.Default); subject.OnCompleted(); }); return subject.AsObservable(); }); }