Beispiel #1
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.
            }
        }
Beispiel #2
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();
        }
Beispiel #3
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;
        }
Beispiel #4
0
        public async Task WaitAsyncTest()
        {
            var rp = new AsyncReactiveProperty <int>(128);

            var f = await rp.FirstAsync();

            f.Should().Be(128);

            {
                var t = rp.WaitAsync();
                rp.Value = 99;
                rp.Value = 100;
                var v = await t;

                v.Should().Be(99);
            }

            {
                var t = rp.WaitAsync();
                rp.Value = 99;
                rp.Value = 100;
                var v = await t;

                v.Should().Be(99);
            }
        }
Beispiel #5
0
        public async Task TakeUntil()
        {
            var cts = new AsyncReactiveProperty <int>(0);

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

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

            var c = CancelAsync();

            await c;
            var   foo = await xs;

            foo.Should().Equal(new[] { 1, 10, 20 });

            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;
            }
        }
Beispiel #6
0
        public async Task TakeUntilCanceled()
        {
            var cts = new CancellationTokenSource();

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

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

            var c = CancelAsync();

            await c;
            var   foo = await xs;

            foo.Should().Equal(new[] { 1, 10, 20 });

            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;
            }
        }
Beispiel #7
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);
        }
Beispiel #8
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.
            }
        }
Beispiel #9
0
 public Player(ControlScheme controlScheme, Color theme, Settings settings)
 {
     id = Guid.NewGuid();
     this.controlScheme = controlScheme;
     this.theme         = theme;
     maxHealth          = settings.startingHealth;
     health             = new AsyncReactiveProperty <float>(maxHealth);
 }
Beispiel #10
0
 public ShipModel(Transform transform, Vector3 position, Quaternion rotation, Settings settings)
 {
     this.transform     = transform;
     transform.position = position;
     transform.rotation = rotation;
     health             = new AsyncReactiveProperty <float>(settings.startingHealth);
     health.Subscribe(HandleChangeHealth, token.Token);
     healthPercentStream = health.Select(h => h / settings.startingHealth).ToReadOnlyAsyncReactiveProperty(token.Token);
 }
Beispiel #11
0
    public void AsyncReactivePropertyTest()
    {
        var rp = new AsyncReactiveProperty <int>(1000);

        //Forget让你对异步直接注册一个回调,不需要再await他了
        rp.ForEachAsync <int>(i => Debug.Log($"{i}")).Forget();

        rp.Value = 11;
        rp.Value = 22;
    }
Beispiel #12
0
        private void Start()
        {
            var token = this.GetCancellationTokenOnDestroy();

            // AsyncReactiveProperty生成
            _current = new AsyncReactiveProperty <int>(0);

            // AsyncReactivePropertyのDispose()呼び出しを
            // CancellationTokenと紐付ける
            _current.AddTo(token);
        }
Beispiel #13
0
        public async Task TakeWhile()
        {
            var rp = new AsyncReactiveProperty <int>(1);

            var xs = rp.TakeWhile(x => x != 5).ToArrayAsync();

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

            (await xs).Should().Equal(1, 2, 3, 4);
        }
Beispiel #14
0
        public async Task Take()
        {
            var rp = new AsyncReactiveProperty <int>(1);

            var xs = rp.Take(5).ToArrayAsync();

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

            (await xs).Should().BeEquivalentTo(1, 2, 3, 4, 5);
        }
Beispiel #15
0
        private void Start()
        {
            var token = this.GetCancellationTokenOnDestroy();

            // AsyncReactiveProperty生成
            var asyncReactiveProperty = new AsyncReactiveProperty <int>(0);

            // 書き込み
            WriteAsync(asyncReactiveProperty).Forget();

            // 待受開始
            WaitForAsync(asyncReactiveProperty, token).Forget();
        }
Beispiel #16
0
        public async Task ReadOnlyWaitAsyncTest()
        {
            var rp  = new AsyncReactiveProperty <int>(128);
            var rrp = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None);

            var t = rrp.WaitAsync();

            rp.Value = 99;
            rp.Value = 100;
            var v = await t;

            v.Should().Be(99);
        }
Beispiel #17
0
        public async Task Q()
        {
            var rp = new AsyncReactiveProperty <int>(100);

            var l = new List <int>();
            await rp.Take(10).Queue().ForEachAsync(x =>
            {
                rp.Value += 10;
                l.Add(x);
            });

            l.Should().Equal(100, 110, 120, 130, 140, 150, 160, 170, 180, 190);
        }
        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();
        }
Beispiel #19
0
        public async Task WaitAsyncCancellationTest()
        {
            var cts = new CancellationTokenSource();

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

            var t = rp.WaitAsync(cts.Token);

            cts.Cancel();

            rp.Value = 99;
            rp.Value = 100;

            await Assert.ThrowsAsync <OperationCanceledException>(async() => { await t; });
        }
Beispiel #20
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;
        }
Beispiel #21
0
        public async Task WithoutCurrent()
        {
            var rp = new AsyncReactiveProperty <int>(99);

            var array = rp.WithoutCurrent().Take(5).ToArrayAsync();

            rp.Value = 100;
            rp.Value = 100;
            rp.Value = 100;
            rp.Value = 131;
            rp.Value = 191;

            var ar = await array;

            ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
        }
        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();
        }
Beispiel #23
0
        public void StateFromEnumeration()
        {
            var rp = new AsyncReactiveProperty <int>(10);

            var state = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None);

            rp.Value = 10;
            state.Value.Should().Be(10);

            rp.Value = 20;
            state.Value.Should().Be(20);

            state.Dispose();

            rp.Value = 30;
            state.Value.Should().Be(20);
        }
Beispiel #24
0
        public async Task Iteration()
        {
            var rp = new AsyncReactiveProperty <int>(99);

            var f = await rp.FirstAsync();

            f.Should().Be(99);

            var array = rp.Take(5).ToArrayAsync();

            rp.Value = 100;
            rp.Value = 100;
            rp.Value = 100;
            rp.Value = 131;

            var ar = await array;

            ar.Should().BeEquivalentTo(new[] { 99, 100, 100, 100, 131 });
        }
Beispiel #25
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();
        }
Beispiel #26
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));
        }
Beispiel #27
0
        public async Task CombineLatestError()
        {
            var a = new AsyncReactiveProperty <int>(0);
            var b = new AsyncReactiveProperty <int>(0);

            var list     = new List <(int, int)>();
            var complete = a.WithoutCurrent()
                           .Select(x => { if (x == 0)
                                          {
                                              throw new MyException();
                                          }
                                          return(x); })
                           .CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));


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

            a.Value = 0;

            await Assert.ThrowsAsync <MyException>(async() => await complete);
        }
Beispiel #28
0
        static async Task Main(string[] args)
        {
#if !DEBUG
            //await new AllocationCheck().ViaUniTaskVoid();
            //Console.ReadLine();
            BenchmarkDotNet.Running.BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

            //await new ComparisonBenchmarks().ViaUniTaskT();
            return;
#endif
            // await new AllocationCheck().ViaUniTaskVoid();

            var buttonTest = new AsyncReactiveProperty <AsyncUnit>(AsyncUnit.Default);

            buttonTest
            .Subscribe(async _ =>
            {
                try
                {
                    await new Foo().MethodFooAsync();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            });

            buttonTest.Value = AsyncUnit.Default;


            // AsyncTest().Forge

            Console.WriteLine("A?");
            var a = await new ZeroAllocAsyncAwaitInDotNetCore().NanikaAsync(1, 2);
            Console.WriteLine("RET:" + a);
            await WhereSelect();

            SynchronizationContext.SetSynchronizationContext(new MySyncContext());

            await Aaa();



            //AsyncTest().Forget();

            // AsyncTest().Forget();

            ThreadPool.SetMinThreads(100, 100);

            //List<UniTask<int>> list = new List<UniTask<int>>();
            for (int i = 0; i < short.MaxValue; i++)
            {
                ////    list.Add(AsyncTest());
                await YieldCore();
            }
            //await UniTask.WhenAll(list);

            //Console.WriteLine("TOGO");

            //var a = await AsyncTest();
            //var b = AsyncTest();
            //var c = AsyncTest();
            await YieldCore();

            //await b;
            //await c;


            //foreach (var item in Cysharp.Threading.Tasks.Internal.TaskPool.GetCacheSizeInfo())
            //{
            //    Console.WriteLine(item);
            //}

            Console.ReadLine();
        }