Add UnityEvent<T> and InputField AsyncEventHandler and extensions
parent
962c215e3b
commit
3b593f349c
|
@ -1,9 +1,8 @@
|
||||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
using Cysharp.Threading.Tasks.Linq;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
@ -27,6 +26,21 @@ namespace Cysharp.Threading.Tasks
|
||||||
return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
|
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)
|
public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
||||||
|
@ -207,6 +221,36 @@ namespace Cysharp.Threading.Tasks
|
||||||
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
|
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)
|
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
||||||
|
|
|
@ -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 okButton;
|
||||||
public Button cancelButton;
|
public Button cancelButton;
|
||||||
public Text text;
|
|
||||||
|
|
||||||
CancellationTokenSource cts;
|
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()
|
async UniTask RunStandardDelayAsync()
|
||||||
{
|
{
|
||||||
UnityEngine.Debug.Log("DEB");
|
UnityEngine.Debug.Log("DEB");
|
||||||
|
@ -126,10 +208,6 @@ public class SandboxMain : MonoBehaviour
|
||||||
|
|
||||||
var scheduled = job.Schedule();
|
var scheduled = job.Schedule();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
UnityEngine.Debug.Log("OK");
|
UnityEngine.Debug.Log("OK");
|
||||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||||
UnityEngine.Debug.Log("OK2");
|
UnityEngine.Debug.Log("OK2");
|
||||||
|
@ -170,40 +248,64 @@ public class SandboxMain : MonoBehaviour
|
||||||
Debug.Log("Done");
|
Debug.Log("Done");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int MyProperty { get; set; }
|
||||||
|
|
||||||
|
public class MyClass
|
||||||
|
{
|
||||||
|
public int MyProperty { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
MyClass mcc;
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
//var rp = new AsyncReactiveProperty<int>(10);
|
this.mcc = new MyClass();
|
||||||
|
this.MyProperty = 999;
|
||||||
|
|
||||||
//Running(rp).Forget();
|
CheckDest().Forget();
|
||||||
|
|
||||||
//await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i);
|
|
||||||
|
//UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty)
|
||||||
//rp.Dispose();
|
// .Do(_ => { }, () => Debug.Log("COMPLETED"))
|
||||||
|
// .ForEachAsync(x =>
|
||||||
//var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
// {
|
||||||
//Debug.Log("wait channel");
|
// Debug.Log("VALUE_CHANGED:" + x);
|
||||||
//await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { });
|
// })
|
||||||
|
// .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(_ =>
|
okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||||
{
|
{
|
||||||
|
|
||||||
Debug.Log("foo");
|
mcc.MyProperty += 10;
|
||||||
pubsub.Publish(i++);
|
|
||||||
|
|
||||||
|
|
||||||
}).Forget();
|
}).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)
|
async UniTaskVoid Running(CancellationToken ct)
|
||||||
{
|
{
|
||||||
Debug.Log("BEGIN");
|
Debug.Log("BEGIN");
|
||||||
|
|
Loading…
Reference in New Issue