コード例 #1
0
        public static IObservable <T> ToObservable <T>(this UniTask <T> task)
        {
            if (task.IsCompleted)
            {
                try
                {
                    return(Observable.Return <T>(task.GetAwaiter().GetResult()));
                }
                catch (Exception ex)
                {
                    return(Observable.Throw <T>(ex));
                }
            }

            var subject = new AsyncSubject <T>();

            Fire(subject, task).Forget();
            return(subject);
        }
コード例 #2
0
        static AsyncSubject <TSource> RunAsync <TSource>(IConnectableObservable <TSource> source, CancellationToken cancellationToken)
        {
            AsyncSubject <TSource> s = new AsyncSubject <TSource>();

            if (cancellationToken.IsCancellationRequested)
            {
                return(Cancel(s, cancellationToken));
            }

            IDisposable d = source.Subscribe(s);
            IDisposable c = source.Connect();

            if (cancellationToken.CanBeCanceled)
            {
                RegisterCancelation(s, StableCompositeDisposable.Create(d, c), cancellationToken);
            }

            return(s);
        }
コード例 #3
0
        public static IObservable <Unit> ToObservable(this UniTask task)
        {
            if (task.IsCompleted)
            {
                try
                {
                    return(Observable.ReturnUnit());
                }
                catch (Exception ex)
                {
                    return(Observable.Throw <Unit>(ex));
                }
            }

            var subject = new AsyncSubject <Unit>();

            Fire(subject, task).Forget();
            return(subject);
        }
コード例 #4
0
		static void RegisterCancelation<T>(AsyncSubject<T> subject, IDisposable subscription, CancellationToken token) {
			//
			// Separate method used to avoid heap allocation of closure when no cancellation is needed,
			// e.g. when CancellationToken.None is provided to the RunAsync overloads.
			//

			var ctr = token.Register(() => {
				subscription.Dispose();
				Cancel(subject, token);
			});

			//
			// No null-check for ctr is needed:
			//
			// - CancellationTokenRegistration is a struct
			// - Registration will succeed 99% of the time, no warranting an attempt to avoid spurious Subscribe calls
			//
			subject.Subscribe(Stubs<T>.Ignore, _ => ctr.Dispose(), ctr.Dispose);
		}
コード例 #5
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();
            });
        }
コード例 #6
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();
            });
        }
コード例 #7
0
            public void Dispose()
            {
                lock (gate) {
                    if (parent != null)
                    {
                        lock (parent.observerLock) {
                            var listObserver = parent.outObserver as ListObserver <T>;
                            if (listObserver != null)
                            {
                                parent.outObserver = listObserver.Remove(unsubscribeTarget);
                            }
                            else
                            {
                                parent.outObserver = EmptyObserver <T> .Instance;
                            }

                            unsubscribeTarget = null;
                            parent            = null;
                        }
                    }
                }
            }
コード例 #8
0
 public Subscription(AsyncSubject <T> parent, IObserver <T> unsubscribeTarget)
 {
     this.parent            = parent;
     this.unsubscribeTarget = unsubscribeTarget;
 }
コード例 #9
0
 static AsyncSubject<T> Cancel<T>(AsyncSubject<T> subject, CancellationToken cancellationToken)
 {
     subject.OnError(new OperationCanceledException(cancellationToken));
     return subject;
 }
コード例 #10
0
ファイル: AsyncSubject.cs プロジェクト: YousicianGit/UniRx
 public AsyncSubjectDisposable(AsyncSubject <T> parent, IObserver <T> downstream)
 {
     _parent    = parent;
     Downstream = downstream;
 }