コード例 #1
0
        /// <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();
            });
        }
コード例 #2
0
        /// <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 <T> > ToAsync <T>(this 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();
            });
        }
コード例 #3
0
 static AsyncSubject<T> Cancel<T>(AsyncSubject<T> subject, CancellationToken cancellationToken)
 {
     subject.OnError(new OperationCanceledException(cancellationToken));
     return subject;
 }