more working

master
neuecc 2020-04-26 02:38:16 +09:00
parent 3654a9e2f9
commit f28743f7f6
16 changed files with 710 additions and 334 deletions

View File

@ -50,7 +50,7 @@ public class SandboxMain : MonoBehaviour
{
text.text = "";
ucs.SetResult();
ucs.TrySetResult();
await ucs.Task;
});

View File

@ -24,7 +24,7 @@ namespace UniRx.Async
static void Callback(object state)
{
var promise = (UniTaskCompletionSource)state;
promise.SetResult();
promise.TrySetResult();
}
public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback)

View File

@ -60,7 +60,7 @@ namespace UniRx.Async.CompilerServices
if (promise != null)
{
promise.SetException(exception);
promise.TrySetException(exception);
}
else
{
@ -81,7 +81,7 @@ namespace UniRx.Async.CompilerServices
if (promise != null)
{
promise.SetResult();
promise.TrySetResult();
}
}
@ -192,7 +192,7 @@ namespace UniRx.Async.CompilerServices
}
else
{
promise.SetException(exception);
promise.TrySetException(exception);
}
}
@ -213,7 +213,7 @@ namespace UniRx.Async.CompilerServices
return;
}
promise.SetResult(result);
promise.TrySetResult(result);
}
// 5. AwaitOnCompleted

View File

@ -6,6 +6,11 @@ namespace UniRx.Async.Internal
{
internal static class StateTuple
{
public static StateTuple<T1> Create<T1>(T1 item1)
{
return StatePool<T1>.Create(item1);
}
public static StateTuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
{
return StatePool<T1, T2>.Create(item1, item2);
@ -17,6 +22,45 @@ namespace UniRx.Async.Internal
}
}
internal class StateTuple<T1> : IDisposable
{
public T1 Item1;
public void Deconstruct(out T1 item1)
{
item1 = this.Item1;
}
public void Dispose()
{
StatePool<T1>.Return(this);
}
}
internal static class StatePool<T1>
{
static readonly ConcurrentQueue<StateTuple<T1>> queue = new ConcurrentQueue<StateTuple<T1>>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static StateTuple<T1> Create(T1 item1)
{
if (queue.TryDequeue(out var value))
{
value.Item1 = item1;
return value;
}
return new StateTuple<T1> { Item1 = item1 };
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Return(StateTuple<T1> tuple)
{
tuple.Item1 = default;
queue.Enqueue(tuple);
}
}
internal class StateTuple<T1, T2> : IDisposable
{
public T1 Item1;

View File

@ -12,7 +12,7 @@ namespace UniRx.Async
static readonly UniTask CanceledUniTask = new Func<UniTask>(() =>
{
var promise = new UniTaskCompletionSource();
promise.SetCanceled(CancellationToken.None);
promise.TrySetCanceled(CancellationToken.None);
promise.MarkHandled();
return promise.Task;
})();
@ -24,7 +24,7 @@ namespace UniRx.Async
static CanceledUniTaskCache()
{
var promise = new UniTaskCompletionSource<T>();
promise.SetCanceled(CancellationToken.None);
promise.TrySetCanceled(CancellationToken.None);
promise.MarkHandled();
Task = promise.Task;
}
@ -35,7 +35,7 @@ namespace UniRx.Async
public static UniTask FromException(Exception ex)
{
var promise = new UniTaskCompletionSource();
promise.SetException(ex);
promise.TrySetException(ex);
promise.MarkHandled();
return promise.Task;
}
@ -43,7 +43,7 @@ namespace UniRx.Async
public static UniTask<T> FromException<T>(Exception ex)
{
var promise = new UniTaskCompletionSource<T>();
promise.SetException(ex);
promise.TrySetException(ex);
promise.MarkHandled();
return promise.Task;
}
@ -62,7 +62,7 @@ namespace UniRx.Async
else
{
var promise = new UniTaskCompletionSource();
promise.SetCanceled(cancellationToken);
promise.TrySetCanceled(cancellationToken);
promise.MarkHandled();
return promise.Task;
}
@ -77,7 +77,7 @@ namespace UniRx.Async
else
{
var promise = new UniTaskCompletionSource<T>();
promise.SetCanceled(cancellationToken);
promise.TrySetCanceled(cancellationToken);
promise.MarkHandled();
return promise.Task;
}

File diff suppressed because it is too large Load Diff

View File

@ -21,9 +21,9 @@ namespace UniRx.Async
var args = string.Join(", ", range.Select(x => $"UniTask<T{x}> task{x}"));
var targs = string.Join(", ", range.Select(x => $"task{x}"));
var tresult = string.Join(", ", range.Select(x => $"task{x}.GetAwaiter().GetResult()"));
var tBool = string.Join(", ", range.Select(x => $"(bool hasResult, T{x} result{x})"));
var tBool = string.Join(", ", range.Select(x => $"T{x} result{x}"));
var tfield = string.Join(", ", range.Select(x => $"self.t{x}"));
Func<int, string> getResult = j => string.Join(", ", range.Select(x => (x == j) ? "(true, result)" : "(false, default)"));
Func<int, string> getResult = j => string.Join(", ", range.Select(x => (x == j) ? "result" : "default"));
#>
public static UniTask<(int winArgumentIndex, <#= tBool #>)> WhenAny<<#= t #>>(<#= args #>)
{

View File

@ -403,6 +403,7 @@ namespace UniRx.Async
return UniTask.CompletedTask;
}
// Converting UniTask<T> -> UniTask is zero overhead.
return new UniTask(this.source, this.token);
}

View File

@ -13,22 +13,22 @@ namespace UniRx.Async
{
public interface IResolvePromise
{
void SetResult();
bool TrySetResult();
}
public interface IResolvePromise<T>
{
void SetResult(T value);
bool TrySetResult(T value);
}
public interface IRejectPromise
{
void SetException(Exception exception);
bool TrySetException(Exception exception);
}
public interface ICancelPromise
{
void SetCanceled(CancellationToken cancellationToken = default);
bool TrySetCanceled(CancellationToken cancellationToken = default);
}
public interface IPromise<T> : IResolvePromise<T>, IRejectPromise, ICancelPromise
@ -301,19 +301,19 @@ namespace UniRx.Async
core.Reset();
}
public void SetResult()
public bool TrySetResult()
{
core.TrySetResult(AsyncUnit.Default);
return core.TrySetResult(AsyncUnit.Default);
}
public void SetCanceled(CancellationToken cancellationToken = default)
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
core.TrySetCanceled(cancellationToken);
return core.TrySetCanceled(cancellationToken);
}
public void SetException(Exception exception)
public bool TrySetException(Exception exception)
{
core.TrySetException(exception);
return core.TrySetException(exception);
}
public void GetResult(short token)
@ -364,7 +364,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource CreateFromCanceled(CancellationToken cancellationToken, out short token)
{
var source = Create();
source.SetCanceled(cancellationToken);
source.TrySetCanceled(cancellationToken);
token = source.core.Version;
return source;
}
@ -372,7 +372,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource CreateFromException(Exception exception, out short token)
{
var source = Create();
source.SetException(exception);
source.TrySetException(exception);
token = source.core.Version;
return source;
}
@ -380,7 +380,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource CreateCompleted(out short token)
{
var source = Create();
source.SetResult();
source.TrySetResult();
token = source.core.Version;
return source;
}
@ -393,19 +393,19 @@ namespace UniRx.Async
}
}
public void SetResult()
public bool TrySetResult()
{
core.TrySetResult(AsyncUnit.Default);
return core.TrySetResult(AsyncUnit.Default);
}
public void SetCanceled(CancellationToken cancellationToken = default)
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
core.TrySetCanceled(cancellationToken);
return core.TrySetCanceled(cancellationToken);
}
public void SetException(Exception exception)
public bool TrySetException(Exception exception)
{
core.TrySetException(exception);
return core.TrySetException(exception);
}
public void GetResult(short token)
@ -487,19 +487,19 @@ namespace UniRx.Async
TaskTracker2.TrackActiveTask(this, 2);
}
public void SetResult(T result)
public bool TrySetResult(T result)
{
core.TrySetResult(result);
return core.TrySetResult(result);
}
public void SetCanceled(CancellationToken cancellationToken = default)
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
core.TrySetCanceled(cancellationToken);
return core.TrySetCanceled(cancellationToken);
}
public void SetException(Exception exception)
public bool TrySetException(Exception exception)
{
core.TrySetException(exception);
return core.TrySetException(exception);
}
public T GetResult(short token)
@ -555,7 +555,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource<T> CreateFromCanceled(CancellationToken cancellationToken, out short token)
{
var source = Create();
source.SetCanceled(cancellationToken);
source.TrySetCanceled(cancellationToken);
token = source.core.Version;
return source;
}
@ -563,7 +563,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource<T> CreateFromException(Exception exception, out short token)
{
var source = Create();
source.SetException(exception);
source.TrySetException(exception);
token = source.core.Version;
return source;
}
@ -571,7 +571,7 @@ namespace UniRx.Async
public static AutoResetUniTaskCompletionSource<T> CreateFromResult(T result, out short token)
{
var source = Create();
source.SetResult(result);
source.TrySetResult(result);
token = source.core.Version;
return source;
}
@ -584,19 +584,19 @@ namespace UniRx.Async
}
}
public void SetResult(T result)
public bool TrySetResult(T result)
{
core.TrySetResult(result);
return core.TrySetResult(result);
}
public void SetCanceled(CancellationToken cancellationToken = default)
public bool TrySetCanceled(CancellationToken cancellationToken = default)
{
core.TrySetCanceled(cancellationToken);
return core.TrySetCanceled(cancellationToken);
}
public void SetException(Exception exception)
public bool TrySetException(Exception exception)
{
core.TrySetException(exception);
return core.TrySetException(exception);
}
public T GetResult(short token)

View File

@ -1,6 +1,4 @@
#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
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Collections.Generic;
namespace UniRx.Async
@ -9,45 +7,181 @@ namespace UniRx.Async
{
// shorthand of WhenAll
public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this IEnumerable<UniTask> tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask<T[]>.Awaiter GetAwaiter<T>(this UniTask<T>[] tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask<T[]>.Awaiter GetAwaiter<T>(this IEnumerable<UniTask<T>> tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2)>.Awaiter GetAwaiter<T1, T2>(this (UniTask<T1> task1, UniTask<T2> task2) tasks)
public static UniTask<(T1, T2)>.Awaiter GetAwaiter<T1, T2>(this (UniTask<T1> task1, UniTask<T2> task2) tasks)
{
return UniTask.WhenAll(tasks.task1, tasks.task2).GetAwaiter();
return UniTask.WhenAll(tasks.Item1, tasks.Item2).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2, T3 result3)> WhenAll<T1, T2, T3>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3) tasks)
public static UniTask<(T1, T2, T3)>.Awaiter GetAwaiter<T1, T2, T3>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3) tasks)
{
return UniTask.WhenAll<T1, T2, T3>(tasks.task1, tasks.task2, tasks.task3);
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2, T3 result3, T4 result4)> WhenAll<T1, T2, T3, T4>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4) tasks)
public static UniTask<(T1, T2, T3, T4)>.Awaiter GetAwaiter<T1, T2, T3, T4>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4) tasks)
{
return UniTask.WhenAll<T1, T2, T3, T4>(tasks.task1, tasks.task2, tasks.task3, tasks.task4);
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2, T3 result3, T4 result4, T5 result5)> WhenAll<T1, T2, T3, T4, T5>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5) tasks)
public static UniTask<(T1, T2, T3, T4, T5)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5) tasks)
{
return UniTask.WhenAll<T1, T2, T3, T4, T5>(tasks.task1, tasks.task2, tasks.task3, tasks.task4, tasks.task5);
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6)> WhenAll<T1, T2, T3, T4, T5, T6>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6) tasks)
public static UniTask<(T1, T2, T3, T4, T5, T6)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6) tasks)
{
return UniTask.WhenAll<T1, T2, T3, T4, T5, T6>(tasks.task1, tasks.task2, tasks.task3, tasks.task4, tasks.task5, tasks.task6);
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6).GetAwaiter();
}
public static UniTask<(T1 result1, T2 result2, T3 result3, T4 result4, T5 result5, T6 result6, T7 result7)> WhenAll<T1, T2, T3, T4, T5, T6, T7>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7) tasks)
public static UniTask<(T1, T2, T3, T4, T5, T6, T7)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7) tasks)
{
return UniTask.WhenAll<T1, T2, T3, T4, T5, T6, T7>(tasks.task1, tasks.task2, tasks.task3, tasks.task4, tasks.task5, tasks.task6, tasks.task7);
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10, UniTask<T11> task11) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10, UniTask<T11> task11, UniTask<T12> task12) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10, UniTask<T11> task11, UniTask<T12> task12, UniTask<T13> task13) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10, UniTask<T11> task11, UniTask<T12> task12, UniTask<T13> task13, UniTask<T14> task14) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14).GetAwaiter();
}
public static UniTask<(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15)>.Awaiter GetAwaiter<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(this (UniTask<T1> task1, UniTask<T2> task2, UniTask<T3> task3, UniTask<T4> task4, UniTask<T5> task5, UniTask<T6> task6, UniTask<T7> task7, UniTask<T8> task8, UniTask<T9> task9, UniTask<T10> task10, UniTask<T11> task11, UniTask<T12> task12, UniTask<T13> task13, UniTask<T14> task14, UniTask<T15> task15) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14, tasks.Item15).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this (UniTask task1, UniTask task2, UniTask task3, UniTask task4, UniTask task5, UniTask task6, UniTask task7, UniTask task8, UniTask task9, UniTask task10, UniTask task11, UniTask task12, UniTask task13, UniTask task14, UniTask task15) tasks)
{
return UniTask.WhenAll(tasks.Item1, tasks.Item2, tasks.Item3, tasks.Item4, tasks.Item5, tasks.Item6, tasks.Item7, tasks.Item8, tasks.Item9, tasks.Item10, tasks.Item11, tasks.Item12, tasks.Item13, tasks.Item14, tasks.Item15).GetAwaiter();
}
}
}
#endif

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 4b4ff020f73dc6d4b8ebd4760d61fb43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,63 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System.Collections.Generic;
namespace UniRx.Async
{
public static partial class UniTaskExtensions
{
// shorthand of WhenAll
public static UniTask.Awaiter GetAwaiter(this UniTask[] tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask.Awaiter GetAwaiter(this IEnumerable<UniTask> tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask<T[]>.Awaiter GetAwaiter<T>(this UniTask<T>[] tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
public static UniTask<T[]>.Awaiter GetAwaiter<T>(this IEnumerable<UniTask<T>> tasks)
{
return UniTask.WhenAll(tasks).GetAwaiter();
}
<# for(var i = 2; i <= 15; i++ ) {
var range = Enumerable.Range(1, i);
var t = string.Join(", ", range.Select(x => "T" + x));
var args = string.Join(", ", range.Select(x => $"UniTask<T{x}> task{x}"));
var titems = string.Join(", ", range.Select(x => $"tasks.Item{x}"));
#>
public static UniTask<(<#= t #>)>.Awaiter GetAwaiter<<#= t #>>(this (<#= args #>) tasks)
{
return UniTask.WhenAll(<#= titems #>).GetAwaiter();
}
<# } #>
<# for(var i = 2; i <= 15; i++ ) {
var range = Enumerable.Range(1, i);
var args = string.Join(", ", range.Select(x => $"UniTask task{x}"));
var titems = string.Join(", ", range.Select(x => $"tasks.Item{x}"));
#>
public static UniTask.Awaiter GetAwaiter(this (<#= args #>) tasks)
{
return UniTask.WhenAll(<#= titems #>).GetAwaiter();
}
<# } #>
}
}

View File

@ -26,13 +26,13 @@ namespace UniRx.Async
switch (x.Status)
{
case TaskStatus.Canceled:
p.SetCanceled();
p.TrySetCanceled();
break;
case TaskStatus.Faulted:
p.SetException(x.Exception);
p.TrySetException(x.Exception);
break;
case TaskStatus.RanToCompletion:
p.SetResult(x.Result);
p.TrySetResult(x.Result);
break;
default:
throw new NotSupportedException();
@ -56,13 +56,13 @@ namespace UniRx.Async
switch (x.Status)
{
case TaskStatus.Canceled:
p.SetCanceled();
p.TrySetCanceled();
break;
case TaskStatus.Faulted:
p.SetException(x.Exception);
p.TrySetException(x.Exception);
break;
case TaskStatus.RanToCompletion:
p.SetResult();
p.TrySetResult();
break;
default:
throw new NotSupportedException();
@ -190,23 +190,26 @@ namespace UniRx.Async
return new ToCoroutineEnumerator(task, exceptionHandler);
}
public static UniTask Timeout(this UniTask task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
public static async UniTask Timeout(this UniTask task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
return Timeout(task.AsAsyncUnitUniTask(), timeout, ignoreTimeScale, timeoutCheckTiming, taskCancellationTokenSource);
}
// TODO: require UniTask2.Delay, WhenAny, etc...
public static async UniTask<T> Timeout<T>(this UniTask<T> task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
// left, right both suppress operation canceled exception.
var delayCancellationTokenSource = new CancellationTokenSource();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow();
var (hasValue, value) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
int winArgIndex;
bool taskResultIsCanceled;
try
{
(winArgIndex, taskResultIsCanceled, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
}
catch
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
throw;
}
if (!hasValue)
// timeout
if (winArgIndex == 1)
{
if (taskCancellationTokenSource != null)
{
@ -222,37 +225,32 @@ namespace UniRx.Async
delayCancellationTokenSource.Dispose();
}
if (value.IsCanceled)
if (taskResultIsCanceled)
{
Error.ThrowOperationCanceledException();
}
return value.Result;
}
/// <summary>
/// Timeout with suppress OperationCanceledException. Returns (bool, IsCacneled).
/// </summary>
public static async UniTask<bool> TimeoutWithoutException(this UniTask task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
public static async UniTask<T> Timeout<T>(this UniTask<T> task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
var v = await TimeoutWithoutException(task.AsAsyncUnitUniTask(), timeout, ignoreTimeScale, timeoutCheckTiming, taskCancellationTokenSource);
return v.IsTimeout;
}
/// <summary>
/// Timeout with suppress OperationCanceledException. Returns (bool IsTimeout, T Result).
/// </summary>
public static async UniTask<(bool IsTimeout, T Result)> TimeoutWithoutException<T>(this UniTask<T> task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
// left, right both suppress operation canceled exception.
var delayCancellationTokenSource = new CancellationTokenSource();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming).SuppressCancellationThrow();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow();
var (hasValue, value) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
int winArgIndex;
(bool IsCanceled, T Result) taskResult;
try
{
(winArgIndex, taskResult, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
}
catch
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
throw;
}
if (!hasValue)
// timeout
if (winArgIndex == 1)
{
if (taskCancellationTokenSource != null)
{
@ -260,7 +258,7 @@ namespace UniRx.Async
taskCancellationTokenSource.Dispose();
}
return (true, default(T));
throw new TimeoutException("Exceed Timeout:" + timeout);
}
else
{
@ -268,24 +266,142 @@ namespace UniRx.Async
delayCancellationTokenSource.Dispose();
}
if (value.IsCanceled)
if (taskResult.IsCanceled)
{
Error.ThrowOperationCanceledException();
}
return (false, value.Result);
return taskResult.Result;
}
/// <summary>
/// Timeout with suppress OperationCanceledException. Returns (bool, IsCacneled).
/// </summary>
public static async UniTask<bool> TimeoutWithoutException(this UniTask task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
var delayCancellationTokenSource = new CancellationTokenSource();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow();
int winArgIndex;
bool taskResultIsCanceled;
try
{
(winArgIndex, taskResultIsCanceled, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
}
catch
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
return true;
}
// timeout
if (winArgIndex == 1)
{
if (taskCancellationTokenSource != null)
{
taskCancellationTokenSource.Cancel();
taskCancellationTokenSource.Dispose();
}
throw new TimeoutException("Exceed Timeout:" + timeout);
}
else
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
}
if (taskResultIsCanceled)
{
Error.ThrowOperationCanceledException();
}
return false;
}
/// <summary>
/// Timeout with suppress OperationCanceledException. Returns (bool IsTimeout, T Result).
/// </summary>
public static async UniTask<(bool IsTimeout, T Result)> TimeoutWithoutException<T>(this UniTask<T> task, TimeSpan timeout, bool ignoreTimeScale = true, PlayerLoopTiming timeoutCheckTiming = PlayerLoopTiming.Update, CancellationTokenSource taskCancellationTokenSource = null)
{
var delayCancellationTokenSource = new CancellationTokenSource();
var timeoutTask = UniTask.Delay(timeout, ignoreTimeScale, timeoutCheckTiming, delayCancellationTokenSource.Token).SuppressCancellationThrow();
int winArgIndex;
(bool IsCanceled, T Result) taskResult;
try
{
(winArgIndex, taskResult, _) = await UniTask.WhenAny(task.SuppressCancellationThrow(), timeoutTask);
}
catch
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
return (true, default);
}
// timeout
if (winArgIndex == 1)
{
if (taskCancellationTokenSource != null)
{
taskCancellationTokenSource.Cancel();
taskCancellationTokenSource.Dispose();
}
throw new TimeoutException("Exceed Timeout:" + timeout);
}
else
{
delayCancellationTokenSource.Cancel();
delayCancellationTokenSource.Dispose();
}
if (taskResult.IsCanceled)
{
Error.ThrowOperationCanceledException();
}
return (false, taskResult.Result);
}
public static void Forget(this UniTask task)
{
ForgetCore(task).Forget();
var awaiter = task.GetAwaiter();
if (awaiter.IsCompleted)
{
try
{
awaiter.GetResult();
}
catch (Exception ex)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
}
}
awaiter.SourceOnCompleted(state =>
{
using (var t = (StateTuple<UniTask.Awaiter>)state)
{
try
{
t.Item1.GetResult();
}
catch (Exception ex)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
}
}
}, StateTuple.Create(awaiter));
}
public static void Forget(this UniTask task, Action<Exception> exceptionHandler, bool handleExceptionOnMainThread = true)
{
if (exceptionHandler == null)
{
ForgetCore(task).Forget();
Forget(task);
}
else
{
@ -293,12 +409,6 @@ namespace UniRx.Async
}
}
// UniTask to UniTaskVoid
static async UniTaskVoid ForgetCore(UniTask task)
{
await task;
}
static async UniTaskVoid ForgetCoreWithCatch(UniTask task, Action<Exception> exceptionHandler, bool handleExceptionOnMainThread)
{
try
@ -324,14 +434,40 @@ namespace UniRx.Async
public static void Forget<T>(this UniTask<T> task)
{
ForgetCore(task).Forget();
var awaiter = task.GetAwaiter();
if (awaiter.IsCompleted)
{
try
{
awaiter.GetResult();
}
catch (Exception ex)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
}
}
awaiter.SourceOnCompleted(state =>
{
using (var t = (StateTuple<UniTask<T>.Awaiter>)state)
{
try
{
t.Item1.GetResult();
}
catch (Exception ex)
{
UniTaskScheduler.PublishUnobservedTaskException(ex);
}
}
}, StateTuple.Create(awaiter));
}
public static void Forget<T>(this UniTask<T> task, Action<Exception> exceptionHandler, bool handleExceptionOnMainThread = true)
{
if (exceptionHandler == null)
{
ForgetCore(task).Forget();
task.Forget();
}
else
{
@ -339,12 +475,6 @@ namespace UniRx.Async
}
}
// UniTask to UniTaskVoid
static async UniTaskVoid ForgetCore<T>(UniTask<T> task)
{
await task;
}
static async UniTaskVoid ForgetCoreWithCatch<T>(UniTask<T> task, Action<Exception> exceptionHandler, bool handleExceptionOnMainThread)
{
try

View File

@ -33,7 +33,7 @@ namespace UniRx.Async
public static IObservable<T> ToObservable<T>(this UniTask<T> task)
{
if (task.IsCompleted)
if (task.Status.IsCompleted())
{
try
{
@ -55,7 +55,7 @@ namespace UniRx.Async
/// </summary>
public static IObservable<AsyncUnit> ToObservable(this UniTask task)
{
if (task.IsCompleted)
if (task.Status.IsCompleted())
{
try
{

View File

@ -0,0 +1,14 @@
using System;
using System.Threading;
namespace UniRx.Async
{
public static partial class UnityAsyncExtensions
{
public static void StartAsyncCoroutine(this UnityEngine.MonoBehaviour monoBehaviour, Func<CancellationToken, UniTask> asyncCoroutine)
{
var token = monoBehaviour.GetCancellationTokenOnDestroy();
asyncCoroutine(token).Forget();
}
}
}

View File

@ -2,6 +2,7 @@
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using UniRx.Async.Internal;
using UnityEngine;
@ -183,11 +184,11 @@ namespace UniRx.Async
#endif
public struct AsyncOperationAwaiter : IAwaiter
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
{
AsyncOperation asyncOperation;
Action<AsyncOperation> continuationAction;
UniTaskStatus status;
// UniTaskStatus status;
public AsyncOperationAwaiter(AsyncOperation asyncOperation)
{
@ -196,13 +197,12 @@ namespace UniRx.Async
this.continuationAction = null;
}
public bool IsCompleted => status.IsCompleted();
public UniTaskStatus Status => status;
public bool IsCompleted => asyncOperation.isDone;
public void GetResult()
{
if (status == UniTaskStatus.Succeeded) return;
//*/ if (status == UniTaskStatus.Succeeded) return;
/*
if (status == UniTaskStatus.Pending)
{
// first timing of call
@ -215,6 +215,7 @@ namespace UniRx.Async
Error.ThrowNotYetCompleted();
}
}
*/
if (continuationAction != null)
{
@ -236,7 +237,7 @@ namespace UniRx.Async
public void UnsafeOnCompleted(Action continuation)
{
Error.ThrowWhenContinuationIsAlreadyRegistered(continuationAction);
continuationAction = continuation.AsFuncOfT<AsyncOperation>();
continuationAction = continuation.AsFuncOfT<AsyncOperation>(); // allocate delegate.
asyncOperation.completed += continuationAction;
}
}