Add UnityEvent<T> and InputField AsyncEventHandler and extensions

master
neuecc 2020-05-21 02:26:09 +09:00
parent 962c215e3b
commit 3b593f349c
2 changed files with 170 additions and 24 deletions

View File

@ -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<T> GetAsyncEventHandler<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, false);
}
public static UniTask<T> OnInvokeAsync<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<T> OnInvokeAsAsyncEnumerable<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<T>(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<string>(inputField.onEndEdit, cancellationToken);
}
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField)
{
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false);
}
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, false);
}
public static UniTask<string> OnValueChangedAsync(this InputField inputField)
{
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
}
public static UniTask<string> OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField)
{
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, cancellationToken);
}
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);

View File

@ -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<int> Hp { get; }
AsyncReactiveProperty<int> hp;
IReadOnlyAsyncReactiveProperty<int> Hp => hp;
public Model()
{
// hp = new AsyncReactiveProperty<int>();
//setHp = Hp.GetSetter();
}
void Increment(int value)
{
// setHp(Hp.Value += value);
}
}
public Text text;
public Button button;
[SerializeField]
State<int> 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<int>(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<int>();
//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<int>();
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");