Esempio n. 1
0
 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();
     });
 }
Esempio n. 2
0
        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());
     }
 }
Esempio n. 4
0
        public static Func <IObservable <Unit> > ToAsync(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();
            });
        }