diff --git a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs index c107e9f..1ab4f1c 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Runtime/UnityAsyncExtensions.uGUI.cs @@ -1,9 +1,8 @@ #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member +using Cysharp.Threading.Tasks.Linq; using System; using System.Threading; -using Cysharp.Threading.Tasks.Internal; -using Cysharp.Threading.Tasks.Linq; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; @@ -27,6 +26,21 @@ namespace Cysharp.Threading.Tasks return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); } + public static AsyncUnityEventHandler GetAsyncEventHandler(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, false); + } + + public static UniTask OnInvokeAsync(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(unityEvent, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnInvokeAsAsyncEnumerable(this UnityEvent unityEvent, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken); + } + public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button) { return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false); @@ -207,6 +221,36 @@ namespace Cysharp.Threading.Tasks return new UnityEventHandlerAsyncEnumerable(inputField.onEndEdit, cancellationToken); } + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false); + } + + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, false); + } + + public static UniTask OnValueChangedAsync(this InputField inputField) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync(); + } + + public static UniTask OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken) + { + return new AsyncUnityEventHandler(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync(); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy()); + } + + public static IUniTaskAsyncEnumerable OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken) + { + return new UnityEventHandlerAsyncEnumerable(inputField.onValueChanged, cancellationToken); + } + public static IAsyncValueChangedEventHandler GetAsyncValueChangedEventHandler(this Dropdown dropdown) { return new AsyncUnityEventHandler(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false); diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index bbf75bb..825bc0d 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -35,8 +35,33 @@ public enum MyEnum } +public class SimplePresenter +{ + // View + public UnityEngine.UI.InputField Input; + // Presenter + + + public SimplePresenter() + { + //Input.OnValueChangedAsAsyncEnumerable() + // .Queue() + // .SelectAwait(async x => + // { + // await UniTask.Delay(TimeSpan.FromSeconds(1)); + // return x; + // }) + // .Select(x=> x.ToUpper()) + // .BindTo( + + + + } + +} + @@ -87,7 +112,7 @@ public class SandboxMain : MonoBehaviour { public Button okButton; public Button cancelButton; - public Text text; + CancellationTokenSource cts; @@ -110,6 +135,63 @@ public class SandboxMain : MonoBehaviour } + + + + public class Model + { + // State Hp { get; } + + AsyncReactiveProperty hp; + IReadOnlyAsyncReactiveProperty Hp => hp; + + + + public Model() + { + // hp = new AsyncReactiveProperty(); + + + + + + + //setHp = Hp.GetSetter(); + } + + void Increment(int value) + { + + + // setHp(Hp.Value += value); + } + } + + + + public Text text; + public Button button; + + [SerializeField] + State count; + + void Start2() + { + count = 10; + + var countS = count.GetSetter(); + + count.BindTo(text); + button.OnClickAsAsyncEnumerable().ForEachAsync(_ => + { + // int foo = countS; + //countS.Set(countS += 10); + + // setter.SetValue(count.Value + 10); + }); + } + + async UniTask RunStandardDelayAsync() { UnityEngine.Debug.Log("DEB"); @@ -126,10 +208,6 @@ public class SandboxMain : MonoBehaviour var scheduled = job.Schedule(); - - - - UnityEngine.Debug.Log("OK"); await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update); UnityEngine.Debug.Log("OK2"); @@ -170,40 +248,64 @@ public class SandboxMain : MonoBehaviour Debug.Log("Done"); } + public int MyProperty { get; set; } + + public class MyClass + { + public int MyProperty { get; set; } + } + + MyClass mcc; + void Start() { - //var rp = new AsyncReactiveProperty(10); + this.mcc = new MyClass(); + this.MyProperty = 999; - //Running(rp).Forget(); - - //await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i); - - //rp.Dispose(); - - //var channel = Channel.CreateSingleConsumerUnbounded(); - //Debug.Log("wait channel"); - //await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { }); + CheckDest().Forget(); + + + //UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty) + // .Do(_ => { }, () => Debug.Log("COMPLETED")) + // .ForEachAsync(x => + // { + // Debug.Log("VALUE_CHANGED:" + x); + // }) + // .Forget(); - var pubsub = new AsyncMessageBroker(); - - pubsub.Subscribe().ForEachAsync(x => Debug.Log("A:" + x)).Forget(); - pubsub.Subscribe().ForEachAsync(x => Debug.Log("B:" + x)).Forget(); - int i = 0; okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ => { - Debug.Log("foo"); - pubsub.Publish(i++); + mcc.MyProperty += 10; + }).Forget(); + cancelButton.OnClickAsAsyncEnumerable().ForEachAsync(_ => + { + this.mcc = null; + }); } + async UniTaskVoid CheckDest() + { + try + { + Debug.Log("WAIT"); + await UniTask.WaitUntilValueChanged(mcc, x => x.MyProperty); + Debug.Log("CHANGED?"); + } + finally + { + Debug.Log("END"); + } + } + async UniTaskVoid Running(CancellationToken ct) { Debug.Log("BEGIN");