mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-19 13:40:11 +00:00
Compare commits
17 Commits
2.0.7-rc4
...
2.0.10-rc7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eca5b1c096 | ||
|
|
c74ce14ad1 | ||
|
|
f59c56506f | ||
|
|
896eef1ee4 | ||
|
|
ec0123eec7 | ||
|
|
78f56b9b33 | ||
|
|
1d88ed85bc | ||
|
|
2b7986da19 | ||
|
|
c3d22968e1 | ||
|
|
0e25122ee2 | ||
|
|
4504d84aa8 | ||
|
|
2b87cadba3 | ||
|
|
21dc83c641 | ||
|
|
3b593f349c | ||
|
|
962c215e3b | ||
|
|
42dcfdbcdc | ||
|
|
6d7e6ec871 |
@@ -93,10 +93,16 @@ namespace NetCoreSandbox
|
|||||||
|
|
||||||
var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
||||||
|
|
||||||
|
// Observable.Range(1,10).CombineLatest(
|
||||||
|
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
var token = cts.Token;
|
||||||
|
|
||||||
|
FooAsync(token).ForEachAsync(x => { }, token);
|
||||||
|
|
||||||
|
|
||||||
|
// Observable.Range(1,10).CombineLatest(
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,67 @@ namespace NetCoreTests
|
|||||||
|
|
||||||
ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//[Fact]
|
||||||
|
//public async Task StateIteration()
|
||||||
|
//{
|
||||||
|
// var rp = new ReadOnlyAsyncReactiveProperty<int>(99);
|
||||||
|
// var setter = rp.GetSetter();
|
||||||
|
|
||||||
|
// var f = await rp.FirstAsync();
|
||||||
|
// f.Should().Be(99);
|
||||||
|
|
||||||
|
// var array = rp.Take(5).ToArrayAsync();
|
||||||
|
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(131);
|
||||||
|
|
||||||
|
// var ar = await array;
|
||||||
|
|
||||||
|
// ar.Should().BeEquivalentTo(new[] { 99, 100, 100, 100, 131 });
|
||||||
|
//}
|
||||||
|
|
||||||
|
//[Fact]
|
||||||
|
//public async Task StateWithoutCurrent()
|
||||||
|
//{
|
||||||
|
// var rp = new ReadOnlyAsyncReactiveProperty<int>(99);
|
||||||
|
// var setter = rp.GetSetter();
|
||||||
|
|
||||||
|
// var array = rp.WithoutCurrent().Take(5).ToArrayAsync();
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(100);
|
||||||
|
// setter(131);
|
||||||
|
// setter(191);
|
||||||
|
|
||||||
|
// var ar = await array;
|
||||||
|
|
||||||
|
// ar.Should().BeEquivalentTo(new[] { 100, 100, 100, 131, 191 });
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void StateFromEnumeration()
|
||||||
|
{
|
||||||
|
var rp = new AsyncReactiveProperty<int>(10);
|
||||||
|
|
||||||
|
var state = rp.ToReadOnlyAsyncReactiveProperty(CancellationToken.None);
|
||||||
|
|
||||||
|
rp.Value = 10;
|
||||||
|
state.Value.Should().Be(10);
|
||||||
|
|
||||||
|
rp.Value = 20;
|
||||||
|
state.Value.Should().Be(20);
|
||||||
|
|
||||||
|
state.Dispose();
|
||||||
|
|
||||||
|
rp.Value = 30;
|
||||||
|
state.Value.Should().Be(20);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -319,5 +319,119 @@ namespace NetCoreTests.Linq
|
|||||||
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
await Assert.ThrowsAsync<UniTaskTestException>(async () => await ys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestOK()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent().CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
a.Value = 10;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
a.Value = 20;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
|
||||||
|
b.Value = 1;
|
||||||
|
list.Count.Should().Be(1);
|
||||||
|
|
||||||
|
list[0].Should().Be((20, 1));
|
||||||
|
|
||||||
|
a.Value = 30;
|
||||||
|
list.Last().Should().Be((30, 1));
|
||||||
|
|
||||||
|
b.Value = 2;
|
||||||
|
list.Last().Should().Be((30, 2));
|
||||||
|
|
||||||
|
a.Dispose();
|
||||||
|
b.Value = 3;
|
||||||
|
list.Last().Should().Be((30, 3));
|
||||||
|
|
||||||
|
b.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestLong()
|
||||||
|
{
|
||||||
|
var a = UniTaskAsyncEnumerable.Range(1, 100000);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
b.Value = 1;
|
||||||
|
|
||||||
|
list[0].Should().Be((100000, 1));
|
||||||
|
|
||||||
|
b.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task CombineLatestError()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
var b = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent()
|
||||||
|
.Select(x => { if (x == 0) { throw new MyException(); } return x; })
|
||||||
|
.CombineLatest(b.WithoutCurrent(), (x, y) => (x, y)).ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
|
||||||
|
a.Value = 10;
|
||||||
|
b.Value = 1;
|
||||||
|
list.Last().Should().Be((10, 1));
|
||||||
|
|
||||||
|
a.Value = 0;
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<MyException>(async () => await complete);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task PariwiseImmediate()
|
||||||
|
{
|
||||||
|
var xs = await UniTaskAsyncEnumerable.Range(1, 5).Pairwise().ToArrayAsync();
|
||||||
|
xs.Should().BeEquivalentTo((1, 2), (2, 3), (3, 4), (4, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Pariwise()
|
||||||
|
{
|
||||||
|
var a = new AsyncReactiveProperty<int>(0);
|
||||||
|
|
||||||
|
var list = new List<(int, int)>();
|
||||||
|
var complete = a.WithoutCurrent().Pairwise().ForEachAsync(x => list.Add(x));
|
||||||
|
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
a.Value = 10;
|
||||||
|
list.Count.Should().Be(0);
|
||||||
|
a.Value = 20;
|
||||||
|
list.Count.Should().Be(1);
|
||||||
|
a.Value = 30;
|
||||||
|
a.Value = 40;
|
||||||
|
a.Value = 50;
|
||||||
|
|
||||||
|
a.Dispose();
|
||||||
|
|
||||||
|
await complete;
|
||||||
|
|
||||||
|
list.Should().BeEquivalentTo((10, 20), (20, 30), (30, 40), (40, 50));
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyException : Exception
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
triggerEvent.SetCompleted();
|
triggerEvent.SetCompleted();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static implicit operator T(AsyncReactiveProperty<T> value)
|
||||||
|
{
|
||||||
|
return value.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (isValueType) return latestValue.ToString();
|
||||||
|
return latestValue?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isValueType;
|
||||||
|
|
||||||
|
static AsyncReactiveProperty()
|
||||||
|
{
|
||||||
|
isValueType = typeof(T).IsValueType;
|
||||||
|
}
|
||||||
|
|
||||||
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
||||||
{
|
{
|
||||||
readonly AsyncReactiveProperty<T> parent;
|
readonly AsyncReactiveProperty<T> parent;
|
||||||
@@ -156,4 +174,198 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class ReadOnlyAsyncReactiveProperty<T> : IReadOnlyAsyncReactiveProperty<T>, IDisposable
|
||||||
|
{
|
||||||
|
TriggerEvent<T> triggerEvent;
|
||||||
|
|
||||||
|
T latestValue;
|
||||||
|
IUniTaskAsyncEnumerator<T> enumerator;
|
||||||
|
|
||||||
|
public T Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return latestValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlyAsyncReactiveProperty(T initialValue, IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
latestValue = initialValue;
|
||||||
|
ConsumeEnumerator(source, cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReadOnlyAsyncReactiveProperty(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
ConsumeEnumerator(source, cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid ConsumeEnumerator(IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await enumerator.MoveNextAsync())
|
||||||
|
{
|
||||||
|
var value = enumerator.Current;
|
||||||
|
this.latestValue = value;
|
||||||
|
triggerEvent.SetResult(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
await enumerator.DisposeAsync();
|
||||||
|
enumerator = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerable<T> WithoutCurrent()
|
||||||
|
{
|
||||||
|
return new WithoutCurrentEnumerable(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new Enumerator(this, cancellationToken, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
enumerator.DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
triggerEvent.SetCompleted();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator T(ReadOnlyAsyncReactiveProperty<T> value)
|
||||||
|
{
|
||||||
|
return value.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (isValueType) return latestValue.ToString();
|
||||||
|
return latestValue?.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool isValueType;
|
||||||
|
|
||||||
|
static ReadOnlyAsyncReactiveProperty()
|
||||||
|
{
|
||||||
|
isValueType = typeof(T).IsValueType;
|
||||||
|
}
|
||||||
|
|
||||||
|
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
||||||
|
{
|
||||||
|
readonly ReadOnlyAsyncReactiveProperty<T> parent;
|
||||||
|
|
||||||
|
public WithoutCurrentEnumerable(ReadOnlyAsyncReactiveProperty<T> parent)
|
||||||
|
{
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new Enumerator(parent, cancellationToken, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
|
||||||
|
{
|
||||||
|
static Action<object> cancellationCallback = CancellationCallback;
|
||||||
|
|
||||||
|
readonly ReadOnlyAsyncReactiveProperty<T> parent;
|
||||||
|
readonly CancellationToken cancellationToken;
|
||||||
|
readonly CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
|
T value;
|
||||||
|
bool isDisposed;
|
||||||
|
bool firstCall;
|
||||||
|
|
||||||
|
public Enumerator(ReadOnlyAsyncReactiveProperty<T> parent, CancellationToken cancellationToken, bool publishCurrentValue)
|
||||||
|
{
|
||||||
|
this.parent = parent;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.firstCall = publishCurrentValue;
|
||||||
|
|
||||||
|
parent.triggerEvent.Add(this);
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
|
||||||
|
if (cancellationToken.CanBeCanceled)
|
||||||
|
{
|
||||||
|
cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationCallback, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Current => value;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
// raise latest value on first call.
|
||||||
|
if (firstCall)
|
||||||
|
{
|
||||||
|
firstCall = false;
|
||||||
|
value = parent.Value;
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!isDisposed)
|
||||||
|
{
|
||||||
|
isDisposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
completionSource.TrySetCanceled(cancellationToken);
|
||||||
|
parent.triggerEvent.Remove(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnNext(T value)
|
||||||
|
{
|
||||||
|
this.value = value;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCanceled(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted()
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnError(Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CancellationCallback(object state)
|
||||||
|
{
|
||||||
|
var self = (Enumerator)state;
|
||||||
|
self.DisposeAsync().Forget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class StateExtensions
|
||||||
|
{
|
||||||
|
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new ReadOnlyAsyncReactiveProperty<T>(source, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReadOnlyAsyncReactiveProperty<T> ToReadOnlyAsyncReactiveProperty<T>(this IUniTaskAsyncEnumerable<T> source, T initialValue, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new ReadOnlyAsyncReactiveProperty<T>(initialValue, source, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -14,16 +14,19 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static UniTask.Awaiter GetAwaiter(this IEnumerator enumerator)
|
public static UniTask.Awaiter GetAwaiter(this IEnumerator enumerator)
|
||||||
{
|
{
|
||||||
|
Error.ThrowArgumentNullException(enumerator, nameof(enumerator));
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter();
|
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token).GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this IEnumerator enumerator)
|
public static UniTask WithCancellation(this IEnumerator enumerator, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, CancellationToken.None, out var token), token);
|
Error.ThrowArgumentNullException(enumerator, nameof(enumerator));
|
||||||
|
return new UniTask(EnumeratorPromise.Create(enumerator, PlayerLoopTiming.Update, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
public static UniTask ToUniTask(this IEnumerator enumerator, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
|
Error.ThrowArgumentNullException(enumerator, nameof(enumerator));
|
||||||
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token);
|
return new UniTask(EnumeratorPromise.Create(enumerator, timing, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,21 +13,23 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
public static class AddressableAsyncExtensions
|
public static class AddressableAsyncExtensions
|
||||||
{
|
{
|
||||||
#region AsyncOperationHandle
|
#region AsyncOperationHandle
|
||||||
|
|
||||||
public static AsyncOperationHandleAwaiter GetAwaiter(this AsyncOperationHandle handle)
|
public static AsyncOperationHandleAwaiter GetAwaiter(this AsyncOperationHandle handle)
|
||||||
{
|
{
|
||||||
return new AsyncOperationHandleAwaiter(handle);
|
return new AsyncOperationHandleAwaiter(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this AsyncOperationHandle handle)
|
public static UniTask WithCancellation(this AsyncOperationHandle handle, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
if (handle.IsDone) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask ToUniTask(this AsyncOperationHandle handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellation, out var token), token);
|
if (handle.IsDone) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(AsyncOperationHandleConfiguredSource.Create(handle, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
|
public struct AsyncOperationHandleAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -75,7 +77,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
sealed class AsyncOperationHandleConfiguredSource : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AsyncOperationHandleConfiguredSource> pool = new PromisePool<AsyncOperationHandleConfiguredSource>();
|
static readonly PromisePool<AsyncOperationHandleConfiguredSource> pool = new PromisePool<AsyncOperationHandleConfiguredSource>();
|
||||||
|
|
||||||
@@ -185,22 +187,24 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region AsyncOperationHandle_T
|
#region AsyncOperationHandle_T
|
||||||
|
|
||||||
public static AsyncOperationHandleAwaiter<T> GetAwaiter<T>(this AsyncOperationHandle<T> handle)
|
public static AsyncOperationHandleAwaiter<T> GetAwaiter<T>(this AsyncOperationHandle<T> handle)
|
||||||
{
|
{
|
||||||
return new AsyncOperationHandleAwaiter<T>(handle);
|
return new AsyncOperationHandleAwaiter<T>(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle)
|
public static UniTask<T> WithCancellation<T>(this AsyncOperationHandle<T> handle, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
if (handle.IsDone) return UniTask.FromResult(handle.Result);
|
||||||
|
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<T> ConfigureAwait<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<T> ToUniTask<T>(this AsyncOperationHandle<T> handle, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
||||||
{
|
{
|
||||||
|
if (handle.IsDone) return UniTask.FromResult(handle.Result);
|
||||||
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellation, out var token), token);
|
return new UniTask<T>(AsyncOperationHandleConfiguredSource<T>.Create(handle, timing, progress, cancellation, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +254,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, IPromisePoolItem
|
sealed class AsyncOperationHandleConfiguredSource<T> : IUniTaskSource<T>, IPlayerLoopItem, IPromisePoolItem
|
||||||
{
|
{
|
||||||
static readonly PromisePool<AsyncOperationHandleConfiguredSource<T>> pool = new PromisePool<AsyncOperationHandleConfiguredSource<T>>();
|
static readonly PromisePool<AsyncOperationHandleConfiguredSource<T>> pool = new PromisePool<AsyncOperationHandleConfiguredSource<T>>();
|
||||||
|
|
||||||
@@ -366,7 +370,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
245
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs
vendored
Normal file
245
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs
vendored
Normal file
@@ -0,0 +1,245 @@
|
|||||||
|
// asmdef Version Defines, enabled when com.demigiant.dotween is imported.
|
||||||
|
|
||||||
|
#if UNITASK_DOTWEEN_SUPPORT
|
||||||
|
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using DG.Tweening;
|
||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks
|
||||||
|
{
|
||||||
|
// The idea of TweenCancelBehaviour is borrowed from https://www.shibuya24.info/entry/dotween_async_await
|
||||||
|
public enum TweenCancelBehaviour
|
||||||
|
{
|
||||||
|
Kill,
|
||||||
|
KillWithCompleteCallback,
|
||||||
|
Complete,
|
||||||
|
CompleteWithSeqeunceCallback,
|
||||||
|
CancelAwait,
|
||||||
|
|
||||||
|
// AndCancelAwait
|
||||||
|
KillAndCancelAwait,
|
||||||
|
KillWithCompleteCallbackAndCancelAwait,
|
||||||
|
CompleteAndCancelAwait,
|
||||||
|
CompleteWithSeqeunceCallbackAndCancelAwait
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DOTweenAsyncExtensions
|
||||||
|
{
|
||||||
|
public static TweenAwaiter GetAwaiter(this Tween tween)
|
||||||
|
{
|
||||||
|
return new TweenAwaiter(tween);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask WithCancellation(this Tween tween, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(tween, nameof(tween));
|
||||||
|
|
||||||
|
if (!tween.IsActive()) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(TweenConfiguredSource.Create(tween, TweenCancelBehaviour.Kill, cancellationToken, out var token), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask ToUniTask(this Tween tween, TweenCancelBehaviour tweenCancelBehaviour = TweenCancelBehaviour.Kill, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(tween, nameof(tween));
|
||||||
|
|
||||||
|
if (!tween.IsActive()) return UniTask.CompletedTask;
|
||||||
|
return new UniTask(TweenConfiguredSource.Create(tween, tweenCancelBehaviour, cancellationToken, out var token), token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct TweenAwaiter : ICriticalNotifyCompletion
|
||||||
|
{
|
||||||
|
readonly Tween tween;
|
||||||
|
|
||||||
|
// killed(non active) as completed.
|
||||||
|
public bool IsCompleted => !tween.IsActive();
|
||||||
|
|
||||||
|
public TweenAwaiter(Tween tween)
|
||||||
|
{
|
||||||
|
this.tween = tween;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TweenAwaiter GetAwaiter()
|
||||||
|
{
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(System.Action continuation)
|
||||||
|
{
|
||||||
|
UnsafeOnCompleted(continuation);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UnsafeOnCompleted(System.Action continuation)
|
||||||
|
{
|
||||||
|
// convert Action -> TweenCallback allocation.
|
||||||
|
// onKill is called after OnCompleted, both Complete(false/true) and Kill(false/true).
|
||||||
|
tween.onKill = new TweenCallback(continuation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class TweenConfiguredSource : IUniTaskSource, IPromisePoolItem
|
||||||
|
{
|
||||||
|
static readonly PromisePool<TweenConfiguredSource> pool = new PromisePool<TweenConfiguredSource>();
|
||||||
|
static Action<object> CancellationCallbackDelegate = CancellationCallback;
|
||||||
|
|
||||||
|
Tween tween;
|
||||||
|
TweenCancelBehaviour cancelBehaviour;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
bool canceled;
|
||||||
|
|
||||||
|
CancellationTokenRegistration cancellationTokenRegistration;
|
||||||
|
UniTaskCompletionSourceCore<AsyncUnit> core;
|
||||||
|
|
||||||
|
TweenConfiguredSource()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskSource Create(Tween tween, TweenCancelBehaviour cancelBehaviour, CancellationToken cancellationToken, out short token)
|
||||||
|
{
|
||||||
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = pool.TryRent() ?? new TweenConfiguredSource();
|
||||||
|
|
||||||
|
result.tween = tween;
|
||||||
|
result.cancelBehaviour = cancelBehaviour;
|
||||||
|
result.cancellationToken = cancellationToken;
|
||||||
|
|
||||||
|
TaskTracker.TrackActiveTask(result, 3);
|
||||||
|
|
||||||
|
result.RegisterEvent();
|
||||||
|
|
||||||
|
token = result.core.Version;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterEvent()
|
||||||
|
{
|
||||||
|
if (cancellationToken.CanBeCanceled)
|
||||||
|
{
|
||||||
|
cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(CancellationCallbackDelegate, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
// allocate delegate.
|
||||||
|
tween.OnKill(new TweenCallback(OnKill));
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnKill()
|
||||||
|
{
|
||||||
|
cancellationTokenRegistration.Dispose();
|
||||||
|
if (canceled)
|
||||||
|
{
|
||||||
|
if (cancelBehaviour == TweenCancelBehaviour.CancelAwait)
|
||||||
|
{
|
||||||
|
// already called TrySetCanceled, do nothing.
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
core.TrySetCanceled(cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
core.TrySetResult(AsyncUnit.Default);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void CancellationCallback(object state)
|
||||||
|
{
|
||||||
|
var self = (TweenConfiguredSource)state;
|
||||||
|
|
||||||
|
switch (self.cancelBehaviour)
|
||||||
|
{
|
||||||
|
case TweenCancelBehaviour.Kill:
|
||||||
|
default:
|
||||||
|
self.tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillAndCancelAwait:
|
||||||
|
self.canceled = true;
|
||||||
|
self.tween.Kill(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallback:
|
||||||
|
self.tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.KillWithCompleteCallbackAndCancelAwait:
|
||||||
|
self.canceled = true;
|
||||||
|
self.tween.Kill(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.Complete:
|
||||||
|
self.tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteAndCancelAwait:
|
||||||
|
self.canceled = true;
|
||||||
|
self.tween.Complete(false);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallback:
|
||||||
|
self.tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CompleteWithSeqeunceCallbackAndCancelAwait:
|
||||||
|
self.canceled = true;
|
||||||
|
self.tween.Complete(true);
|
||||||
|
break;
|
||||||
|
case TweenCancelBehaviour.CancelAwait:
|
||||||
|
self.canceled = true;
|
||||||
|
self.core.TrySetCanceled(self.cancellationToken);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void GetResult(short token)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
core.GetResult(token);
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
pool.TryReturn(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public UniTaskStatus GetStatus(short token)
|
||||||
|
{
|
||||||
|
return core.GetStatus(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTaskStatus UnsafeGetStatus()
|
||||||
|
{
|
||||||
|
return core.UnsafeGetStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||||
|
{
|
||||||
|
core.OnCompleted(continuation, state, token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
core.Reset();
|
||||||
|
tween = default;
|
||||||
|
cancellationToken = default;
|
||||||
|
}
|
||||||
|
|
||||||
|
~TweenConfiguredSource()
|
||||||
|
{
|
||||||
|
if (pool.TryReturn(this))
|
||||||
|
{
|
||||||
|
GC.ReRegisterForFinalize(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
11
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs.meta
vendored
Normal file
11
src/UniTask/Assets/Plugins/UniTask/Runtime/External/DoTweenAsyncExtensions.cs.meta
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1f448d5bc5b232e4f98d89d5d1832e8e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
11372
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.cs
Normal file
11372
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6cb07f6e88287e34d9b9301a572284a5
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
221
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.tt
Normal file
221
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/CombineLatest.tt
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
<#@ 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" #>
|
||||||
|
<#
|
||||||
|
var tMax = 15;
|
||||||
|
Func<int, string> typeArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"T{x}")) + ", TResult";
|
||||||
|
Func<int, string> paramArgs = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"IUniTaskAsyncEnumerable<T{x}> source{x}"));
|
||||||
|
Func<int, string> parameters = x => string.Join(", ", Enumerable.Range(1, x).Select(x => $"source{x}"));
|
||||||
|
|
||||||
|
|
||||||
|
#>
|
||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
<# for(var i = 2; i <= tMax; i++) { #>
|
||||||
|
public static IUniTaskAsyncEnumerable<TResult> CombineLatest<<#= typeArgs(i) #>>(this <#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
Error.ThrowArgumentNullException(source<#= j #>, nameof(source<#= j #>));
|
||||||
|
<# } #>
|
||||||
|
Error.ThrowArgumentNullException(resultSelector, nameof(resultSelector));
|
||||||
|
|
||||||
|
return new CombineLatest<<#= typeArgs(i) #>>(<#= parameters(i) #>, resultSelector);
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
|
||||||
|
<# for(var i = 2; i <= tMax; i++) { #>
|
||||||
|
internal class CombineLatest<<#= typeArgs(i) #>> : IUniTaskAsyncEnumerable<TResult>
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
readonly IUniTaskAsyncEnumerable<T<#= j #>> source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
readonly Func<<#= typeArgs(i) #>> resultSelector;
|
||||||
|
|
||||||
|
public CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
this.source<#= j #> = source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
this.resultSelector = resultSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TResult> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _CombineLatest(<#= parameters(i) #>, resultSelector, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
class _CombineLatest : MoveNextSource, IUniTaskAsyncEnumerator<TResult>
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
static readonly Action<object> Completed<#= j #>Delegate = Completed<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
const int CompleteCount = <#= i #>;
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
readonly IUniTaskAsyncEnumerable<T<#= j #>> source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
readonly Func<<#= typeArgs(i) #>> resultSelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
IUniTaskAsyncEnumerator<T<#= j #>> enumerator<#= j #>;
|
||||||
|
UniTask<bool>.Awaiter awaiter<#= j #>;
|
||||||
|
bool hasCurrent<#= j #>;
|
||||||
|
bool running<#= j #>;
|
||||||
|
T<#= j #> current<#= j #>;
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
int completedCount;
|
||||||
|
bool syncRunning;
|
||||||
|
TResult result;
|
||||||
|
|
||||||
|
public _CombineLatest(<#= paramArgs(i) #>, Func<<#= typeArgs(i) #>> resultSelector, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
this.source<#= j #> = source<#= j #>;
|
||||||
|
<# } #>
|
||||||
|
this.resultSelector = resultSelector;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TResult Current => result;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
if (completedCount == CompleteCount) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (enumerator1 == null)
|
||||||
|
{
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
enumerator<#= j #> = source<#= j #>.GetAsyncEnumerator(cancellationToken);
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
|
||||||
|
AGAIN:
|
||||||
|
syncRunning = true;
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
if (!running<#= j #>)
|
||||||
|
{
|
||||||
|
running<#= j #> = true;
|
||||||
|
awaiter<#= j #> = enumerator<#= j #>.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter<#= j #>.IsCompleted)
|
||||||
|
{
|
||||||
|
Completed<#= j #>(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
<# } #>
|
||||||
|
|
||||||
|
if (<#= string.Join(" || ", Enumerable.Range(1, i).Select(x => $"!running{x}")) #>)
|
||||||
|
{
|
||||||
|
goto AGAIN;
|
||||||
|
}
|
||||||
|
syncRunning = false;
|
||||||
|
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
static void Completed<#= j #>(object state)
|
||||||
|
{
|
||||||
|
var self = (_CombineLatest)state;
|
||||||
|
self.running<#= j #> = false;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (self.awaiter<#= j #>.GetResult())
|
||||||
|
{
|
||||||
|
self.hasCurrent<#= j #> = true;
|
||||||
|
self.current<#= j #> = self.enumerator<#= j #>.Current;
|
||||||
|
goto SUCCESS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
if (Interlocked.Increment(ref self.completedCount) == CompleteCount)
|
||||||
|
{
|
||||||
|
goto COMPLETE;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
self.completedCount = CompleteCount;
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SUCCESS:
|
||||||
|
if (!self.TrySetResult())
|
||||||
|
{
|
||||||
|
if (self.syncRunning) return;
|
||||||
|
self.running<#= j #> = true; // as complete, no more call MoveNextAsync.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
self.awaiter<#= j #> = self.enumerator<#= j #>.MoveNextAsync().GetAwaiter();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
self.completedCount = CompleteCount;
|
||||||
|
self.completionSource.TrySetException(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.awaiter<#= j #>.SourceOnCompleted(Completed<#= j #>Delegate, self);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
COMPLETE:
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
bool TrySetResult()
|
||||||
|
{
|
||||||
|
if (<#= string.Join(" && ", Enumerable.Range(1, i).Select(x => $"hasCurrent{x}")) #>)
|
||||||
|
{
|
||||||
|
result = resultSelector(<#= string.Join(", ", Enumerable.Range(1, i).Select(x => $"current{x}")) #>);
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
<# for(var j = 1; j <= i; j++) { #>
|
||||||
|
if (enumerator<#= j #> != null)
|
||||||
|
{
|
||||||
|
await enumerator<#= j #>.DisposeAsync();
|
||||||
|
}
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
<# } #>
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: b1b8cfa9d17af814a971ee2224aaaaa2
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
128
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Pairwise.cs
Normal file
128
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Pairwise.cs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<(TSource, TSource)> Pairwise<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||||
|
{
|
||||||
|
Error.ThrowArgumentNullException(source, nameof(source));
|
||||||
|
|
||||||
|
return new Pairwise<TSource>(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class Pairwise<TSource> : IUniTaskAsyncEnumerable<(TSource, TSource)>
|
||||||
|
{
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
|
||||||
|
public Pairwise(IUniTaskAsyncEnumerable<TSource> source)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<(TSource, TSource)> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _Pairwise(source, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _Pairwise : MoveNextSource, IUniTaskAsyncEnumerator<(TSource, TSource)>
|
||||||
|
{
|
||||||
|
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||||
|
|
||||||
|
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||||
|
UniTask<bool>.Awaiter awaiter;
|
||||||
|
|
||||||
|
TSource prev;
|
||||||
|
bool isFirst;
|
||||||
|
|
||||||
|
public _Pairwise(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.source = source;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
TaskTracker.TrackActiveTask(this, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public (TSource, TSource) Current { get; private set; }
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
|
||||||
|
if (enumerator == null)
|
||||||
|
{
|
||||||
|
isFirst = true;
|
||||||
|
enumerator = source.GetAsyncEnumerator(cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
SourceMoveNext();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
void SourceMoveNext()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||||
|
if (awaiter.IsCompleted)
|
||||||
|
{
|
||||||
|
MoveNextCore(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void MoveNextCore(object state)
|
||||||
|
{
|
||||||
|
var self = (_Pairwise)state;
|
||||||
|
|
||||||
|
if (self.TryGetResult(self.awaiter, out var result))
|
||||||
|
{
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
|
if (self.isFirst)
|
||||||
|
{
|
||||||
|
self.isFirst = false;
|
||||||
|
self.prev = self.enumerator.Current;
|
||||||
|
self.SourceMoveNext(); // run again. okay to use recursive(only one more).
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var p = self.prev;
|
||||||
|
self.prev = self.enumerator.Current;
|
||||||
|
self.Current = (p, self.prev);
|
||||||
|
self.completionSource.TrySetResult(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
self.completionSource.TrySetResult(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
if (enumerator != null)
|
||||||
|
{
|
||||||
|
return enumerator.DisposeAsync();
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cddbf051d2a88f549986c468b23214af
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -1,7 +1,4 @@
|
|||||||
using Cysharp.Threading.Tasks.Internal;
|
using System.Threading;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
|
||||||
|
|
||||||
namespace Cysharp.Threading.Tasks.Linq
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,241 @@
|
|||||||
|
using Cysharp.Threading.Tasks.Internal;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
|
namespace Cysharp.Threading.Tasks.Linq
|
||||||
|
{
|
||||||
|
public static partial class UniTaskAsyncEnumerable
|
||||||
|
{
|
||||||
|
public static IUniTaskAsyncEnumerable<TProperty> EveryValueChanged<TTarget, TProperty>(TTarget target, Func<TTarget, TProperty> propertySelector, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer<TProperty> equalityComparer = null)
|
||||||
|
where TTarget : class
|
||||||
|
{
|
||||||
|
var unityObject = target as UnityEngine.Object;
|
||||||
|
var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null)
|
||||||
|
|
||||||
|
if (isUnityObject)
|
||||||
|
{
|
||||||
|
return new EveryValueChangedUnityObject<TTarget, TProperty>(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault<TProperty>(), monitorTiming);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return new EveryValueChangedStandardObject<TTarget, TProperty>(target, propertySelector, equalityComparer ?? UnityEqualityComparer.GetDefault<TProperty>(), monitorTiming);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class EveryValueChangedUnityObject<TTarget, TProperty> : IUniTaskAsyncEnumerable<TProperty>
|
||||||
|
{
|
||||||
|
readonly TTarget target;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly PlayerLoopTiming monitorTiming;
|
||||||
|
|
||||||
|
public EveryValueChangedUnityObject(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.monitorTiming = monitorTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TProperty> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator<TProperty>, IPlayerLoopItem
|
||||||
|
{
|
||||||
|
readonly TTarget target;
|
||||||
|
readonly UnityEngine.Object targetAsUnityObject;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
bool first;
|
||||||
|
TProperty currentValue;
|
||||||
|
bool disposed;
|
||||||
|
|
||||||
|
public _EveryValueChanged(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.targetAsUnityObject = target as UnityEngine.Object;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.first = true;
|
||||||
|
TaskTracker.TrackActiveTask(this, 2);
|
||||||
|
PlayerLoopHelper.AddAction(monitorTiming, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TProperty Current => currentValue;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
// return false instead of throw
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
if (targetAsUnityObject == null)
|
||||||
|
{
|
||||||
|
return CompletedTasks.False;
|
||||||
|
}
|
||||||
|
this.currentValue = propertySelector(target);
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TProperty nextValue = default(TProperty);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nextValue = propertySelector(target);
|
||||||
|
if (equalityComparer.Equals(currentValue, nextValue))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentValue = nextValue;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal sealed class EveryValueChangedStandardObject<TTarget, TProperty> : IUniTaskAsyncEnumerable<TProperty>
|
||||||
|
where TTarget : class
|
||||||
|
{
|
||||||
|
readonly WeakReference<TTarget> target;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly PlayerLoopTiming monitorTiming;
|
||||||
|
|
||||||
|
public EveryValueChangedStandardObject(TTarget target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming)
|
||||||
|
{
|
||||||
|
this.target = new WeakReference<TTarget>(target, false);
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.monitorTiming = monitorTiming;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IUniTaskAsyncEnumerator<TProperty> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
return new _EveryValueChanged(target, propertySelector, equalityComparer, monitorTiming, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class _EveryValueChanged : MoveNextSource, IUniTaskAsyncEnumerator<TProperty>, IPlayerLoopItem
|
||||||
|
{
|
||||||
|
readonly WeakReference<TTarget> target;
|
||||||
|
readonly IEqualityComparer<TProperty> equalityComparer;
|
||||||
|
readonly Func<TTarget, TProperty> propertySelector;
|
||||||
|
CancellationToken cancellationToken;
|
||||||
|
|
||||||
|
bool first;
|
||||||
|
TProperty currentValue;
|
||||||
|
bool disposed;
|
||||||
|
|
||||||
|
public _EveryValueChanged(WeakReference<TTarget> target, Func<TTarget, TProperty> propertySelector, IEqualityComparer<TProperty> equalityComparer, PlayerLoopTiming monitorTiming, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.target = target;
|
||||||
|
this.propertySelector = propertySelector;
|
||||||
|
this.equalityComparer = equalityComparer;
|
||||||
|
this.cancellationToken = cancellationToken;
|
||||||
|
this.first = true;
|
||||||
|
TaskTracker.TrackActiveTask(this, 2);
|
||||||
|
PlayerLoopHelper.AddAction(monitorTiming, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TProperty Current => currentValue;
|
||||||
|
|
||||||
|
public UniTask<bool> MoveNextAsync()
|
||||||
|
{
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
|
||||||
|
|
||||||
|
if (first)
|
||||||
|
{
|
||||||
|
first = false;
|
||||||
|
if (!target.TryGetTarget(out var t))
|
||||||
|
{
|
||||||
|
return CompletedTasks.False;
|
||||||
|
}
|
||||||
|
this.currentValue = propertySelector(t);
|
||||||
|
return CompletedTasks.True;
|
||||||
|
}
|
||||||
|
|
||||||
|
completionSource.Reset();
|
||||||
|
return new UniTask<bool>(this, completionSource.Version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UniTask DisposeAsync()
|
||||||
|
{
|
||||||
|
if (!disposed)
|
||||||
|
{
|
||||||
|
disposed = true;
|
||||||
|
TaskTracker.RemoveTracking(this);
|
||||||
|
}
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MoveNext()
|
||||||
|
{
|
||||||
|
UnityEngine.Debug.Log("TRY_RESULT:" + target.TryGetTarget(out var _));
|
||||||
|
if (disposed || cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t))
|
||||||
|
{
|
||||||
|
completionSource.TrySetResult(false);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
TProperty nextValue = default(TProperty);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
nextValue = propertySelector(t);
|
||||||
|
if (equalityComparer.Equals(currentValue, nextValue))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
completionSource.TrySetException(ex);
|
||||||
|
DisposeAsync().Forget();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
currentValue = nextValue;
|
||||||
|
completionSource.TrySetResult(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1ec39f1c41c305344854782c935ad354
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -105,34 +105,67 @@ namespace Cysharp.Threading.Tasks
|
|||||||
/// helper of create add UniTaskVoid to delegate.
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
/// For example: FooEvent += () => UniTask.Void(async () => { /* */ })
|
/// For example: FooEvent += () => UniTask.Void(async () => { /* */ })
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Void(Func<UniTask> asyncAction)
|
public static void Void(Func<UniTaskVoid> asyncAction)
|
||||||
{
|
{
|
||||||
asyncAction().Forget();
|
asyncAction().Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Action VoidAction(Func<UniTask> asyncAction)
|
/// <summary>
|
||||||
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
|
/// </summary>
|
||||||
|
public static void Void(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
return () => Void(asyncAction);
|
asyncAction(cancellationToken).Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
|
|
||||||
public static UnityEngine.Events.UnityAction VoidUnityAction(Func<UniTask> asyncAction)
|
|
||||||
{
|
|
||||||
return () => Void(asyncAction);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// helper of create add UniTaskVoid to delegate.
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
/// For example: FooEvent += (sender, e) => UniTask.Void(async arg => { /* */ }, (sender, e))
|
/// For example: FooEvent += (sender, e) => UniTask.Void(async arg => { /* */ }, (sender, e))
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void Void<T>(Func<T, UniTask> asyncAction, T state)
|
public static void Void<T>(Func<T, UniTaskVoid> asyncAction, T state)
|
||||||
{
|
{
|
||||||
asyncAction(state).Forget();
|
asyncAction(state).Forget();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
|
/// For example: FooAction = UniTask.Action(async () => { /* */ })
|
||||||
|
/// </summary>
|
||||||
|
public static Action Action(Func<UniTaskVoid> asyncAction)
|
||||||
|
{
|
||||||
|
return () => asyncAction().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// helper of create add UniTaskVoid to delegate.
|
||||||
|
/// </summary>
|
||||||
|
public static Action Action(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return () => asyncAction(cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create async void(UniTaskVoid) UnityAction.
|
||||||
|
/// For exampe: onClick.AddListener(UniTask.UnityAction(async () => { /* */ } ))
|
||||||
|
/// </summary>
|
||||||
|
public static UnityEngine.Events.UnityAction UnityAction(Func<UniTaskVoid> asyncAction)
|
||||||
|
{
|
||||||
|
return () => asyncAction().Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create async void(UniTaskVoid) UnityAction.
|
||||||
|
/// For exampe: onClick.AddListener(UniTask.UnityAction(FooAsync, this.GetCancellationTokenOnDestroy()))
|
||||||
|
/// </summary>
|
||||||
|
public static UnityEngine.Events.UnityAction UnityAction(Func<CancellationToken, UniTaskVoid> asyncAction, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return () => asyncAction(cancellationToken).Forget();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Defer the task creation just before call await.
|
/// Defer the task creation just before call await.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
var unityObject = target as UnityEngine.Object;
|
var unityObject = target as UnityEngine.Object;
|
||||||
var isUnityObject = !object.ReferenceEquals(target, null); // don't use (unityObject == null)
|
var isUnityObject = target is UnityEngine.Object; // don't use (unityObject == null)
|
||||||
|
|
||||||
return new UniTask<U>(isUnityObject
|
return new UniTask<U>(isUnityObject
|
||||||
? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
|
? WaitUntilValueChangedUnityObjectPromise<T, U>.Create(target, monitorFunction, equalityComparer, monitorTiming, cancellationToken, out var token)
|
||||||
@@ -330,6 +330,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
static readonly PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>>();
|
static readonly PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>> pool = new PromisePool<WaitUntilValueChangedUnityObjectPromise<T, U>>();
|
||||||
|
|
||||||
T target;
|
T target;
|
||||||
|
UnityEngine.Object targetAsUnityObject;
|
||||||
U currentValue;
|
U currentValue;
|
||||||
Func<T, U> monitorFunction;
|
Func<T, U> monitorFunction;
|
||||||
IEqualityComparer<U> equalityComparer;
|
IEqualityComparer<U> equalityComparer;
|
||||||
@@ -351,6 +352,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
var result = pool.TryRent() ?? new WaitUntilValueChangedUnityObjectPromise<T, U>();
|
var result = pool.TryRent() ?? new WaitUntilValueChangedUnityObjectPromise<T, U>();
|
||||||
|
|
||||||
result.target = target;
|
result.target = target;
|
||||||
|
result.targetAsUnityObject = target as UnityEngine.Object;
|
||||||
result.monitorFunction = monitorFunction;
|
result.monitorFunction = monitorFunction;
|
||||||
result.currentValue = monitorFunction(target);
|
result.currentValue = monitorFunction(target);
|
||||||
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
|
result.equalityComparer = equalityComparer ?? UnityEqualityComparer.GetDefault<U>();
|
||||||
@@ -399,7 +401,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
|
|
||||||
public bool MoveNext()
|
public bool MoveNext()
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested || target == null) // destroyed = cancel.
|
if (cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel.
|
||||||
{
|
{
|
||||||
core.TrySetCanceled(cancellationToken);
|
core.TrySetCanceled(cancellationToken);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
"name": "UniTask",
|
"name": "UniTask",
|
||||||
"references": [
|
"references": [
|
||||||
"Unity.ResourceManager",
|
"Unity.ResourceManager",
|
||||||
"Unity.TextMeshPro"
|
"Unity.TextMeshPro",
|
||||||
|
"DOTween.Modules"
|
||||||
],
|
],
|
||||||
"includePlatforms": [],
|
"includePlatforms": [],
|
||||||
"excludePlatforms": [],
|
"excludePlatforms": [],
|
||||||
@@ -21,6 +22,11 @@
|
|||||||
"name": "com.unity.textmeshpro",
|
"name": "com.unity.textmeshpro",
|
||||||
"expression": "",
|
"expression": "",
|
||||||
"define": "UNITASK_TEXTMESHPRO_SUPPORT"
|
"define": "UNITASK_TEXTMESHPRO_SUPPORT"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "com.demigiant.dotween",
|
||||||
|
"expression": "",
|
||||||
|
"define": "UNITASK_DOTWEEN_SUPPORT"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"noEngineReferences": false
|
"noEngineReferences": false
|
||||||
|
|||||||
@@ -123,7 +123,14 @@ namespace Cysharp.Threading.Tasks
|
|||||||
{
|
{
|
||||||
// setup result
|
// setup result
|
||||||
this.hasUnhandledError = true;
|
this.hasUnhandledError = true;
|
||||||
this.error = ExceptionDispatchInfo.Capture(error);
|
if (error is OperationCanceledException)
|
||||||
|
{
|
||||||
|
this.error = error;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
this.error = ExceptionDispatchInfo.Capture(error);
|
||||||
|
}
|
||||||
|
|
||||||
if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null)
|
if (continuation != null || Interlocked.CompareExchange(ref this.continuation, UniTaskCompletionSourceCoreShared.s_sentinel, null) != null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -559,36 +559,6 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return await continuationFunction();
|
return await continuationFunction();
|
||||||
}
|
}
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
|
||||||
|
|
||||||
public static async UniTask ConfigureAwait(this Task task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
await task.ConfigureAwait(false);
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask<T> ConfigureAwait<T>(this Task<T> task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
var v = await task.ConfigureAwait(false);
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask ConfigureAwait(this UniTask task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
await task;
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async UniTask<T> ConfigureAwait<T>(this UniTask<T> task, PlayerLoopTiming timing)
|
|
||||||
{
|
|
||||||
var v = await task;
|
|
||||||
await UniTask.Yield(timing);
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
public static async UniTask<T> Unwrap<T>(this UniTask<UniTask<T>> task)
|
public static async UniTask<T> Unwrap<T>(this UniTask<UniTask<T>> task)
|
||||||
{
|
{
|
||||||
return await await task;
|
return await await task;
|
||||||
|
|||||||
@@ -29,21 +29,7 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UniTask(handler, token).GetAwaiter();
|
return new UniTask(handler, token).GetAwaiter();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this JobHandle jobHandle)
|
public static UniTask ToUniTask(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
||||||
{
|
|
||||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
|
||||||
{
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.EarlyUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.Update, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PreLateUpdate, handler);
|
|
||||||
PlayerLoopHelper.AddAction(PlayerLoopTiming.PostLateUpdate, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new UniTask(handler, token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this JobHandle jobHandle, PlayerLoopTiming waitTiming)
|
|
||||||
{
|
{
|
||||||
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
var handler = JobHandlePromise.Create(jobHandle, out var token);
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -21,18 +21,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new AsyncOperationAwaiter(asyncOperation);
|
return new AsyncOperationAwaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ToUniTask(this AsyncOperation asyncOperation)
|
public static UniTask WithCancellation(this AsyncOperation asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.CompletedTask;
|
||||||
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask ConfigureAwait(this AsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask ToUniTask(this AsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.CompletedTask;
|
||||||
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new UniTask(AsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
public struct AsyncOperationAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -125,7 +125,6 @@ namespace Cysharp.Threading.Tasks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public UniTaskStatus GetStatus(short token)
|
public UniTaskStatus GetStatus(short token)
|
||||||
{
|
{
|
||||||
return core.GetStatus(token);
|
return core.GetStatus(token);
|
||||||
@@ -190,18 +189,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new ResourceRequestAwaiter(asyncOperation);
|
return new ResourceRequestAwaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation)
|
public static UniTask<UnityEngine.Object> WithCancellation(this ResourceRequest asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
|
||||||
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityEngine.Object> ConfigureAwait(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<UnityEngine.Object> ToUniTask(this ResourceRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
|
||||||
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new UniTask<UnityEngine.Object>(ResourceRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct ResourceRequestAwaiter : ICriticalNotifyCompletion
|
public struct ResourceRequestAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -367,18 +366,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new AssetBundleRequestAwaiter(asyncOperation);
|
return new AssetBundleRequestAwaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityEngine.Object> ToUniTask(this AssetBundleRequest asyncOperation)
|
public static UniTask<UnityEngine.Object> WithCancellation(this AssetBundleRequest asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
|
||||||
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityEngine.Object> ConfigureAwait(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<UnityEngine.Object> ToUniTask(this AssetBundleRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.asset);
|
||||||
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new UniTask<UnityEngine.Object>(AssetBundleRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion
|
public struct AssetBundleRequestAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -544,18 +543,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new AssetBundleCreateRequestAwaiter(asyncOperation);
|
return new AssetBundleCreateRequestAwaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<AssetBundle> ToUniTask(this AssetBundleCreateRequest asyncOperation)
|
public static UniTask<AssetBundle> WithCancellation(this AssetBundleCreateRequest asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle);
|
||||||
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<AssetBundle> ConfigureAwait(this AssetBundleCreateRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<AssetBundle> ToUniTask(this AssetBundleCreateRequest asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.assetBundle);
|
||||||
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new UniTask<AssetBundle>(AssetBundleCreateRequestConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion
|
public struct AssetBundleCreateRequestAwaiter : ICriticalNotifyCompletion
|
||||||
@@ -722,18 +721,18 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UnityWebRequestAsyncOperationAwaiter(asyncOperation);
|
return new UnityWebRequestAsyncOperationAwaiter(asyncOperation);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityWebRequest> ToUniTask(this UnityWebRequestAsyncOperation asyncOperation)
|
public static UniTask<UnityWebRequest> WithCancellation(this UnityWebRequestAsyncOperation asyncOperation, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.webRequest);
|
||||||
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, CancellationToken.None, out var token), token);
|
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, PlayerLoopTiming.Update, null, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static UniTask<UnityWebRequest> ConfigureAwait(this UnityWebRequestAsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellation = default(CancellationToken))
|
public static UniTask<UnityWebRequest> ToUniTask(this UnityWebRequestAsyncOperation asyncOperation, IProgress<float> progress = null, PlayerLoopTiming timing = PlayerLoopTiming.Update, CancellationToken cancellationToken = default(CancellationToken))
|
||||||
{
|
{
|
||||||
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
Error.ThrowArgumentNullException(asyncOperation, nameof(asyncOperation));
|
||||||
|
if (asyncOperation.isDone) return UniTask.FromResult(asyncOperation.webRequest);
|
||||||
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellation, out var token), token);
|
return new UniTask<UnityWebRequest>(UnityWebRequestAsyncOperationConfiguredSource.Create(asyncOperation, timing, progress, cancellationToken, out var token), token);
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion
|
public struct UnityWebRequestAsyncOperationAwaiter : ICriticalNotifyCompletion
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
#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 Cysharp.Threading.Tasks.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using Cysharp.Threading.Tasks.Internal;
|
|
||||||
using Cysharp.Threading.Tasks.Linq;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Events;
|
using UnityEngine.Events;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
@@ -27,6 +26,21 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
|
return new UnityEventHandlerAsyncEnumerable(unityEvent, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static AsyncUnityEventHandler<T> GetAsyncEventHandler<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<T> OnInvokeAsync<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<T>(unityEvent, cancellationToken, true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<T> OnInvokeAsAsyncEnumerable<T>(this UnityEvent<T> unityEvent, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<T>(unityEvent, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
|
public static IAsyncClickEventHandler GetAsyncClickEventHandler(this Button button)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler(button.onClick, button.GetCancellationTokenOnDestroy(), false);
|
||||||
@@ -207,6 +221,36 @@ namespace Cysharp.Threading.Tasks
|
|||||||
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onEndEdit, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IAsyncValueChangedEventHandler<string> GetAsyncValueChangedEventHandler(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<string> OnValueChangedAsync(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy(), true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UniTask<string> OnValueChangedAsync(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new AsyncUnityEventHandler<string>(inputField.onValueChanged, cancellationToken, true).OnInvokeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, inputField.GetCancellationTokenOnDestroy());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IUniTaskAsyncEnumerable<string> OnValueChangedAsAsyncEnumerable(this InputField inputField, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return new UnityEventHandlerAsyncEnumerable<string>(inputField.onValueChanged, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
|
public static IAsyncValueChangedEventHandler<int> GetAsyncValueChangedEventHandler(this Dropdown dropdown)
|
||||||
{
|
{
|
||||||
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
return new AsyncUnityEventHandler<int>(dropdown.onValueChanged, dropdown.GetCancellationTokenOnDestroy(), false);
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
{
|
{
|
||||||
"name": "com.cysharp.unitask",
|
"name": "com.cysharp.unitask",
|
||||||
"displayName": "UniTask",
|
"displayName": "UniTask",
|
||||||
"version": "2.0.7-rc4",
|
"version": "2.0.10-rc7",
|
||||||
"unity": "2018.3",
|
"unity": "2018.3",
|
||||||
"description": "Provides an efficient async/await integration to Unity.",
|
"description": "Provides an efficient async/await integration to Unity.",
|
||||||
"keywords": ["async/await", "async", "Task", "UniTask"],
|
"keywords": [ "async/await", "async", "Task", "UniTask" ],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"category": "Task",
|
"category": "Task",
|
||||||
"dependencies": {}
|
"dependencies": {}
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ using Unity.Jobs;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.UI;
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
|
||||||
|
// using DG.Tweening;
|
||||||
|
|
||||||
public struct MyJob : IJob
|
public struct MyJob : IJob
|
||||||
{
|
{
|
||||||
public int loopCount;
|
public int loopCount;
|
||||||
@@ -35,8 +38,33 @@ public enum MyEnum
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public class SimplePresenter
|
||||||
|
{
|
||||||
|
// View
|
||||||
|
public UnityEngine.UI.InputField Input;
|
||||||
|
|
||||||
|
|
||||||
|
// Presenter
|
||||||
|
|
||||||
|
|
||||||
|
public SimplePresenter()
|
||||||
|
{
|
||||||
|
//Input.OnValueChangedAsAsyncEnumerable()
|
||||||
|
// .Queue()
|
||||||
|
// .SelectAwait(async x =>
|
||||||
|
// {
|
||||||
|
// await UniTask.Delay(TimeSpan.FromSeconds(1));
|
||||||
|
// return x;
|
||||||
|
// })
|
||||||
|
// .Select(x=> x.ToUpper())
|
||||||
|
// .BindTo(
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -87,7 +115,7 @@ public class SandboxMain : MonoBehaviour
|
|||||||
{
|
{
|
||||||
public Button okButton;
|
public Button okButton;
|
||||||
public Button cancelButton;
|
public Button cancelButton;
|
||||||
public Text text;
|
|
||||||
|
|
||||||
CancellationTokenSource cts;
|
CancellationTokenSource cts;
|
||||||
|
|
||||||
@@ -110,6 +138,48 @@ public class SandboxMain : MonoBehaviour
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class Model
|
||||||
|
{
|
||||||
|
// State<int> Hp { get; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Model()
|
||||||
|
{
|
||||||
|
// hp = new AsyncReactiveProperty<int>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//setHp = Hp.GetSetter();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Increment(int value)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// setHp(Hp.Value += value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Text text;
|
||||||
|
public Button button;
|
||||||
|
|
||||||
|
|
||||||
|
void Start2()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async UniTask RunStandardDelayAsync()
|
async UniTask RunStandardDelayAsync()
|
||||||
{
|
{
|
||||||
UnityEngine.Debug.Log("DEB");
|
UnityEngine.Debug.Log("DEB");
|
||||||
@@ -126,10 +196,6 @@ public class SandboxMain : MonoBehaviour
|
|||||||
|
|
||||||
var scheduled = job.Schedule();
|
var scheduled = job.Schedule();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
UnityEngine.Debug.Log("OK");
|
UnityEngine.Debug.Log("OK");
|
||||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||||
UnityEngine.Debug.Log("OK2");
|
UnityEngine.Debug.Log("OK2");
|
||||||
@@ -170,38 +236,71 @@ public class SandboxMain : MonoBehaviour
|
|||||||
Debug.Log("Done");
|
Debug.Log("Done");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start()
|
public int MyProperty { get; set; }
|
||||||
|
|
||||||
|
public class MyClass
|
||||||
{
|
{
|
||||||
//var rp = new AsyncReactiveProperty<int>(10);
|
public int MyProperty { get; set; }
|
||||||
|
}
|
||||||
//Running(rp).Forget();
|
|
||||||
|
|
||||||
//await UniTaskAsyncEnumerable.EveryUpdate().Take(10).ForEachAsync((x, i) => rp.Value = i);
|
|
||||||
|
|
||||||
//rp.Dispose();
|
|
||||||
|
|
||||||
//var channel = Channel.CreateSingleConsumerUnbounded<int>();
|
|
||||||
//Debug.Log("wait channel");
|
|
||||||
//await channel.Reader.ReadAllAsync(this.GetCancellationTokenOnDestroy()).ForEachAsync(_ => { });
|
|
||||||
|
|
||||||
|
|
||||||
var pubsub = new AsyncMessageBroker<int>();
|
void Start()
|
||||||
|
{
|
||||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("A:" + x)).Forget();
|
//UniTaskAsyncEnumerable.EveryValueChanged(mcc, x => x.MyProperty)
|
||||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("B:" + x)).Forget();
|
// .Do(_ => { }, () => Debug.Log("COMPLETED"))
|
||||||
|
// .ForEachAsync(x =>
|
||||||
|
// {
|
||||||
|
// Debug.Log("VALUE_CHANGED:" + x);
|
||||||
|
// })
|
||||||
|
// .Forget();
|
||||||
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
|
// DG.Tweening.Core.TweenerCore<int>
|
||||||
|
//okButton.GetComponent<RectTransform>().DOMoveX(10.2f, 30);
|
||||||
|
|
||||||
|
|
||||||
|
// DOTween.To(
|
||||||
|
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
//var tween = okButton.GetComponent<RectTransform>().DOLocalMoveX(100, 5.0f);
|
||||||
|
|
||||||
|
cancelButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||||
|
{
|
||||||
|
cts.Cancel();
|
||||||
|
}).Forget();
|
||||||
|
|
||||||
|
|
||||||
|
// await tween.ToUniTask(TweenCancelBehaviour.KillAndCancelAwait, cts.Token);
|
||||||
|
|
||||||
|
//tween.SetRecyclable(true);
|
||||||
|
|
||||||
|
Debug.Log("END");
|
||||||
|
|
||||||
|
// tween.Play();
|
||||||
|
|
||||||
|
// DOTween.
|
||||||
|
|
||||||
|
// DOVirtual.Float(0, 1, 1, x => { }).ToUniTask();
|
||||||
|
|
||||||
okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||||
{
|
{
|
||||||
|
|
||||||
Debug.Log("foo");
|
|
||||||
pubsub.Publish(i++);
|
|
||||||
|
|
||||||
|
|
||||||
}).Forget();
|
}).Forget();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
okButton.onClick.AddListener(UniTask.UnityAction(async () => await UniTask.Yield()));
|
||||||
|
}
|
||||||
|
|
||||||
|
async UniTaskVoid CloseAsync(CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
await UniTask.Yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
async UniTaskVoid Running(CancellationToken ct)
|
async UniTaskVoid Running(CancellationToken ct)
|
||||||
@@ -436,7 +535,7 @@ public class SandboxMain : MonoBehaviour
|
|||||||
|
|
||||||
public class ShowPlayerLoop
|
public class ShowPlayerLoop
|
||||||
{
|
{
|
||||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
// [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||||
static void Init()
|
static void Init()
|
||||||
{
|
{
|
||||||
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetDefaultPlayerLoop();
|
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetDefaultPlayerLoop();
|
||||||
|
|||||||
@@ -124,7 +124,6 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
||||||
await ToaruCoroutineEnumerator().ConfigureAwait(PlayerLoopTiming.PostLateUpdate);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
|
|||||||
@@ -124,7 +124,6 @@ namespace Cysharp.Threading.TasksTests
|
|||||||
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
public IEnumerator BothEnumeratorCheck() => UniTask.ToCoroutine(async () =>
|
||||||
{
|
{
|
||||||
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
await ToaruCoroutineEnumerator(); // wait 5 frame:)
|
||||||
await ToaruCoroutineEnumerator().ConfigureAwait(PlayerLoopTiming.PostLateUpdate);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
[UnityTest]
|
[UnityTest]
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
"UnityEditor.TestRunner",
|
"UnityEditor.TestRunner",
|
||||||
"UniTask.Tests",
|
"UniTask.Tests",
|
||||||
"UniTask",
|
"UniTask",
|
||||||
"Unity.ResourceManager"
|
"Unity.ResourceManager",
|
||||||
|
"DOTween.Modules"
|
||||||
],
|
],
|
||||||
"includePlatforms": [
|
"includePlatforms": [
|
||||||
"Editor"
|
"Editor"
|
||||||
@@ -14,7 +15,8 @@
|
|||||||
"allowUnsafeCode": false,
|
"allowUnsafeCode": false,
|
||||||
"overrideReferences": true,
|
"overrideReferences": true,
|
||||||
"precompiledReferences": [
|
"precompiledReferences": [
|
||||||
"nunit.framework.dll"
|
"nunit.framework.dll",
|
||||||
|
"DOTween.dll"
|
||||||
],
|
],
|
||||||
"autoReferenced": false,
|
"autoReferenced": false,
|
||||||
"defineConstraints": [
|
"defineConstraints": [
|
||||||
|
|||||||
@@ -4,14 +4,16 @@
|
|||||||
"UnityEngine.TestRunner",
|
"UnityEngine.TestRunner",
|
||||||
"UnityEditor.TestRunner",
|
"UnityEditor.TestRunner",
|
||||||
"UniTask",
|
"UniTask",
|
||||||
"Unity.ResourceManager"
|
"Unity.ResourceManager",
|
||||||
|
"DOTween.Modules"
|
||||||
],
|
],
|
||||||
"includePlatforms": [],
|
"includePlatforms": [],
|
||||||
"excludePlatforms": [],
|
"excludePlatforms": [],
|
||||||
"allowUnsafeCode": false,
|
"allowUnsafeCode": false,
|
||||||
"overrideReferences": true,
|
"overrideReferences": true,
|
||||||
"precompiledReferences": [
|
"precompiledReferences": [
|
||||||
"nunit.framework.dll"
|
"nunit.framework.dll",
|
||||||
|
"DOTween.dll"
|
||||||
],
|
],
|
||||||
"autoReferenced": false,
|
"autoReferenced": false,
|
||||||
"defineConstraints": [
|
"defineConstraints": [
|
||||||
|
|||||||
Reference in New Issue
Block a user