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
|
2020-04-16 11:28:14 +08:00
|
|
|
|
#pragma warning disable CS0436
|
2019-05-19 23:14:47 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
using System.Runtime.ExceptionServices;
|
2019-05-19 23:14:47 +08:00
|
|
|
|
using UniRx.Async.CompilerServices;
|
|
|
|
|
using UniRx.Async.Internal;
|
|
|
|
|
|
|
|
|
|
namespace UniRx.Async
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
internal static class AwaiterActions
|
|
|
|
|
{
|
|
|
|
|
internal static readonly Action<object> InvokeActionDelegate = InvokeAction;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
static void InvokeAction(object state)
|
|
|
|
|
{
|
|
|
|
|
((Action)state).Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lightweight unity specified task-like object.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder))]
|
|
|
|
|
public readonly partial struct UniTask
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
readonly IUniTaskSource source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
readonly short token;
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask(IUniTaskSource source, short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
this.source = source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
this.token = token;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus Status
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (source == null) return UniTaskStatus.Succeeded;
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return source.GetStatus(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public Awaiter GetAwaiter()
|
|
|
|
|
{
|
|
|
|
|
return new Awaiter(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns (bool IsCanceled) instead of throws OperationCanceledException.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask<bool> SuppressCancellationThrow()
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
var status = Status;
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (status == UniTaskStatus.Succeeded) return CompletedTasks.False;
|
|
|
|
|
if (status == UniTaskStatus.Canceled) return CompletedTasks.True;
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask<bool>(new IsCanceledSource(source), token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
if (source == null) return "()";
|
|
|
|
|
return "(" + source.UnsafeGetStatus() + ")";
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-19 01:55:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Memoizing inner IValueTaskSource. The result UniTask can await multiple.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask Preserve()
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask(new MemoizeSource(source), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTask<AsyncUnit> AsAsyncUnitUniTask()
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (this.source == null) return CompletedTasks.AsyncUnit;
|
2020-04-18 21:11:40 +08:00
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
var status = this.source.GetStatus(this.token);
|
2020-04-18 21:11:40 +08:00
|
|
|
|
if (status.IsCompletedSuccessfully())
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return CompletedTasks.AsyncUnit;
|
2020-04-18 21:11:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
return new UniTask<AsyncUnit>(new AsyncUnitSource(this.source), this.token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
sealed class AsyncUnitSource : IUniTaskSource<AsyncUnit>
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
readonly IUniTaskSource source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public AsyncUnitSource(IUniTaskSource source)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
this.source = source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public AsyncUnit GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
source.GetResult(token);
|
|
|
|
|
return AsyncUnit.Default;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-18 21:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
return source.GetStatus(token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
source.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-18 21:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
return source.UnsafeGetStatus();
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
sealed class IsCanceledSource : IUniTaskSource<bool>
|
2020-04-18 21:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
readonly IUniTaskSource source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public IsCanceledSource(IUniTaskSource source)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
public bool GetResult(short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (source.GetStatus(token) == UniTaskStatus.Canceled)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return true;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
source.GetResult(token);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return source.GetStatus(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return source.UnsafeGetStatus();
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
source.OnCompleted(continuation, state, token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
sealed class MemoizeSource : IUniTaskSource
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
IUniTaskSource source;
|
|
|
|
|
ExceptionDispatchInfo exception;
|
2020-04-20 07:35:06 +08:00
|
|
|
|
UniTaskStatus status;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
|
|
|
|
public MemoizeSource(IUniTaskSource source)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
if (exception != null)
|
|
|
|
|
{
|
|
|
|
|
exception.Throw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
source.GetResult(token);
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Succeeded;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exception = ExceptionDispatchInfo.Capture(ex);
|
|
|
|
|
if (ex is OperationCanceledException)
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Canceled;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Faulted;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
source = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
continuation(state);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
source.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source.UnsafeGetStatus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 04:07:59 +08:00
|
|
|
|
public readonly struct Awaiter : ICriticalNotifyCompletion
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
readonly UniTask task;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public Awaiter(in UniTask task)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
this.task = task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsCompleted
|
|
|
|
|
{
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return task.Status.IsCompleted();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void GetResult()
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
if (task.source == null) return;
|
|
|
|
|
task.source.GetResult(task.token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
if (task.source == null)
|
|
|
|
|
{
|
|
|
|
|
continuation();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
task.source.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
if (task.source == null)
|
|
|
|
|
{
|
|
|
|
|
continuation();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
task.source.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If register manually continuation, you can use it instead of for compiler OnCompleted methods.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SourceOnCompleted(Action<object> continuation, object state)
|
|
|
|
|
{
|
|
|
|
|
if (task.source == null)
|
|
|
|
|
{
|
|
|
|
|
continuation(state);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
task.source.OnCompleted(continuation, state, task.token);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lightweight unity specified task-like object.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
[AsyncMethodBuilder(typeof(AsyncUniTaskMethodBuilder<>))]
|
|
|
|
|
public readonly struct UniTask<T>
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
readonly IUniTaskSource<T> source;
|
|
|
|
|
readonly T result;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
readonly short token;
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask(T result)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
this.source = default;
|
|
|
|
|
this.token = default;
|
|
|
|
|
this.result = result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask(IUniTaskSource<T> source, short token)
|
2020-04-18 21:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
this.token = token;
|
2020-04-18 21:11:40 +08:00
|
|
|
|
this.result = default;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus Status
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
return (source == null) ? UniTaskStatus.Succeeded : source.GetStatus(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public Awaiter GetAwaiter()
|
|
|
|
|
{
|
|
|
|
|
return new Awaiter(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 01:55:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Memoizing inner IValueTaskSource. The result UniTask can await multiple.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask<T> Preserve()
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask<T>(new MemoizeSource(source), token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTask AsUniTask()
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (this.source == null) return UniTask.CompletedTask;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
var status = this.source.GetStatus(this.token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
if (status.IsCompletedSuccessfully())
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return UniTask.CompletedTask;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
return new UniTask(this.source, this.token);
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// returns (bool IsCanceled, T Result) instead of throws OperationCanceledException.
|
|
|
|
|
/// </summary>
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public UniTask<(bool IsCanceled, T Result)> SuppressCancellationThrow()
|
2020-04-18 21:11:40 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask<(bool IsCanceled, T Result)>((false, result));
|
2020-04-18 21:11:40 +08:00
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-19 02:24:53 +08:00
|
|
|
|
return new UniTask<(bool, T)>(new IsCanceledSource(source), token);
|
2020-04-18 21:11:40 +08:00
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return (this.source == null) ? result?.ToString()
|
|
|
|
|
: "(" + this.source.UnsafeGetStatus() + ")";
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
sealed class IsCanceledSource : IUniTaskSource<(bool, T)>
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
readonly IUniTaskSource<T> source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public IsCanceledSource(IUniTaskSource<T> source)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
this.source = source;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public (bool, T) GetResult(short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
if (source.GetStatus(token) == UniTaskStatus.Canceled)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return (true, default);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
var result = source.GetResult(token);
|
|
|
|
|
return (false, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return source.GetStatus(token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
return source.UnsafeGetStatus();
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 21:11:40 +08:00
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-18 04:07:59 +08:00
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
source.OnCompleted(continuation, state, token);
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
sealed class MemoizeSource : IUniTaskSource<T>
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
IUniTaskSource<T> source;
|
|
|
|
|
T result;
|
|
|
|
|
ExceptionDispatchInfo exception;
|
2020-04-20 07:35:06 +08:00
|
|
|
|
UniTaskStatus status;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
|
|
|
|
public MemoizeSource(IUniTaskSource<T> source)
|
|
|
|
|
{
|
|
|
|
|
this.source = source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
if (exception != null)
|
|
|
|
|
{
|
|
|
|
|
exception.Throw();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result = source.GetResult(token);
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Succeeded;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
exception = ExceptionDispatchInfo.Capture(ex);
|
|
|
|
|
if (ex is OperationCanceledException)
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Canceled;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-20 07:35:06 +08:00
|
|
|
|
status = UniTaskStatus.Faulted;
|
2020-04-19 01:55:44 +08:00
|
|
|
|
}
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
source = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IUniTaskSource.GetResult(short token)
|
|
|
|
|
{
|
|
|
|
|
GetResult(token);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus GetStatus(short token)
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source.GetStatus(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
continuation(state);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
source.OnCompleted(continuation, state, token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 07:35:06 +08:00
|
|
|
|
public UniTaskStatus UnsafeGetStatus()
|
2020-04-19 01:55:44 +08:00
|
|
|
|
{
|
|
|
|
|
if (source == null)
|
|
|
|
|
{
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return source.UnsafeGetStatus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-18 04:07:59 +08:00
|
|
|
|
public readonly struct Awaiter : ICriticalNotifyCompletion
|
|
|
|
|
{
|
2020-04-19 02:24:53 +08:00
|
|
|
|
readonly UniTask<T> task;
|
2020-04-18 04:07:59 +08:00
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2020-04-19 02:24:53 +08:00
|
|
|
|
public Awaiter(in UniTask<T> task)
|
2020-04-18 04:07:59 +08:00
|
|
|
|
{
|
|
|
|
|
this.task = task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsCompleted
|
|
|
|
|
{
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return task.Status.IsCompleted();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public T GetResult()
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
var s = task.source;
|
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
|
|
|
|
return task.result;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return s.GetResult(task.token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void OnCompleted(Action continuation)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
var s = task.source;
|
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
|
|
|
|
continuation();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DebuggerHidden]
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void UnsafeOnCompleted(Action continuation)
|
|
|
|
|
{
|
2020-04-18 21:11:40 +08:00
|
|
|
|
var s = task.source;
|
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
|
|
|
|
continuation();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s.OnCompleted(AwaiterActions.InvokeActionDelegate, continuation, task.token);
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
2020-04-19 01:55:44 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If register manually continuation, you can use it instead of for compiler OnCompleted methods.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SourceOnCompleted(Action<object> continuation, object state)
|
|
|
|
|
{
|
|
|
|
|
var s = task.source;
|
|
|
|
|
if (s == null)
|
|
|
|
|
{
|
|
|
|
|
continuation(state);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s.OnCompleted(continuation, state, task.token);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-18 04:07:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-19 23:14:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-19 01:55:44 +08:00
|
|
|
|
#endif
|