Skip to content

Commit

Permalink
Add UnityEvent<T> and InputField AsyncEventHandler and extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed May 20, 2020
1 parent 962c215 commit 3b593f3
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +26,21 @@ public static IUniTaskAsyncEnumerable<AsyncUnit> OnInvokeAsAsyncEnumerable(this
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);
Expand Down Expand Up @@ -207,6 +221,36 @@ public static IUniTaskAsyncEnumerable<string> OnEndEditAsAsyncEnumerable(this In
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);
Expand Down
140 changes: 121 additions & 19 deletions src/UniTask/Assets/Scenes/SandboxMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(



}

}




Expand Down Expand Up @@ -87,7 +112,7 @@ public class SandboxMain : MonoBehaviour
{
public Button okButton;
public Button cancelButton;
public Text text;


CancellationTokenSource cts;

Expand All @@ -110,6 +135,63 @@ async UniTask<int> FooAsync()
}





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");
Expand All @@ -126,10 +208,6 @@ async UniTask RunJobAsync()

var scheduled = job.Schedule();





UnityEngine.Debug.Log("OK");
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
UnityEngine.Debug.Log("OK2");
Expand Down Expand Up @@ -170,40 +248,64 @@ await UniTaskAsyncEnumerable
Debug.Log("Done");
}

void Start()
public int MyProperty { get; set; }

public class MyClass
{
//var rp = new AsyncReactiveProperty<int>(10);
public int MyProperty { get; set; }
}

//Running(rp).Forget();
MyClass mcc;

//await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i);
void Start()
{
this.mcc = new MyClass();
this.MyProperty = 999;

//rp.Dispose();
CheckDest().Forget();

//var channel = Channel.CreateSingleConsumerUnbounded<int>();
//Debug.Log("wait channel");
//await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { });

//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");
Expand Down

0 comments on commit 3b593f3

Please sign in to comment.