From fb1152d8f4480b631143a18212a4d1f7e6f25bd7 Mon Sep 17 00:00:00 2001 From: neuecc Date: Tue, 19 May 2020 01:20:20 +0900 Subject: [PATCH] IAsyncReadOnlyReactiveProperty -> IReadOnlyAsyncReactiveProperty, .Dipose retrurns MoveNext -> false --- .../UniTask/Runtime/AsyncReactiveProperty.cs | 19 ++++++++------- src/UniTask/Assets/Scenes/SandboxMain.cs | 23 +++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs index 4e51e43..e196222 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/AsyncReactiveProperty.cs @@ -4,13 +4,13 @@ using System.Threading; namespace Cysharp.Threading.Tasks { - public interface IAsyncReadOnlyReactiveProperty : IUniTaskAsyncEnumerable + public interface IReadOnlyAsyncReactiveProperty : IUniTaskAsyncEnumerable { T Value { get; } IUniTaskAsyncEnumerable WithoutCurrent(); } - public interface IAsyncReactiveProperty : IAsyncReadOnlyReactiveProperty + public interface IAsyncReactiveProperty : IReadOnlyAsyncReactiveProperty { new T Value { get; set; } } @@ -56,7 +56,7 @@ namespace Cysharp.Threading.Tasks public void Dispose() { - triggerEvent.SetCanceled(CancellationToken.None); + triggerEvent.SetCompleted(); } class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable @@ -74,7 +74,7 @@ namespace Cysharp.Threading.Tasks } } - sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator, IResolveCancelPromise + sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator, ITriggerHandler { static Action cancellationCallback = CancellationCallback; @@ -128,17 +128,20 @@ namespace Cysharp.Threading.Tasks return default; } - public bool TrySetResult(T value) + public void OnNext(T value) { this.value = value; completionSource.TrySetResult(true); - return true; } - public bool TrySetCanceled(CancellationToken cancellationToken = default) + public void OnCanceled(CancellationToken cancellationToken) { DisposeAsync().Forget(); - return true; + } + + public void OnCompleted() + { + completionSource.TrySetResult(false); } static void CancellationCallback(object state) diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index 40a8583..6ba3ae5 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -194,23 +194,28 @@ public class SandboxMain : MonoBehaviour Debug.Log("Done"); } - void Start() + async void Start() { - var channel = Channel.CreateSingleConsumerUnbounded(); + //var rp = new AsyncReactiveProperty(10); - var reader = channel.Reader; + //Running(rp).Forget(); - WaitForChannelAsync(reader, this.GetCancellationTokenOnDestroy()).Forget(); + //await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i); - var writer = channel.Writer; + //rp.Dispose(); - writer.TryWrite(1); - writer.TryWrite(2); - writer.TryWrite(3); - writer.Complete(new Exception()); + await this.GetAsyncUpdateTrigger().ForEachAsync(x => Debug.Log("yeah")); + Debug.Log("DONE"); } + async UniTaskVoid Running(IReadOnlyAsyncReactiveProperty rp) + { + Debug.Log("BEGIN"); + await rp.ForEachAsync(x => Debug.Log("AR:" + x)); + Debug.Log("DONE"); + } + async UniTaskVoid WaitForChannelAsync(ChannelReader reader, CancellationToken token) { try