Esempio n. 1
0
        public async Task SkipUntilCanceled()
        {
            var cts = new CancellationTokenSource();

            var rp = new AsyncReactiveProperty <int>(1);

            var xs = rp.SkipUntilCanceled(cts.Token).ToArrayAsync();

            var c = CancelAsync();

            await c;
            var   foo = await xs;

            foo.Should().Equal(new[] { 20, 30, 40 });

            async Task CancelAsync()
            {
                rp.Value = 10;
                await Task.Yield();

                rp.Value = 20;
                await Task.Yield();

                cts.Cancel();
                rp.Value = 30;
                await Task.Yield();

                rp.Value = 40;

                rp.Dispose(); // complete.
            }
        }
Esempio n. 2
0
        public async Task CombineLatestOK()
        {
            var a = new AsyncReactiveProperty <int>(0);
            var b = new AsyncReactiveProperty <int>(0);

            var list     = new List <(int, int)>();
            var complete = a.WithoutCurrent().CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));

            list.Count.Should().Be(0);

            a.Value = 10;
            list.Count.Should().Be(0);

            a.Value = 20;
            list.Count.Should().Be(0);

            b.Value = 1;
            list.Count.Should().Be(1);

            list[0].Should().Be((20, 1));

            a.Value = 30;
            list.Last().Should().Be((30, 1));

            b.Value = 2;
            list.Last().Should().Be((30, 2));

            a.Dispose();
            b.Value = 3;
            list.Last().Should().Be((30, 3));

            b.Dispose();

            await complete;
        }
Esempio n. 3
0
        public async Task SkipUntil()
        {
            var cts = new AsyncReactiveProperty <int>(0);

            var rp = new AsyncReactiveProperty <int>(1);

            var xs = rp.SkipUntil(cts.WaitAsync()).ToArrayAsync();

            var c = CancelAsync();

            await c;
            var   foo = await xs;

            foo.Should().BeEquivalentTo(new[] { 20, 30, 40 });

            async Task CancelAsync()
            {
                rp.Value = 10;
                await Task.Yield();

                rp.Value = 20;
                await Task.Yield();

                cts.Value = 9999;
                rp.Value  = 30;
                await Task.Yield();

                rp.Value = 40;

                rp.Dispose(); // complete.
            }
        }
Esempio n. 4
0
        public async Task Cancel()
        {
            var rp = new AsyncReactiveProperty <int>(1);

            var multicast = rp.Publish();

            var a = multicast.ToArrayAsync();
            var b = multicast.Take(2).ToArrayAsync();

            var disp = multicast.Connect();

            rp.Value = 2;

            (await b).Should().BeEquivalentTo(1, 2);

            var c = multicast.ToArrayAsync();

            rp.Value = 3;

            disp.Dispose();

            rp.Value = 4;
            rp.Value = 5;

            rp.Dispose();

            await Assert.ThrowsAsync <OperationCanceledException>(async() => await a);

            await Assert.ThrowsAsync <OperationCanceledException>(async() => await c);
        }
Esempio n. 5
0
        public async Task Normal()
        {
            var rp = new AsyncReactiveProperty <int>(1);

            var multicast = rp.Publish();

            var a = multicast.ToArrayAsync();
            var b = multicast.Take(2).ToArrayAsync();

            var disp = multicast.Connect();

            rp.Value = 2;

            (await b).Should().BeEquivalentTo(1, 2);

            var c = multicast.ToArrayAsync();

            rp.Value = 3;
            rp.Value = 4;
            rp.Value = 5;

            rp.Dispose();

            (await a).Should().BeEquivalentTo(1, 2, 3, 4, 5);
            (await c).Should().BeEquivalentTo(3, 4, 5);

            disp.Dispose();
        }
        private void Start()
        {
            var token = this.GetCancellationTokenOnDestroy();

            // AsyncReactiveProperty生成
            var asyncReactiveProperty = new AsyncReactiveProperty <string>("Initialize!");

            // WithoutCurrent()を挟むと現在値の発行をスキップする
            asyncReactiveProperty
            .WithoutCurrent()
            .ForEachAsync(x => Debug.Log(x), token);

            asyncReactiveProperty.Value = "Hello!";
            asyncReactiveProperty.Dispose();
        }
Esempio n. 7
0
        public async Task CombineLatestLong()
        {
            var a = UniTaskAsyncEnumerable.Range(1, 100000);
            var b = new AsyncReactiveProperty <int>(0);

            var list     = new List <(int, int)>();
            var complete = a.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));

            b.Value = 1;

            list[0].Should().Be((100000, 1));

            b.Dispose();

            await complete;
        }
        private void Start()
        {
            var token = this.GetCancellationTokenOnDestroy();

            // AsyncReactiveProperty生成
            var asyncReactiveProperty = new AsyncReactiveProperty <string>("Initialize!");

            // 待受開始
            WaitForAsync(asyncReactiveProperty, token).Forget();

            // 値を設定
            asyncReactiveProperty.Value = "Hello!";
            asyncReactiveProperty.Value = "World!";
            asyncReactiveProperty.Value = "Thank you!";

            asyncReactiveProperty.Dispose();
        }
Esempio n. 9
0
        // 時間を空けて書き込み
        private async UniTaskVoid WriteAsync(
            AsyncReactiveProperty <int> asyncReactiveProperty)
        {
            // 値を設定
            asyncReactiveProperty.Value = 1;
            await UniTask.Yield();

            asyncReactiveProperty.Value = 2;
            await UniTask.Yield();

            asyncReactiveProperty.Value = 3;
            await UniTask.Yield();

            asyncReactiveProperty.Value = -1;
            await UniTask.Yield();

            // Dispose()するとこのAsyncReactivePropertyへの
            // すべてのawaitをキャンセルできる
            asyncReactiveProperty.Dispose();
        }
Esempio n. 10
0
        public async Task Pariwise()
        {
            var a = new AsyncReactiveProperty <int>(0);

            var list     = new List <(int, int)>();
            var complete = a.WithoutCurrent().Pairwise().ForEachAsync(x => list.Add(x));

            list.Count.Should().Be(0);
            a.Value = 10;
            list.Count.Should().Be(0);
            a.Value = 20;
            list.Count.Should().Be(1);
            a.Value = 30;
            a.Value = 40;
            a.Value = 50;

            a.Dispose();

            await complete;

            list.Should().Equal((10, 20), (20, 30), (30, 40), (40, 50));
        }