redesigned asyncenumerable ugui and monobehaviourmessagetrigger

master
neuecc 2020-05-12 14:29:21 +09:00
parent d3538bdc8f
commit 354fd65d58
6 changed files with 810 additions and 917 deletions

View File

@ -55,6 +55,8 @@ namespace NetCoreSandbox
// AsyncEnumerable.Range(1, 10).GroupBy(x=>x).Select(x=>x.first
// AsyncEnumerable.Range(1,10).WithCancellation(CancellationToken.None).WithCancellation
//Enumerable.Range(1,10).ToHashSet(

View File

@ -1,16 +1,17 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks.Internal;
using Cysharp.Threading.Tasks.Linq;
using System;
using System.Threading;
using UnityEngine;
namespace Cysharp.Threading.Tasks.Triggers
{
public abstract class AsyncTriggerBase : MonoBehaviour
public abstract class AsyncTriggerBase<T> : MonoBehaviour, IUniTaskAsyncEnumerable<T>
{
protected TriggerEvent<T> triggerEvent;
bool calledAwake;
bool calledDestroy;
ICancelPromise triggerSlot;
@ -20,14 +21,14 @@ namespace Cysharp.Threading.Tasks.Triggers
calledAwake = true;
}
protected TriggerEvent<T> SetTriggerEvent<T>(ref TriggerEvent<T> field)
protected TriggerEvent<T> GetTriggerEvent()
{
if (field == null)
if (triggerEvent == null)
{
field = new TriggerEvent<T>();
triggerEvent = new TriggerEvent<T>();
if (triggerSlot == null)
{
triggerSlot = field;
triggerSlot = triggerEvent;
}
else
{
@ -40,7 +41,7 @@ namespace Cysharp.Threading.Tasks.Triggers
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}
return field;
return triggerEvent;
}
void OnDestroy()
@ -52,11 +53,87 @@ namespace Cysharp.Threading.Tasks.Triggers
triggerSlot = null;
}
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(GetTriggerEvent(), cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, IResolveCancelPromise<T>
{
static Action<object> cancellationCallback = CancellationCallback;
readonly TriggerEvent<T> triggerEvent;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool called;
bool isDisposed;
public Enumerator(TriggerEvent<T> triggerEvent, CancellationToken cancellationToken)
{
this.triggerEvent = triggerEvent;
this.cancellationToken = cancellationToken;
}
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
return completionSource.TrySetCanceled(cancellationToken);
}
public bool TrySetResult(T value)
{
Current = value;
return completionSource.TrySetResult(true);
}
static void CancellationCallback(object state)
{
var self = (Enumerator)state;
self.DisposeAsync().Forget(); // sync
self.completionSource.TrySetCanceled(self.cancellationToken);
}
public T Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
cancellationToken.ThrowIfCancellationRequested();
completionSource.Reset();
if (!called)
{
called = true;
TaskTracker.TrackActiveTask(this, 3);
triggerEvent.Add(this);
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
}
return new UniTask<bool>(this, completionSource.Version);
}
public UniTask DisposeAsync()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration.Dispose();
triggerEvent.Remove(this);
}
return default;
}
}
class AwakeMonitor : IPlayerLoopItem
{
readonly AsyncTriggerBase trigger;
readonly AsyncTriggerBase<T> trigger;
public AwakeMonitor(AsyncTriggerBase trigger)
public AwakeMonitor(AsyncTriggerBase<T> trigger)
{
this.trigger = trigger;
}
@ -208,90 +285,6 @@ namespace Cysharp.Threading.Tasks.Triggers
}
}
public sealed class TriggerAsyncEnumerable<T> : IUniTaskAsyncEnumerable<T>
{
readonly TriggerEvent<T> triggerEvent;
public TriggerAsyncEnumerable(TriggerEvent<T> triggerEvent)
{
this.triggerEvent = triggerEvent;
}
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
return new Enumerator(triggerEvent, cancellationToken);
}
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, IResolveCancelPromise<T>
{
static Action<object> cancellationCallback = CancellationCallback;
readonly TriggerEvent<T> triggerEvent;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool called;
bool isDisposed;
public Enumerator(TriggerEvent<T> triggerEvent, CancellationToken cancellationToken)
{
this.triggerEvent = triggerEvent;
this.cancellationToken = cancellationToken;
}
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
return completionSource.TrySetCanceled(cancellationToken);
}
public bool TrySetResult(T value)
{
Current = value;
return completionSource.TrySetResult(true);
}
static void CancellationCallback(object state)
{
var self = (Enumerator)state;
self.DisposeAsync().Forget(); // sync
self.completionSource.TrySetCanceled(self.cancellationToken);
}
public T Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
cancellationToken.ThrowIfCancellationRequested();
if (!called)
{
TaskTracker.TrackActiveTask(this, 3);
triggerEvent.Add(this);
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
}
completionSource.Reset();
return new UniTask<bool>(this, completionSource.Version);
}
public UniTask DisposeAsync()
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration.Dispose();
triggerEvent.Remove(this);
}
return default;
}
}
}
public sealed class TriggerEvent<T> : IResolveCancelPromise<T>
{
// optimize: many cases, handler is single.
@ -494,4 +487,3 @@ namespace Cysharp.Threading.Tasks.Triggers
}
}
}

View File

@ -99,7 +99,6 @@
Func<string, string> ToInterfaceName = x => $"IAsync{x}Handler";
Func<string, string> ToUniTaskName = x => x == "AsyncUnit" ? "UniTask" : $"UniTask<{x}>";
Func<string, string> ToCastUniTasSourceType = x => x == "AsyncUnit" ? "IUniTaskSource" : $"IUniTaskSource<{x}>";
Func<string, string> TriggerFieldName = x => char.ToLowerInvariant(x[0]) + x.Substring(1, x.Length - 1) + "TriggerEvent";
Func<string, string> OnInvokeSuffix = x => x == "AsyncUnit" ? ".AsUniTask()" : $"";
Func<(string argType, string argName)[], string> BuildMethodArgument = x => string.Join(", ", x.Select(y => y.argType + " " + y.argName));
Func<(string argType, string argName)[], string> BuildResultParameter = x => x.Length == 0 ? "AsyncUnit.Default" : "(" + string.Join(", ", x.Select(y => y.argName)) + ")";
@ -148,33 +147,31 @@ namespace Cysharp.Threading.Tasks.Triggers
}
[DisallowMultipleComponent]
public sealed class Async<#= t.triggerName #>Trigger : AsyncTriggerBase<#= (t.handlerInterface == null) ? "" : $", {t.handlerInterface}" #>
public sealed class Async<#= t.triggerName #>Trigger : AsyncTriggerBase<<#= t.returnType #>><#= (t.handlerInterface == null) ? "" : $", {t.handlerInterface}" #>
{
TriggerEvent<<#= t.returnType #>> <#= TriggerFieldName(t.methodName) #>;
void <#= (t.handlerInterface == null) ? "" : $"{t.handlerInterface}." #><#= t.methodName #>(<#= BuildMethodArgument(t.arguments) #>)
{
<#= TriggerFieldName(t.methodName) #>?.TrySetResult(<#= BuildResultParameter(t.arguments) #>);
triggerEvent?.TrySetResult(<#= BuildResultParameter(t.arguments) #>);
}
public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler()
{
return new AsyncTriggerHandler<<#= t.returnType #>>(SetTriggerEvent(ref <#= TriggerFieldName(t.methodName) #>), false);
return new AsyncTriggerHandler<<#= t.returnType #>>(GetTriggerEvent(), false);
}
public <#= ToInterfaceName(t.methodName) #> Get<#= t.methodName #>AsyncHandler(CancellationToken cancellationToken)
{
return new AsyncTriggerHandler<<#= t.returnType #>>(SetTriggerEvent(ref <#= TriggerFieldName(t.methodName) #>), cancellationToken, false);
return new AsyncTriggerHandler<<#= t.returnType #>>(GetTriggerEvent(), cancellationToken, false);
}
public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async()
{
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(SetTriggerEvent(ref <#= TriggerFieldName(t.methodName) #>), true)).<#= t.methodName #>Async();
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(GetTriggerEvent(), true)).<#= t.methodName #>Async();
}
public <#= ToUniTaskName(t.returnType) #> <#= t.methodName #>Async(CancellationToken cancellationToken)
{
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(SetTriggerEvent(ref <#= TriggerFieldName(t.methodName) #>), cancellationToken, true)).<#= t.methodName #>Async();
return ((<#= ToInterfaceName(t.methodName) #>)new AsyncTriggerHandler<<#= t.returnType #>>(GetTriggerEvent(), cancellationToken, true)).<#= t.methodName #>Async();
}
}
<# if(Is2019_3(t.triggerName)) { #>

View File

@ -3,6 +3,7 @@
using System;
using System.Threading;
using Cysharp.Threading.Tasks.Internal;
using Cysharp.Threading.Tasks.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
@ -21,6 +22,11 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler(unityEvent, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<AsyncUnit> 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);
@ -41,6 +47,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler(button.onClick, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<AsyncUnit> OnClickAsAsyncEnumerable(this Button button)
{
return new UnityEventHandlerAsyncEnumerable(button.onClick, button.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<AsyncUnit> OnClickAsAsyncEnumerable(this Button button, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable(button.onClick, cancellationToken);
}
public static IAsyncValueChangedEventHandler<bool> GetAsyncValueChangedEventHandler(this Toggle toggle)
{
return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy(), false);
@ -61,6 +77,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<bool>(toggle.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<bool> OnValueChangedAsAsyncEnumerable(this Toggle toggle)
{
return new UnityEventHandlerAsyncEnumerable<bool>(toggle.onValueChanged, toggle.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<bool> OnValueChangedAsAsyncEnumerable(this Toggle toggle, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<bool>(toggle.onValueChanged, cancellationToken);
}
public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Scrollbar scrollbar)
{
return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy(), false);
@ -81,6 +107,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<float>(scrollbar.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar)
{
return new UnityEventHandlerAsyncEnumerable<float>(scrollbar.onValueChanged, scrollbar.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Scrollbar scrollbar, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<float>(scrollbar.onValueChanged, cancellationToken);
}
public static IAsyncValueChangedEventHandler<Vector2> GetAsyncValueChangedEventHandler(this ScrollRect scrollRect)
{
return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy(), false);
@ -101,6 +137,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<Vector2>(scrollRect.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<Vector2> OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect)
{
return new UnityEventHandlerAsyncEnumerable<Vector2>(scrollRect.onValueChanged, scrollRect.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<Vector2> OnValueChangedAsAsyncEnumerable(this ScrollRect scrollRect, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<Vector2>(scrollRect.onValueChanged, cancellationToken);
}
public static IAsyncValueChangedEventHandler<float> GetAsyncValueChangedEventHandler(this Slider slider)
{
return new AsyncUnityEventHandler<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy(), false);
@ -121,6 +167,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<float>(slider.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Slider slider)
{
return new UnityEventHandlerAsyncEnumerable<float>(slider.onValueChanged, slider.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<float> OnValueChangedAsAsyncEnumerable(this Slider slider, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<float>(slider.onValueChanged, cancellationToken);
}
public static IAsyncEndEditEventHandler<string> GetAsyncEndEditEventHandler(this InputField inputField)
{
return new AsyncUnityEventHandler<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy(), false);
@ -141,6 +197,16 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<string>(inputField.onEndEdit, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<string> OnEndEditAsAsyncEnumerable(this InputField inputField)
{
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, inputField.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<string> OnEndEditAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
}
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
@ -151,36 +217,43 @@ namespace Cysharp.Threading.Tasks
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, false);
}
public static UniTask<int> OnValueChanged(this Dropdown dropdown)
public static UniTask<int> OnValueChangedAsync(this Dropdown dropdown)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
}
public static UniTask<int> OnValueChanged(this Dropdown dropdown, CancellationToken cancellationToken)
public static UniTask<int> OnValueChangedAsync(this Dropdown dropdown, CancellationToken cancellationToken)
{
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, cancellationToken, true).OnInvokeAsync();
}
public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this Dropdown dropdown)
{
return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy());
}
public static IUniTaskAsyncEnumerable<int> OnValueChangedAsAsyncEnumerable(this Dropdown dropdown, CancellationToken cancellationToken)
{
return new UnityEventHandlerAsyncEnumerable<int>(dropdown.onValueChanged, cancellationToken);
}
}
public interface IAsyncClickEventHandler : IDisposable, IUniTaskAsyncEnumerable<AsyncUnit>
public interface IAsyncClickEventHandler : IDisposable
{
UniTask OnClickAsync();
IAsyncClickEventHandler DisableAutoClose();
}
public interface IAsyncValueChangedEventHandler<T> : IDisposable, IUniTaskAsyncEnumerable<T>
public interface IAsyncValueChangedEventHandler<T> : IDisposable
{
UniTask<T> OnValueChangedAsync();
IAsyncValueChangedEventHandler<T> DisableAutoClose();
}
public interface IAsyncEndEditEventHandler<T> : IDisposable, IUniTaskAsyncEnumerable<T>
public interface IAsyncEndEditEventHandler<T> : IDisposable
{
UniTask<T> OnEndEditAsync();
IAsyncEndEditEventHandler<T> DisableAutoClose();
}
public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler, IUniTaskAsyncEnumerable<AsyncUnit>
public class AsyncUnityEventHandler : IUniTaskSource, IDisposable, IAsyncClickEventHandler
{
static Action<object> cancellationCallback = CancellationCallback;
@ -225,7 +298,6 @@ namespace Cysharp.Threading.Tasks
void Invoke()
{
asyncEnumerator?.SetResult();
core.TrySetResult(AsyncUnit.Default);
}
@ -233,15 +305,6 @@ namespace Cysharp.Threading.Tasks
{
var self = (AsyncUnityEventHandler)state;
self.Dispose();
// call child cancel
if (self.asyncEnumerator != null)
{
self.asyncEnumerator.CancelFromParent(self.cancellationToken);
self.asyncEnumerator = null;
}
self.core.TrySetCanceled(self.cancellationToken);
}
public void Dispose()
@ -255,6 +318,7 @@ namespace Cysharp.Threading.Tasks
{
unityEvent.RemoveListener(action);
}
core.TrySetCanceled(cancellationToken);
}
}
@ -292,118 +356,9 @@ namespace Cysharp.Threading.Tasks
{
core.OnCompleted(continuation, state, token);
}
// AsyncEnumerator
bool disableAutoClose;
Enumerator asyncEnumerator;
public AsyncUnityEventHandler DisableAutoClose()
{
disableAutoClose = true;
return this;
}
IAsyncClickEventHandler IAsyncClickEventHandler.DisableAutoClose()
{
disableAutoClose = true;
return this;
}
IUniTaskAsyncEnumerator<AsyncUnit> IUniTaskAsyncEnumerable<AsyncUnit>.GetAsyncEnumerator(CancellationToken cancellationToken)
{
if (this.asyncEnumerator != null)
{
throw new InvalidOperationException("Already acquired GetAsyncEnumerator, does not allow get twice before previous enumerator completed.");
}
this.asyncEnumerator = new Enumerator(this, cancellationToken);
return asyncEnumerator;
}
class Enumerator : Cysharp.Threading.Tasks.Linq.MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>
{
static Action<object> cancellationCallback = CancellationCallback;
AsyncUnityEventHandler parent;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool isDisposed;
public Enumerator(AsyncUnityEventHandler parent, CancellationToken cancellationToken)
{
this.parent = parent;
this.cancellationToken = cancellationToken;
if (cancellationToken.CanBeCanceled && parent.cancellationToken != cancellationToken)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
TaskTracker.TrackActiveTask(this, 3);
}
static void CancellationCallback(object state)
{
var self = (Enumerator)state;
self.DisposeCore(self.cancellationToken);
}
public void CancelFromParent(CancellationToken cancellationToken)
{
// call from parent, avoid parent close.
parent.disableAutoClose = true;
DisposeCore(cancellationToken);
}
public void SetResult()
{
completionSource.TrySetResult(true);
}
public AsyncUnit Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
completionSource.Reset();
return new UniTask<bool>(this, completionSource.Version);
}
public UniTask DisposeAsync()
{
DisposeCore(CancellationToken.None);
return default;
}
void DisposeCore(CancellationToken cancellationToken)
{
if (!isDisposed)
{
isDisposed = true;
registration.Dispose();
TaskTracker.RemoveTracking(this);
if (!parent.disableAutoClose)
{
parent.Dispose(); // dispose parent.
}
if (parent.asyncEnumerator == this)
{
parent.asyncEnumerator = null;
}
try
{
completionSource.TrySetCanceled(cancellationToken);
}
catch (OperationCanceledException) { }
}
}
}
}
public class AsyncUnityEventHandler<T> : IUniTaskSource<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>, IUniTaskAsyncEnumerable<T>
public class AsyncUnityEventHandler<T> : IUniTaskSource<T>, IDisposable, IAsyncValueChangedEventHandler<T>, IAsyncEndEditEventHandler<T>
{
static Action<object> cancellationCallback = CancellationCallback;
@ -448,7 +403,6 @@ namespace Cysharp.Threading.Tasks
void Invoke(T result)
{
asyncEnumerator?.SetResult(result);
core.TrySetResult(result);
}
@ -456,15 +410,6 @@ namespace Cysharp.Threading.Tasks
{
var self = (AsyncUnityEventHandler<T>)state;
self.Dispose();
// call child cancel
if (self.asyncEnumerator != null)
{
self.asyncEnumerator.CancelFromParent(self.cancellationToken);
self.asyncEnumerator = null;
}
self.core.TrySetCanceled(self.cancellationToken);
}
public void Dispose()
@ -479,12 +424,7 @@ namespace Cysharp.Threading.Tasks
unityEvent.RemoveListener(action);
}
asyncEnumerator?.DisposeAsync().Forget();
try
{
core.TrySetCanceled();
}
catch (OperationCanceledException) { }
core.TrySetCanceled();
}
}
@ -532,120 +472,213 @@ namespace Cysharp.Threading.Tasks
{
core.OnCompleted(continuation, state, token);
}
}
// AsyncEnumerator
public class UnityEventHandlerAsyncEnumerable : IUniTaskAsyncEnumerable<AsyncUnit>
{
readonly UnityEvent unityEvent;
readonly CancellationToken cancellationToken1;
bool disableAutoClose;
Enumerator asyncEnumerator;
public AsyncUnityEventHandler<T> DisableAutoClose()
public UnityEventHandlerAsyncEnumerable(UnityEvent unityEvent, CancellationToken cancellationToken)
{
disableAutoClose = true;
return this;
this.unityEvent = unityEvent;
this.cancellationToken1 = cancellationToken;
}
IAsyncValueChangedEventHandler<T> IAsyncValueChangedEventHandler<T>.DisableAutoClose()
public IUniTaskAsyncEnumerator<AsyncUnit> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
disableAutoClose = true;
return this;
}
IAsyncEndEditEventHandler<T> IAsyncEndEditEventHandler<T>.DisableAutoClose()
{
disableAutoClose = true;
return this;
}
IUniTaskAsyncEnumerator<T> IUniTaskAsyncEnumerable<T>.GetAsyncEnumerator(CancellationToken cancellationToken)
{
if (this.asyncEnumerator != null)
if (this.cancellationToken1 == cancellationToken)
{
throw new InvalidOperationException("Already acquired GetAsyncEnumerator, does not allow get twice before previous enumerator completed.");
return new Enumerator(unityEvent, this.cancellationToken1, CancellationToken.None);
}
else
{
return new Enumerator(unityEvent, this.cancellationToken1, cancellationToken);
}
this.asyncEnumerator = new Enumerator(this, cancellationToken);
return asyncEnumerator;
}
class Enumerator : Cysharp.Threading.Tasks.Linq.MoveNextSource, IUniTaskAsyncEnumerator<T>
class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<AsyncUnit>
{
static Action<object> cancellationCallback = CancellationCallback;
static readonly Action<object> cancel1 = OnCanceled1;
static readonly Action<object> cancel2 = OnCanceled2;
AsyncUnityEventHandler<T> parent;
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
readonly UnityEvent unityEvent;
CancellationToken cancellationToken1;
CancellationToken cancellationToken2;
UnityAction unityAction;
CancellationTokenRegistration registration1;
CancellationTokenRegistration registration2;
bool isDisposed;
public Enumerator(AsyncUnityEventHandler<T> parent, CancellationToken cancellationToken)
public Enumerator(UnityEvent unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
{
this.parent = parent;
this.cancellationToken = cancellationToken;
this.unityEvent = unityEvent;
this.cancellationToken1 = cancellationToken1;
this.cancellationToken2 = cancellationToken2;
}
if (cancellationToken.CanBeCanceled && parent.cancellationToken != cancellationToken)
public AsyncUnit Current => default;
public UniTask<bool> MoveNextAsync()
{
cancellationToken1.ThrowIfCancellationRequested();
cancellationToken2.ThrowIfCancellationRequested();
completionSource.Reset();
if (unityAction == null)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
unityAction = Invoke;
TaskTracker.TrackActiveTask(this, 3);
unityEvent.AddListener(unityAction);
if (cancellationToken1.CanBeCanceled)
{
registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this);
}
if (cancellationToken2.CanBeCanceled)
{
registration2 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel2, this);
}
}
TaskTracker.TrackActiveTask(this, 3);
return new UniTask<bool>(this, completionSource.Version);
}
static void CancellationCallback(object state)
void Invoke()
{
completionSource.TrySetResult(true);
}
static void OnCanceled1(object state)
{
var self = (Enumerator)state;
self.DisposeCore(self.cancellationToken);
self.DisposeAsync().Forget();
}
public void CancelFromParent(CancellationToken cancellationToken)
static void OnCanceled2(object state)
{
// call from parent, avoid parent close.
parent.disableAutoClose = true;
DisposeCore(cancellationToken);
var self = (Enumerator)state;
self.DisposeAsync().Forget();
}
public void SetResult(T result)
public UniTask DisposeAsync()
{
Current = result;
completionSource.TrySetResult(true);
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration1.Dispose();
registration2.Dispose();
unityEvent.RemoveListener(unityAction);
}
return default;
}
}
}
public class UnityEventHandlerAsyncEnumerable<T> : IUniTaskAsyncEnumerable<T>
{
readonly UnityEvent<T> unityEvent;
readonly CancellationToken cancellationToken1;
public UnityEventHandlerAsyncEnumerable(UnityEvent<T> unityEvent, CancellationToken cancellationToken)
{
this.unityEvent = unityEvent;
this.cancellationToken1 = cancellationToken;
}
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
{
if (this.cancellationToken1 == cancellationToken)
{
return new Enumerator(unityEvent, this.cancellationToken1, CancellationToken.None);
}
else
{
return new Enumerator(unityEvent, this.cancellationToken1, cancellationToken);
}
}
class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>
{
static readonly Action<object> cancel1 = OnCanceled1;
static readonly Action<object> cancel2 = OnCanceled2;
readonly UnityEvent<T> unityEvent;
CancellationToken cancellationToken1;
CancellationToken cancellationToken2;
UnityAction<T> unityAction;
CancellationTokenRegistration registration1;
CancellationTokenRegistration registration2;
bool isDisposed;
public Enumerator(UnityEvent<T> unityEvent, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
{
this.unityEvent = unityEvent;
this.cancellationToken1 = cancellationToken1;
this.cancellationToken2 = cancellationToken2;
}
public T Current { get; private set; }
public UniTask<bool> MoveNextAsync()
{
cancellationToken1.ThrowIfCancellationRequested();
cancellationToken2.ThrowIfCancellationRequested();
completionSource.Reset();
if (unityAction == null)
{
unityAction = Invoke;
TaskTracker.TrackActiveTask(this, 3);
unityEvent.AddListener(unityAction);
if (cancellationToken1.CanBeCanceled)
{
registration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel1, this);
}
if (cancellationToken2.CanBeCanceled)
{
registration2 = cancellationToken1.RegisterWithoutCaptureExecutionContext(cancel2, this);
}
}
return new UniTask<bool>(this, completionSource.Version);
}
void Invoke(T value)
{
Current = value;
completionSource.TrySetResult(true);
}
static void OnCanceled1(object state)
{
var self = (Enumerator)state;
self.DisposeAsync().Forget();
}
static void OnCanceled2(object state)
{
var self = (Enumerator)state;
self.DisposeAsync().Forget();
}
public UniTask DisposeAsync()
{
DisposeCore(CancellationToken.None);
return default;
}
void DisposeCore(CancellationToken cancellationToken)
{
if (!isDisposed)
{
isDisposed = true;
registration.Dispose();
TaskTracker.RemoveTracking(this);
if (!parent.disableAutoClose)
{
parent.Dispose(); // dispose parent.
}
if (parent.asyncEnumerator == this)
{
parent.asyncEnumerator = null;
}
try
{
completionSource.TrySetCanceled(cancellationToken);
}
catch (OperationCanceledException) { }
registration1.Dispose();
registration2.Dispose();
unityEvent.RemoveListener(unityAction);
}
return default;
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using Cysharp.Threading.Tasks.Linq;
using Cysharp.Threading.Tasks.Triggers;
using System.Collections;
@ -145,8 +146,23 @@ public class SandboxMain : MonoBehaviour
//StartCoroutine(cor);
await okButton.OnClickAsAsyncEnumerable().Where((x, i) => i % 2 == 0).ForEachAsync(_ =>
{
Debug.Log("Call");
});
//try
//{
// await this.GetAsyncUpdateTrigger().ForEachAsync(_ =>
// {
// UnityEngine.Debug.Log("EveryUpdate:" + Time.frameCount);
// });
//}
//catch (OperationCanceledException ex)
//{
// UnityEngine.Debug.Log("END");
//}
//this.TryGetComponent(
CancellationTokenSource cts = new CancellationTokenSource();
@ -167,18 +183,19 @@ public class SandboxMain : MonoBehaviour
// 5回クリックされるまで待つ、とか。
Debug.Log("Await start.");
//Debug.Log("Await start.");
await okButton.GetAsyncClickEventHandler().DisableAutoClose()
.Select((_, clickCount) => clickCount + 1)
.FirstAsync(x => x == 5);
//await okButton.GetAsyncClickEventHandler().DisableAutoClose()
// .Select((_, clickCount) => clickCount + 1)
// .FirstAsync(x => x == 5);
Debug.Log("Click 5 times.");
//Debug.Log("Click 5 times.");
// await this.GetAsyncUpdateTrigger().UpdateAsAsyncEnumerable()