示例#1
0
        public void Catch_TailRecursive3()
        {
            var ex = new Exception();

            var res =
                Observable.Catch(
                    Observable.Defer(() =>
            {
                if (ex != null)
                {
                    throw ex;
                }

                return(Observable.Return(-2));
            }),
                    Observable.Defer(() =>
            {
                if (ex != null)
                {
                    throw ex;
                }

                return(Observable.Return(-1));
            }),
                    Observable.Return(42)
                    );

            Assert.Equal(42, res.Wait());
        }
示例#2
0
 public void Catch_ArgumentChecking()
 {
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int>((IObservable <int>[])null));
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int>((IEnumerable <IObservable <int> >)null));
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int>(DummyObservable <int> .Instance, null));
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int>((IObservable <int>)null, DummyObservable <int> .Instance));
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int, Exception>(null, _ => DummyObservable <int> .Instance));
     ReactiveAssert.Throws <ArgumentNullException>(() => Observable.Catch <int, Exception>(DummyObservable <int> .Instance, null));
 }
示例#3
0
 public static IObservable <T> LoggedCatch <T>(this IObservable <T> This, LogLevel level = LogLevel.Warn, IObservable <T> next = null, string message = null)
 {
     next = next ?? Observable.Return <T>(default(T));
     return(Observable.Catch <T, Exception>(This, ex =>
     {
         _log.Write(level, message ?? "", ex);
         return next;
     }));
 }
        void Start()
        {
            // 接続先情報を取得する
            _getRoomButton.OnClickAsObservable()
            .ThrottleFirst(TimeSpan.FromSeconds(2))
            .Subscribe(_ =>
            {
                // ユーザ生放送のAPIを先に実行し、失敗したら公式側のAPIを叩く
                Observable.Catch(new[]
                {
                    _manager.NicoliveApiClient.GetProgramInfoAsync().Select(x => x.Rooms.Select(r => r.CommentServerInfo)),
                }).Subscribe(comments =>
                {
                    _commentServerInfos.Value = comments.ToArray();
                }, ex => _commentServerInfos.Value = new CommentServerInfo[0]);
            });

            //部屋数が更新されたらUIへ反映する
            _commentServerInfos
            .Where(x => x != null)
            .Select(x => x.Length)
            .DistinctUntilChanged()
            .Subscribe(x =>
            {
                //接続していない かつ 部屋数が1以上のときのみ接続できる
                _connectButton.interactable = !_isConnected.Value && x > 0;
                _roomCountText.text         = string.Format("現在の部屋数:{0}", x);
            });

            //接続状態が更新されたらボタンに反映する
            _isConnected.Subscribe(x =>
            {
                _connectButton.interactable    = !x && _manager.CurrentRooms.Count > 0;
                _disconnectButton.interactable = x;
            });

            //コメントサーバに接続
            _connectButton.OnClickAsObservable()
            .Subscribe(_ =>
            {
                Connect();
                _isConnected.Value = true;
            });

            //コメントサーバから切断
            _disconnectButton.OnClickAsObservable()
            .Subscribe(_ =>
            {
                Disconnect();
                _isConnected.Value = false;
            });

            //GameObjectが破棄されたら切断する
            this.OnDestroyAsObservable()
            .Subscribe(_ => Disconnect());
        }
示例#5
0
        public void Bug_1297_Catch_None()
        {
            var scheduler = new TestScheduler();

            var results = scheduler.Run(() => Observable.Catch <int>(scheduler));

            results.AssertEqual(
                OnCompleted <int>(201)
                );
        }
示例#6
0
        public void Catch_IEofIO()
        {
            var scheduler = new TestScheduler();

            var xs1 = scheduler.CreateColdObservable <int>(
                OnNext(10, 1),
                OnNext(20, 2),
                OnNext(30, 3),
                OnError <int>(40, new Exception())
                );

            var xs2 = scheduler.CreateColdObservable <int>(
                OnNext(10, 4),
                OnNext(20, 5),
                OnError <int>(30, new Exception())
                );

            var xs3 = scheduler.CreateColdObservable <int>(
                OnNext(10, 6),
                OnNext(20, 7),
                OnNext(30, 8),
                OnNext(40, 9),
                OnCompleted <int>(50)
                );

            var res = scheduler.Start(() =>
                                      Observable.Catch(new[] { xs1, xs2, xs3 })
                                      );

            res.Messages.AssertEqual(
                OnNext(210, 1),
                OnNext(220, 2),
                OnNext(230, 3),
                OnNext(250, 4),
                OnNext(260, 5),
                OnNext(280, 6),
                OnNext(290, 7),
                OnNext(300, 8),
                OnNext(310, 9),
                OnCompleted <int>(320)
                );

            xs1.Subscriptions.AssertEqual(
                Subscribe(200, 240)
                );

            xs2.Subscriptions.AssertEqual(
                Subscribe(240, 270)
                );

            xs3.Subscriptions.AssertEqual(
                Subscribe(270, 320)
                );
        }
示例#7
0
        public void Catch_EmptyIterator()
        {
            var scheduler = new TestScheduler();

            var res = scheduler.Start(() =>
                                      Observable.Catch <int>((IEnumerable <IObservable <int> >) new IObservable <int> [0])
                                      );

            res.Messages.AssertEqual(
                OnCompleted <int>(200)
                );
        }
示例#8
0
        public void Bug_1297_Catch_None()
        {
            var scheduler = new TestScheduler();

            var results = scheduler.Start(() =>
                                          Observable.Catch <int>()
                                          );

            results.Messages.AssertEqual(
                OnCompleted <int>(200)
                );
        }
示例#9
0
        public void Bug_1297_Catch_Single()
        {
            var scheduler = new TestScheduler();

            var xs = Observable.Throw <int>(new MockException(1), scheduler);

            var results = scheduler.Run(() => Observable.Catch(scheduler, xs));

            results.AssertEqual(
                OnError <int>(203, new MockException(1))
                );
        }
示例#10
0
        public void Catch_IteratorThrows()
        {
            var scheduler = new TestScheduler();
            var ex        = new Exception();

            var res = scheduler.Start(() =>
                                      Observable.Catch <int>(Catch_IteratorThrows_Source(ex, true))
                                      );

            res.Messages.AssertEqual(
                OnError <int>(200, ex)
                );
        }
示例#11
0
        public void Bug_1297_Catch_Multi()
        {
            var scheduler = new TestScheduler();

            var xs = Observable.Throw <int>(new MockException(1), scheduler);
            var ys = Observable.Throw <int>(new MockException(2), scheduler);
            var zs = Observable.Throw <int>(new MockException(3), scheduler);

            var results = scheduler.Run(() => Observable.Catch(scheduler, xs, ys, zs));

            results.AssertEqual(
                OnError <int>(207, new MockException(3))
                );
        }
示例#12
0
        public void Catch_DefaultScheduler_Nary()
        {
            var evt = new ManualResetEvent(false);

            int res = 0;

            Observable.Catch(Observable.Return(1), Observable.Return(2), Observable.Return(3)).Subscribe(x =>
            {
                res = x;
                evt.Set();
            });

            evt.WaitOne();
            Assert.Equal(1, res);
        }
示例#13
0
        public void Catch_IEofIO_GetEnumeratorThrows()
        {
            var ex = new Exception();

            var scheduler = new TestScheduler();

            var xss = new RogueEnumerable <IObservable <int> >(ex);

            var res = scheduler.Start(() =>
                                      Observable.Catch(xss)
                                      );

            res.Messages.AssertEqual(
                OnError <int>(200, ex)
                );
        }
示例#14
0
        public void Bug_1297_Catch_Single()
        {
            var scheduler = new TestScheduler();

            var ex = new Exception();

            var xs = Observable.Throw <int>(ex, scheduler);

            var results = scheduler.Start(() =>
                                          Observable.Catch(xs)
                                          );

            results.Messages.AssertEqual(
                OnError <int>(201, ex)
                );
        }
示例#15
0
        public void Catch_DefaultScheduler_NaryEnumerable()
        {
            var evt = new ManualResetEvent(false);

            IEnumerable <IObservable <int> > sources = new[] { Observable.Return(1), Observable.Return(2), Observable.Return(3) };

            int res = 0;

            Observable.Catch(sources).Subscribe(x =>
            {
                res = x;
                evt.Set();
            });

            evt.WaitOne();
            Assert.Equal(1, res);
        }
示例#16
0
        public void Catch_Multiple()
        {
            var scheduler = new TestScheduler();

            var ex = new Exception();

            var o1 = scheduler.CreateHotObservable(
                OnNext(150, 1),
                OnNext(210, 2),
                OnError <int>(215, ex)
                );

            var o2 = scheduler.CreateHotObservable(
                OnNext(220, 3),
                OnError <int>(225, ex)
                );

            var o3 = scheduler.CreateHotObservable(
                OnNext(230, 4),
                OnCompleted <int>(235)
                );

            var res = scheduler.Start(() =>
                                      Observable.Catch(o1, o2, o3)
                                      );

            res.Messages.AssertEqual(
                OnNext(210, 2),
                OnNext(220, 3),
                OnNext(230, 4),
                OnCompleted <int>(235)
                );

            o1.Subscriptions.AssertEqual(
                Subscribe(200, 215)
                );

            o2.Subscriptions.AssertEqual(
                Subscribe(215, 225)
                );

            o3.Subscriptions.AssertEqual(
                Subscribe(225, 235)
                );
        }
示例#17
0
        public void Bug_1297_Catch_Multi()
        {
            var scheduler = new TestScheduler();

            var ex1 = new Exception();
            var ex2 = new Exception();
            var ex3 = new Exception();

            var xs = Observable.Throw <int>(ex1, scheduler);
            var ys = Observable.Throw <int>(ex2, scheduler);
            var zs = Observable.Throw <int>(ex3, scheduler);

            var results = scheduler.Start(() =>
                                          Observable.Catch(xs, ys, zs)
                                          );

            results.Messages.AssertEqual(
                OnError <int>(203, ex3)
                );
        }
示例#18
0
文件: Catch.cs 项目: aalmada/bonsai
 /// <summary>
 /// Continues an observable sequence that is terminated by an exception with
 /// the next observable sequence.
 /// </summary>
 /// <typeparam name="TSource">The type of the elements in the source and handler sequences.</typeparam>
 /// <param name="sources">The observable sequences to catch exceptions for.</param>
 /// <returns>
 /// An observable sequence containing elements from consecutive source sequences
 /// until a source sequence terminates successfully.
 /// </returns>
 public IObservable <TSource> Process <TSource>(params IObservable <TSource>[] sources)
 {
     return(Observable.Catch(sources));
 }