UniTask/Assets/UniRx.Async/Triggers/AsyncTriggerBase.cs

385 lines
10 KiB
C#
Raw Normal View History

2019-05-19 23:14:47 +08:00
#if CSHARP_7_OR_LATER || (UNITY_2018_3_OR_NEWER && (NET_STANDARD_2_0 || NET_4_6))
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Collections.Generic;
using System.Threading;
using UniRx.Async.Internal;
using UnityEngine;
namespace UniRx.Async.Triggers
{
public abstract class AsyncTriggerBase : MonoBehaviour
2019-05-19 23:14:47 +08:00
{
bool calledAwake;
bool calledDestroy;
ICancelPromise triggerSlot;
2019-05-19 23:14:47 +08:00
void Awake()
{
calledAwake = true;
}
2019-05-19 23:14:47 +08:00
protected TriggerEvent<T> SetTriggerEvent<T>(ref TriggerEvent<T> field)
{
if (field == null)
{
field = new TriggerEvent<T>();
if (triggerSlot == null)
{
triggerSlot = field;
}
else
{
throw new InvalidOperationException("triggerSlot is already filled.");
}
}
2019-05-19 23:14:47 +08:00
if (!calledAwake)
{
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, new AwakeMonitor(this));
}
return field;
}
void OnDestroy()
2019-05-19 23:14:47 +08:00
{
if (calledDestroy) return;
calledDestroy = true;
triggerSlot?.TrySetCanceled();
triggerSlot = null;
2019-05-19 23:14:47 +08:00
}
class AwakeMonitor : IPlayerLoopItem
2019-05-19 23:14:47 +08:00
{
readonly AsyncTriggerBase trigger;
public AwakeMonitor(AsyncTriggerBase trigger)
{
this.trigger = trigger;
}
public bool MoveNext()
{
if (trigger.calledAwake) return false;
if (trigger == null)
{
trigger.OnDestroy();
return false;
}
return true;
}
2019-05-19 23:14:47 +08:00
}
}
public interface IAsyncOneShotTrigger
{
UniTask OneShotAsync();
}
2019-05-19 23:14:47 +08:00
public partial class AsyncTriggerHandler<T> : IAsyncOneShotTrigger
{
UniTask IAsyncOneShotTrigger.OneShotAsync()
2019-05-19 23:14:47 +08:00
{
core.Reset();
return new UniTask((IUniTaskSource)this, core.Version);
2019-05-19 23:14:47 +08:00
}
}
public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, IResolvePromise<T>, ICancelPromise, IDisposable
2019-05-19 23:14:47 +08:00
{
static Action<object> cancellationCallback = CancellationCallback;
readonly TriggerEvent<T> trigger;
2019-05-19 23:14:47 +08:00
CancellationToken cancellationToken;
CancellationTokenRegistration registration;
bool isDisposed;
bool callOnce;
2019-05-19 23:14:47 +08:00
UniTaskCompletionSourceCore<T> core;
2019-05-19 23:14:47 +08:00
internal CancellationToken CancellationToken => cancellationToken;
public AsyncTriggerHandler(TriggerEvent<T> trigger, bool callOnce)
2019-05-19 23:14:47 +08:00
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
this.trigger = trigger;
this.cancellationToken = default;
this.registration = default;
this.callOnce = callOnce;
trigger.Add(this);
TaskTracker.TrackActiveTask(this, 3);
2019-05-19 23:14:47 +08:00
}
public AsyncTriggerHandler(TriggerEvent<T> trigger, CancellationToken cancellationToken, bool callOnce)
2019-05-19 23:14:47 +08:00
{
if (cancellationToken.IsCancellationRequested)
{
isDisposed = true;
return;
}
this.trigger = trigger;
this.cancellationToken = cancellationToken;
this.callOnce = callOnce;
trigger.Add(this);
if (cancellationToken.CanBeCanceled)
{
registration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
}
TaskTracker.TrackActiveTask(this, 3);
2019-05-19 23:14:47 +08:00
}
static void CancellationCallback(object state)
2019-05-19 23:14:47 +08:00
{
var self = (AsyncTriggerHandler<T>)state;
self.Dispose();
self.core.TrySetCanceled(self.cancellationToken);
2019-05-19 23:14:47 +08:00
}
public void Dispose()
2019-05-19 23:14:47 +08:00
{
if (!isDisposed)
{
isDisposed = true;
TaskTracker.RemoveTracking(this);
registration.Dispose();
trigger.Remove(this);
}
2019-05-19 23:14:47 +08:00
}
T IUniTaskSource<T>.GetResult(short token)
2019-05-19 23:14:47 +08:00
{
try
2019-05-19 23:14:47 +08:00
{
return core.GetResult(token);
}
finally
{
if (callOnce)
2019-05-19 23:14:47 +08:00
{
Dispose();
2019-05-19 23:14:47 +08:00
}
}
}
bool IResolvePromise<T>.TrySetResult(T result)
2019-05-19 23:14:47 +08:00
{
return core.TrySetResult(result);
}
2019-05-19 23:14:47 +08:00
bool ICancelPromise.TrySetCanceled(CancellationToken cancellationToken)
{
return core.TrySetCanceled(cancellationToken);
}
void IUniTaskSource.GetResult(short token)
{
((IUniTaskSource<T>)this).GetResult(token);
}
UniTaskStatus IUniTaskSource.GetStatus(short token)
{
return core.GetStatus(token);
}
UniTaskStatus IUniTaskSource.UnsafeGetStatus()
{
return core.UnsafeGetStatus();
}
void IUniTaskSource.OnCompleted(Action<object> continuation, object state, short token)
{
core.OnCompleted(continuation, state, token);
}
}
public sealed class TriggerEvent<T> : IResolvePromise<T>, ICancelPromise
{
// optimize: many cases, handler is single.
AsyncTriggerHandler<T> singleHandler;
List<AsyncTriggerHandler<T>> handlers;
public bool TrySetResult(T value)
{
List<Exception> exceptions = null;
if (singleHandler != null)
2019-05-19 23:14:47 +08:00
{
try
2019-05-19 23:14:47 +08:00
{
((IResolvePromise<T>)singleHandler).TrySetResult(value);
}
catch (Exception ex)
{
if (handlers == null)
2019-05-19 23:14:47 +08:00
{
throw;
}
else
{
exceptions = new List<Exception>();
exceptions.Add(ex);
2019-05-19 23:14:47 +08:00
}
}
}
if (handlers != null)
2019-05-19 23:14:47 +08:00
{
// make snapshot
var rentArray = ArrayPoolUtil.CopyToRentArray(handlers);
var clearArray = true;
try
{
var array = rentArray.Array;
var len = rentArray.Length;
for (int i = 0; i < len; i++)
{
try
{
((IResolvePromise<T>)array[i]).TrySetResult(value);
}
catch (Exception ex)
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions.Add(ex);
}
finally
{
array[i] = null;
}
}
}
finally
{
rentArray.DisposeManually(clearArray);
}
2019-05-19 23:14:47 +08:00
}
if (exceptions != null)
2019-05-19 23:14:47 +08:00
{
throw new AggregateException(exceptions);
2019-05-19 23:14:47 +08:00
}
return true;
2019-05-19 23:14:47 +08:00
}
public bool TrySetCanceled(CancellationToken cancellationToken)
2019-05-19 23:14:47 +08:00
{
List<Exception> exceptions = null;
2019-05-19 23:14:47 +08:00
if (singleHandler != null)
{
try
{
((ICancelPromise)singleHandler).TrySetCanceled(cancellationToken);
}
catch (Exception ex)
{
if (handlers == null)
{
throw;
}
else
{
exceptions = new List<Exception>();
exceptions.Add(ex);
}
}
}
2019-05-19 23:14:47 +08:00
if (handlers != null)
2019-05-19 23:14:47 +08:00
{
// make snapshot
var rentArray = ArrayPoolUtil.CopyToRentArray(handlers);
var clearArray = true;
try
{
var array = rentArray.Array;
var len = rentArray.Length;
for (int i = 0; i < len; i++)
{
try
{
((ICancelPromise)array[i]).TrySetCanceled(cancellationToken);
}
catch (Exception ex)
{
if (exceptions == null)
{
exceptions = new List<Exception>();
}
exceptions.Add(ex);
}
finally
{
array[i] = null;
}
}
}
finally
{
rentArray.DisposeManually(clearArray);
}
2019-05-19 23:14:47 +08:00
}
if (exceptions != null)
2019-05-19 23:14:47 +08:00
{
throw new AggregateException(exceptions);
2019-05-19 23:14:47 +08:00
}
return true;
2019-05-19 23:14:47 +08:00
}
public void Add(AsyncTriggerHandler<T> handler)
2019-05-19 23:14:47 +08:00
{
if (singleHandler == null)
2019-05-19 23:14:47 +08:00
{
singleHandler = handler;
2019-05-19 23:14:47 +08:00
}
else
2019-05-19 23:14:47 +08:00
{
if (handlers != null)
2019-05-19 23:14:47 +08:00
{
handlers = new List<AsyncTriggerHandler<T>>();
handlers.Add(handler);
2019-05-19 23:14:47 +08:00
}
}
}
public void Remove(AsyncTriggerHandler<T> handler)
2019-05-19 23:14:47 +08:00
{
if (singleHandler == handler)
2019-05-19 23:14:47 +08:00
{
singleHandler = null;
2019-05-19 23:14:47 +08:00
}
else
2019-05-19 23:14:47 +08:00
{
if (handlers != null)
2019-05-19 23:14:47 +08:00
{
handlers.Remove(handler);
2019-05-19 23:14:47 +08:00
}
}
}
}
}
#endif