mirror of
https://github.com/Cysharp/UniTask.git
synced 2026-05-15 11:30:09 +00:00
Compare commits
17 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36d53a3bcb | ||
|
|
ea9e61c2e1 | ||
|
|
a52c26102b | ||
|
|
e31c87b8a8 | ||
|
|
cc165a6897 | ||
|
|
f99910d802 | ||
|
|
997b0b3710 | ||
|
|
ec7064083a | ||
|
|
07cccfddd6 | ||
|
|
f07527cd06 | ||
|
|
7b273c4bd1 | ||
|
|
d36e7987b3 | ||
|
|
bbd5686816 | ||
|
|
fb1152d8f4 | ||
|
|
7a306118f5 | ||
|
|
efaf3ee8f5 | ||
|
|
2e4fe90956 |
50
README.md
50
README.md
@@ -1,29 +1,30 @@
|
||||
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
||||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
||||
## Table of Contents
|
||||
|
||||
- [UniTask](#unitask)
|
||||
- [Getting started](#getting-started)
|
||||
- [`UniTask<T>`](#unitaskt)
|
||||
- [Cancellation and Exception handling](#cancellation-and-exception-handling)
|
||||
- [Progress](#progress)
|
||||
- [UniTaskTracker](#unitasktracker)
|
||||
- [Reusable Promises](#reusable-promises)
|
||||
- [awaitable Events](#awaitable-events)
|
||||
- [async void vs async UniTask/UniTaskVoid](#async-void-vs-async-unitaskunitaskvoid)
|
||||
- [For Unit Testing](#for-unit-testing)
|
||||
- [Method List](#method-list)
|
||||
- [UPM Package](#upm-package)
|
||||
- [License](#license)
|
||||
|
||||
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
||||
|
||||
UniTask
|
||||
===
|
||||
[](https://github.com/Cysharp/UniTask/actions) [](https://github.com/Cysharp/UniTask/releases)
|
||||
|
||||
Provides an efficient async/await integration to Unity.
|
||||
|
||||
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
||||
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
|
||||
## Table of Contents
|
||||
|
||||
- [Getting started](#getting-started)
|
||||
- [`UniTask<T>`](#unitaskt)
|
||||
- [Cancellation and Exception handling](#cancellation-and-exception-handling)
|
||||
- [Progress](#progress)
|
||||
- [UniTaskTracker](#unitasktracker)
|
||||
- [Reusable Promises](#reusable-promises)
|
||||
- [awaitable Events](#awaitable-events)
|
||||
- [async void vs async UniTask/UniTaskVoid](#async-void-vs-async-unitaskunitaskvoid)
|
||||
- [For Unit Testing](#for-unit-testing)
|
||||
- [Method List](#method-list)
|
||||
- [UPM Package](#upm-package)
|
||||
- [ECS, PlayerLoop](#ecs-playerloop)
|
||||
- [License](#license)
|
||||
|
||||
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
|
||||
|
||||
|
||||
Getting started
|
||||
---
|
||||
Install package(`UniRx.Async.unitypackage`) is available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page.
|
||||
@@ -372,6 +373,15 @@ or add `"com.cysharp.unitask": "https://github.com/Cysharp/UniTask.git?path=Asse
|
||||
|
||||
If you want to set a target version, UniTask is using `*.*.*` release tag so you can specify a version like `#1.3.0`. For example `https://github.com/Cysharp/UniTask.git?path=Assets/UniRx.Async#1.3.1`.
|
||||
|
||||
ECS, PlayerLoop
|
||||
---
|
||||
TODO:
|
||||
|
||||
```csharp
|
||||
var loop = PlayerLoop.GetCurrentPlayerLoop();
|
||||
PlayerLoopHelper.Initialize(ref loop);
|
||||
```
|
||||
|
||||
License
|
||||
---
|
||||
This library is under the MIT License.
|
||||
51
src/UniTask.NetCoreTests/CancellationTokenTest.cs
Normal file
51
src/UniTask.NetCoreTests/CancellationTokenTest.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests
|
||||
{
|
||||
public class CancellationTokenTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task WaitUntilCanceled()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
cts.CancelAfter(TimeSpan.FromSeconds(1.5));
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
await cts.Token.WaitUntilCanceled();
|
||||
|
||||
var elapsed = DateTime.UtcNow - now;
|
||||
|
||||
elapsed.Should().BeGreaterThan(TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AlreadyCanceled()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
cts.Cancel();
|
||||
|
||||
cts.Token.WaitUntilCanceled().GetAwaiter().IsCompleted.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void None()
|
||||
{
|
||||
CancellationToken.None.WaitUntilCanceled().GetAwaiter().IsCompleted.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
47
src/UniTask.NetCoreTests/DeferTest.cs
Normal file
47
src/UniTask.NetCoreTests/DeferTest.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Channels;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests
|
||||
{
|
||||
public class DeferTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task D()
|
||||
{
|
||||
var created = false;
|
||||
var v = UniTask.Defer(() => { created = true; return UniTask.Run(() => 10); });
|
||||
|
||||
created.Should().BeFalse();
|
||||
|
||||
var t = await v;
|
||||
|
||||
created.Should().BeTrue();
|
||||
|
||||
t.Should().Be(10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task D2()
|
||||
{
|
||||
var created = false;
|
||||
var v = UniTask.Defer(() => { created = true; return UniTask.Run(() => 10).AsUniTask(); });
|
||||
|
||||
created.Should().BeFalse();
|
||||
|
||||
await v;
|
||||
|
||||
created.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
78
src/UniTask.NetCoreTests/Linq/PulbishTest.cs
Normal file
78
src/UniTask.NetCoreTests/Linq/PulbishTest.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace NetCoreTests.Linq
|
||||
{
|
||||
public class PublishTest
|
||||
{
|
||||
[Fact]
|
||||
public async Task Normal()
|
||||
{
|
||||
var rp = new AsyncReactiveProperty<int>(1);
|
||||
|
||||
var multicast = rp.Publish();
|
||||
|
||||
var a = multicast.ToArrayAsync();
|
||||
var b = multicast.Take(2).ToArrayAsync();
|
||||
|
||||
var disp = multicast.Connect();
|
||||
|
||||
rp.Value = 2;
|
||||
|
||||
(await b).Should().BeEquivalentTo(1, 2);
|
||||
|
||||
var c = multicast.ToArrayAsync();
|
||||
|
||||
rp.Value = 3;
|
||||
rp.Value = 4;
|
||||
rp.Value = 5;
|
||||
|
||||
rp.Dispose();
|
||||
|
||||
(await a).Should().BeEquivalentTo(1, 2, 3, 4, 5);
|
||||
(await c).Should().BeEquivalentTo(3, 4, 5);
|
||||
|
||||
disp.Dispose();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Cancel()
|
||||
{
|
||||
var rp = new AsyncReactiveProperty<int>(1);
|
||||
|
||||
var multicast = rp.Publish();
|
||||
|
||||
var a = multicast.ToArrayAsync();
|
||||
var b = multicast.Take(2).ToArrayAsync();
|
||||
|
||||
var disp = multicast.Connect();
|
||||
|
||||
rp.Value = 2;
|
||||
|
||||
(await b).Should().BeEquivalentTo(1, 2);
|
||||
|
||||
var c = multicast.ToArrayAsync();
|
||||
|
||||
rp.Value = 3;
|
||||
|
||||
disp.Dispose();
|
||||
|
||||
rp.Value = 4;
|
||||
rp.Value = 5;
|
||||
|
||||
rp.Dispose();
|
||||
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(async () => await a);
|
||||
await Assert.ThrowsAsync<OperationCanceledException>(async () => await c);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using FluentAssertions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
@@ -40,5 +41,66 @@ namespace NetCoreTests.Linq
|
||||
|
||||
(await xs).Should().BeEquivalentTo(1, 2, 3, 4);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task TakeUntil()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var rp = new AsyncReactiveProperty<int>(1);
|
||||
|
||||
var xs = rp.TakeUntilCanceled(cts.Token).ToArrayAsync();
|
||||
|
||||
var c = CancelAsync();
|
||||
|
||||
await c;
|
||||
var foo = await xs;
|
||||
|
||||
foo.Should().BeEquivalentTo(new[] { 1, 10, 20 });
|
||||
|
||||
async Task CancelAsync()
|
||||
{
|
||||
rp.Value = 10;
|
||||
await Task.Yield();
|
||||
rp.Value = 20;
|
||||
await Task.Yield();
|
||||
cts.Cancel();
|
||||
rp.Value = 30;
|
||||
await Task.Yield();
|
||||
rp.Value = 40;
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SkipUntil()
|
||||
{
|
||||
var cts = new CancellationTokenSource();
|
||||
|
||||
var rp = new AsyncReactiveProperty<int>(1);
|
||||
|
||||
var xs = rp.SkipUntilCanceled(cts.Token).ToArrayAsync();
|
||||
|
||||
var c = CancelAsync();
|
||||
|
||||
await c;
|
||||
var foo = await xs;
|
||||
|
||||
foo.Should().BeEquivalentTo(new[] { 30, 40 });
|
||||
|
||||
async Task CancelAsync()
|
||||
{
|
||||
rp.Value = 10;
|
||||
await Task.Yield();
|
||||
rp.Value = 20;
|
||||
await Task.Yield();
|
||||
cts.Cancel();
|
||||
rp.Value = 30;
|
||||
await Task.Yield();
|
||||
rp.Value = 40;
|
||||
|
||||
rp.Dispose(); // complete.
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,12 +4,13 @@ using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public interface IAsyncReadOnlyReactiveProperty<T> : IUniTaskAsyncEnumerable<T>
|
||||
public interface IReadOnlyAsyncReactiveProperty<T> : IUniTaskAsyncEnumerable<T>
|
||||
{
|
||||
T Value { get; }
|
||||
IUniTaskAsyncEnumerable<T> WithoutCurrent();
|
||||
}
|
||||
|
||||
public interface IAsyncReactiveProperty<T> : IAsyncReadOnlyReactiveProperty<T>
|
||||
public interface IAsyncReactiveProperty<T> : IReadOnlyAsyncReactiveProperty<T>
|
||||
{
|
||||
new T Value { get; set; }
|
||||
}
|
||||
@@ -55,7 +56,7 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
triggerEvent.SetCanceled(CancellationToken.None);
|
||||
triggerEvent.SetCompleted();
|
||||
}
|
||||
|
||||
class WithoutCurrentEnumerable : IUniTaskAsyncEnumerable<T>
|
||||
@@ -73,7 +74,7 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, IResolveCancelPromise<T>
|
||||
sealed class Enumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
@@ -127,17 +128,25 @@ namespace Cysharp.Threading.Tasks
|
||||
return default;
|
||||
}
|
||||
|
||||
public bool TrySetResult(T value)
|
||||
public void OnNext(T value)
|
||||
{
|
||||
this.value = value;
|
||||
completionSource.TrySetResult(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TrySetCanceled(CancellationToken cancellationToken = default)
|
||||
public void OnCanceled(CancellationToken cancellationToken)
|
||||
{
|
||||
DisposeAsync().Forget();
|
||||
return true;
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
|
||||
public void OnError(Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
@@ -9,15 +10,15 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
static readonly Action<object> cancellationTokenCallback = Callback;
|
||||
|
||||
public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cts)
|
||||
public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken)
|
||||
{
|
||||
if (cts.IsCancellationRequested)
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return (UniTask.FromCanceled(cts), default(CancellationTokenRegistration));
|
||||
return (UniTask.FromCanceled(cancellationToken), default(CancellationTokenRegistration));
|
||||
}
|
||||
|
||||
var promise = new UniTaskCompletionSource();
|
||||
return (promise.Task, cts.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise));
|
||||
return (promise.Task, cancellationToken.RegisterWithoutCaptureExecutionContext(cancellationTokenCallback, promise));
|
||||
}
|
||||
|
||||
static void Callback(object state)
|
||||
@@ -26,6 +27,11 @@ namespace Cysharp.Threading.Tasks
|
||||
promise.TrySetResult();
|
||||
}
|
||||
|
||||
public static CancellationTokenAwaitable WaitUntilCanceled(this CancellationToken cancellationToken)
|
||||
{
|
||||
return new CancellationTokenAwaitable(cancellationToken);
|
||||
}
|
||||
|
||||
public static CancellationTokenRegistration RegisterWithoutCaptureExecutionContext(this CancellationToken cancellationToken, Action callback)
|
||||
{
|
||||
var restoreFlow = false;
|
||||
@@ -70,5 +76,46 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct CancellationTokenAwaitable
|
||||
{
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public CancellationTokenAwaitable(CancellationToken cancellationToken)
|
||||
{
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public Awaiter GetAwaiter()
|
||||
{
|
||||
return new Awaiter(cancellationToken);
|
||||
}
|
||||
|
||||
public struct Awaiter : ICriticalNotifyCompletion
|
||||
{
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
public Awaiter(CancellationToken cancellationToken)
|
||||
{
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public bool IsCompleted => !cancellationToken.CanBeCanceled || cancellationToken.IsCancellationRequested;
|
||||
|
||||
public void GetResult()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnCompleted(Action continuation)
|
||||
{
|
||||
UnsafeOnCompleted(continuation);
|
||||
}
|
||||
|
||||
public void UnsafeOnCompleted(Action continuation)
|
||||
{
|
||||
cancellationToken.RegisterWithoutCaptureExecutionContext(continuation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -91,7 +91,8 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
readonly Queue<T> items;
|
||||
readonly SingleConsumerUnboundedChannelReader readerSource;
|
||||
readonly UniTaskCompletionSource completedTask;
|
||||
UniTaskCompletionSource completedTaskSource;
|
||||
UniTask completedTask;
|
||||
|
||||
Exception completionError;
|
||||
bool closed;
|
||||
@@ -99,7 +100,6 @@ namespace Cysharp.Threading.Tasks
|
||||
public SingleConsumerUnboundedChannel()
|
||||
{
|
||||
items = new Queue<T>();
|
||||
completedTask = new UniTaskCompletionSource();
|
||||
Writer = new SingleConsumerUnboundedChannelWriter(this);
|
||||
readerSource = new SingleConsumerUnboundedChannelReader(this);
|
||||
Reader = readerSource;
|
||||
@@ -146,11 +146,25 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
if (error == null)
|
||||
{
|
||||
parent.completedTask.TrySetResult();
|
||||
if (parent.completedTaskSource != null)
|
||||
{
|
||||
parent.completedTaskSource.TrySetResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask = UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask.TrySetException(error);
|
||||
if (parent.completedTaskSource != null)
|
||||
{
|
||||
parent.completedTaskSource.TrySetException(error);
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask = UniTask.FromException(error);
|
||||
}
|
||||
}
|
||||
|
||||
if (waiting)
|
||||
@@ -179,9 +193,25 @@ namespace Cysharp.Threading.Tasks
|
||||
public SingleConsumerUnboundedChannelReader(SingleConsumerUnboundedChannel<T> parent)
|
||||
{
|
||||
this.parent = parent;
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 4);
|
||||
}
|
||||
|
||||
public override UniTask Completion => parent.completedTask.Task;
|
||||
public override UniTask Completion
|
||||
{
|
||||
get
|
||||
{
|
||||
if (parent.completedTaskSource != null) return parent.completedTaskSource.Task;
|
||||
|
||||
if (parent.closed)
|
||||
{
|
||||
return parent.completedTask;
|
||||
}
|
||||
|
||||
parent.completedTaskSource = new UniTaskCompletionSource();
|
||||
return parent.completedTaskSource.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public override bool TryRead(out T item)
|
||||
{
|
||||
@@ -196,11 +226,25 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
if (parent.completionError != null)
|
||||
{
|
||||
parent.completedTask.TrySetException(parent.completionError);
|
||||
if (parent.completedTaskSource != null)
|
||||
{
|
||||
parent.completedTaskSource.TrySetException(parent.completionError);
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask = UniTask.FromException(parent.completionError);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask.TrySetResult();
|
||||
if (parent.completedTaskSource != null)
|
||||
{
|
||||
parent.completedTaskSource.TrySetResult();
|
||||
}
|
||||
else
|
||||
{
|
||||
parent.completedTask = UniTask.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -262,6 +306,7 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
public void SingalCancellation(CancellationToken cancellationToken)
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
@@ -269,10 +314,12 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
if (error != null)
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.TrySetException(error);
|
||||
}
|
||||
else
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
core.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,12 @@ namespace Cysharp.Threading.Tasks
|
||||
IUniTaskOrderedAsyncEnumerable<TElement> CreateOrderedEnumerable<TKey>(Func<TElement, CancellationToken, UniTask<TKey>> keySelector, IComparer<TKey> comparer, bool descending);
|
||||
}
|
||||
|
||||
public interface IConnectableUniTaskAsyncEnumerable<out T> : IUniTaskAsyncEnumerable<T>
|
||||
{
|
||||
IDisposable Connect();
|
||||
}
|
||||
|
||||
// don't use AsyncGrouping.
|
||||
//public interface IUniTaskAsyncGrouping<out TKey, out TElement> : IUniTaskAsyncEnumerable<TElement>
|
||||
//{
|
||||
// TKey Key { get; }
|
||||
|
||||
@@ -65,6 +65,8 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.element = element;
|
||||
this.state = append ? State.RequireAppend : State.RequirePrepend;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -136,6 +138,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 4);
|
||||
}
|
||||
|
||||
// abstract
|
||||
@@ -178,6 +179,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||
public virtual UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
@@ -204,6 +206,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 4);
|
||||
}
|
||||
|
||||
// abstract
|
||||
@@ -399,6 +402,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
// if require additional resource to dispose, override and call base.DisposeAsync.
|
||||
public virtual UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -61,6 +61,8 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public IList<TSource> Current { get; private set; }
|
||||
@@ -167,6 +169,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
@@ -217,6 +220,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.count = count;
|
||||
this.skip = skip;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public IList<TSource> Current { get; private set; }
|
||||
@@ -329,6 +333,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -57,6 +57,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.second = second;
|
||||
this.cancellationToken = cancellationToken;
|
||||
this.iteratingState = IteratingState.IteratingFirst;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -150,6 +151,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -63,6 +63,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
this.iteratingState = IteratingState.Empty;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -128,6 +129,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -124,6 +124,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.onError = onError;
|
||||
this.onCompleted = onCompleted;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -244,6 +245,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -22,6 +22,22 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
return Cysharp.Threading.Tasks.Linq.ForEach.ForEachAsync(source, action, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
|
||||
[Obsolete("Use ForEachAwaitAsync instead.", true)]
|
||||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
|
||||
public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotSupportedException("Use ForEachAwaitAsync instead.");
|
||||
}
|
||||
|
||||
/// <summary>Obsolete(Error), Use Use ForEachAwaitAsync instead.</summary>
|
||||
[Obsolete("Use ForEachAwaitAsync instead.", true)]
|
||||
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
|
||||
public static UniTask ForEachAsync<T>(this IUniTaskAsyncEnumerable<T> source, Func<T, int, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotSupportedException("Use ForEachAwaitAsync instead.");
|
||||
}
|
||||
|
||||
public static UniTask ForEachAwaitAsync<TSource>(this IUniTaskAsyncEnumerable<TSource> source, Func<TSource, UniTask> action, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
@@ -255,6 +255,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
@@ -313,6 +314,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
@@ -364,6 +366,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -423,6 +426,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
@@ -470,6 +474,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
@@ -528,6 +533,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
@@ -582,6 +588,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -661,6 +668,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
@@ -708,6 +716,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.elementSelector = elementSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public IGrouping<TKey, TElement> Current { get; private set; }
|
||||
@@ -766,6 +775,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
@@ -820,6 +830,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -899,6 +910,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (groupEnumerator != null)
|
||||
{
|
||||
groupEnumerator.Dispose();
|
||||
|
||||
@@ -129,6 +129,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -208,6 +209,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
@@ -273,6 +275,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -401,6 +404,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
@@ -466,6 +470,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -594,6 +599,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -131,6 +131,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -248,6 +249,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
@@ -321,6 +323,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -476,6 +479,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
@@ -549,6 +553,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.resultSelector = resultSelector;
|
||||
this.comparer = comparer;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -704,6 +709,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (valueEnumerator != null)
|
||||
{
|
||||
valueEnumerator.Dispose();
|
||||
|
||||
@@ -422,6 +422,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
this.parent = parent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TElement Current { get; private set; }
|
||||
@@ -477,6 +478,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
171
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Publish.cs
Normal file
171
src/UniTask/Assets/Plugins/UniTask/Runtime/Linq/Publish.cs
Normal file
@@ -0,0 +1,171 @@
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IConnectableUniTaskAsyncEnumerable<TSource> Publish<TSource>(this IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new Publish<TSource>(source);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class Publish<TSource> : IConnectableUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly CancellationTokenSource cancellationTokenSource;
|
||||
|
||||
TriggerEvent<TSource> trigger;
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
IDisposable connectedDisposable;
|
||||
bool isCompleted;
|
||||
|
||||
public Publish(IUniTaskAsyncEnumerable<TSource> source)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationTokenSource = new CancellationTokenSource();
|
||||
}
|
||||
|
||||
public IDisposable Connect()
|
||||
{
|
||||
if (connectedDisposable != null) return connectedDisposable;
|
||||
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
ConsumeEnumerator().Forget();
|
||||
|
||||
connectedDisposable = new ConnectDisposable(cancellationTokenSource);
|
||||
return connectedDisposable;
|
||||
}
|
||||
|
||||
async UniTaskVoid ConsumeEnumerator()
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
while (await enumerator.MoveNextAsync())
|
||||
{
|
||||
trigger.SetResult(enumerator.Current);
|
||||
}
|
||||
trigger.SetCompleted();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
trigger.SetError(ex);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
isCompleted = true;
|
||||
await enumerator.DisposeAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new _Publish(this, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class ConnectDisposable : IDisposable
|
||||
{
|
||||
readonly CancellationTokenSource cancellationTokenSource;
|
||||
|
||||
public ConnectDisposable(CancellationTokenSource cancellationTokenSource)
|
||||
{
|
||||
this.cancellationTokenSource = cancellationTokenSource;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.cancellationTokenSource.Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
sealed class _Publish : MoveNextSource, IUniTaskAsyncEnumerator<TSource>, ITriggerHandler<TSource>
|
||||
{
|
||||
static readonly Action<object> CancelDelegate = OnCanceled;
|
||||
|
||||
readonly Publish<TSource> parent;
|
||||
CancellationToken cancellationToken;
|
||||
CancellationTokenRegistration cancellationTokenRegistration;
|
||||
bool isDisposed;
|
||||
|
||||
public _Publish(Publish<TSource> parent, CancellationToken cancellationToken)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested) return;
|
||||
|
||||
this.parent = parent;
|
||||
this.cancellationToken = cancellationToken;
|
||||
|
||||
if (cancellationToken.CanBeCanceled)
|
||||
{
|
||||
this.cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(CancelDelegate, this);
|
||||
}
|
||||
|
||||
parent.trigger.Add(this);
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
if (parent.isCompleted) return CompletedTasks.False;
|
||||
|
||||
completionSource.Reset();
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
static void OnCanceled(object state)
|
||||
{
|
||||
var self = (_Publish)state;
|
||||
self.completionSource.TrySetCanceled(self.cancellationToken);
|
||||
self.DisposeAsync().Forget();
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
if (!isDisposed)
|
||||
{
|
||||
isDisposed = true;
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration.Dispose();
|
||||
parent.trigger.Remove(this);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public void OnNext(TSource value)
|
||||
{
|
||||
Current = value;
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
|
||||
public void OnCanceled(CancellationToken cancellationToken)
|
||||
{
|
||||
completionSource.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
|
||||
public void OnError(Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 93c684d1e88c09d4e89b79437d97b810
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -33,6 +33,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
Channel<TSource> channel;
|
||||
IUniTaskAsyncEnumerator<TSource> channelEnumerator;
|
||||
IUniTaskAsyncEnumerator<TSource> sourceEnumerator;
|
||||
bool channelClosed;
|
||||
|
||||
public _Queue(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -53,13 +54,13 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
channelEnumerator = channel.Reader.ReadAllAsync().GetAsyncEnumerator(cancellationToken);
|
||||
|
||||
ConsumeAll(sourceEnumerator, channel).Forget();
|
||||
ConsumeAll(this, sourceEnumerator, channel).Forget();
|
||||
}
|
||||
|
||||
return channelEnumerator.MoveNextAsync();
|
||||
}
|
||||
|
||||
static async UniTaskVoid ConsumeAll(IUniTaskAsyncEnumerator<TSource> enumerator, ChannelWriter<TSource> writer)
|
||||
static async UniTaskVoid ConsumeAll(_Queue self, IUniTaskAsyncEnumerator<TSource> enumerator, ChannelWriter<TSource> writer)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -75,6 +76,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
}
|
||||
finally
|
||||
{
|
||||
self.channelClosed = true;
|
||||
await enumerator.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -89,6 +91,12 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
await channelEnumerator.DisposeAsync();
|
||||
}
|
||||
|
||||
if (!channelClosed)
|
||||
{
|
||||
channelClosed = true;
|
||||
channel.Writer.TryComplete(new OperationCanceledException());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -69,6 +70,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,6 +160,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.selector2 = selector2;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -324,6 +325,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (selectedEnumerator != null)
|
||||
{
|
||||
await selectedEnumerator.DisposeAsync();
|
||||
@@ -398,6 +400,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.selector2 = selector2;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -598,6 +601,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (selectedEnumerator != null)
|
||||
{
|
||||
await selectedEnumerator.DisposeAsync();
|
||||
@@ -672,6 +676,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.selector2 = selector2;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -872,6 +877,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (selectedEnumerator != null)
|
||||
{
|
||||
await selectedEnumerator.DisposeAsync();
|
||||
|
||||
@@ -56,6 +56,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -146,6 +147,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> SkipUntilCanceled<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new SkipUntilCanceled<TSource>(source, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class SkipUntilCanceled<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly CancellationToken cancellationToken;
|
||||
|
||||
public SkipUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new _SkipUntilCanceled(source, this.cancellationToken, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class _SkipUntilCanceled : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
CancellationToken cancellationToken1;
|
||||
CancellationToken cancellationToken2;
|
||||
|
||||
bool isCanceled;
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
bool continueNext;
|
||||
|
||||
public _SkipUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken1 = cancellationToken1;
|
||||
this.cancellationToken2 = cancellationToken2;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
if (cancellationToken1.IsCancellationRequested) isCanceled = true;
|
||||
if (cancellationToken2.IsCancellationRequested) isCanceled = true;
|
||||
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken2); // use only AsyncEnumerator provided token.
|
||||
}
|
||||
completionSource.Reset();
|
||||
SourceMoveNext();
|
||||
return new UniTask<bool>(this, completionSource.Version);
|
||||
}
|
||||
|
||||
void SourceMoveNext()
|
||||
{
|
||||
try
|
||||
{
|
||||
LOOP:
|
||||
awaiter = enumerator.MoveNextAsync().GetAwaiter();
|
||||
if (awaiter.IsCompleted)
|
||||
{
|
||||
continueNext = true;
|
||||
MoveNextCore(this);
|
||||
if (continueNext)
|
||||
{
|
||||
continueNext = false;
|
||||
goto LOOP;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
awaiter.SourceOnCompleted(MoveNextCoreDelegate, this);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
static void MoveNextCore(object state)
|
||||
{
|
||||
var self = (_SkipUntilCanceled)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
AGAIN:
|
||||
|
||||
if (self.isCanceled)
|
||||
{
|
||||
self.continueNext = false;
|
||||
self.Current = self.enumerator.Current;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (self.cancellationToken1.IsCancellationRequested) self.isCanceled = true;
|
||||
if (self.cancellationToken2.IsCancellationRequested) self.isCanceled = true;
|
||||
|
||||
if (self.isCanceled) goto AGAIN;
|
||||
|
||||
if (!self.continueNext)
|
||||
{
|
||||
self.SourceMoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
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: 4b1a778aef7150d47b93a49aa1bc34ae
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -47,6 +47,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -111,6 +112,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -57,6 +57,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.source = source;
|
||||
this.count = count;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
@@ -162,6 +163,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks.Linq
|
||||
{
|
||||
public static partial class UniTaskAsyncEnumerable
|
||||
{
|
||||
public static IUniTaskAsyncEnumerable<TSource> TakeUntilCanceled<TSource>(this IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
Error.ThrowArgumentNullException(source, nameof(source));
|
||||
|
||||
return new TakeUntilCanceled<TSource>(source, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class TakeUntilCanceled<TSource> : IUniTaskAsyncEnumerable<TSource>
|
||||
{
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
readonly CancellationToken cancellationToken;
|
||||
|
||||
public TakeUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerator<TSource> GetAsyncEnumerator(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return new _TakeUntilCanceled(source, this.cancellationToken, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class _TakeUntilCanceled : MoveNextSource, IUniTaskAsyncEnumerator<TSource>
|
||||
{
|
||||
static readonly Action<object> CancelDelegate1 = OnCanceled1;
|
||||
static readonly Action<object> CancelDelegate2 = OnCanceled2;
|
||||
static readonly Action<object> MoveNextCoreDelegate = MoveNextCore;
|
||||
|
||||
readonly IUniTaskAsyncEnumerable<TSource> source;
|
||||
CancellationToken cancellationToken1;
|
||||
CancellationToken cancellationToken2;
|
||||
CancellationTokenRegistration cancellationTokenRegistration1;
|
||||
CancellationTokenRegistration cancellationTokenRegistration2;
|
||||
|
||||
bool isCanceled;
|
||||
IUniTaskAsyncEnumerator<TSource> enumerator;
|
||||
UniTask<bool>.Awaiter awaiter;
|
||||
|
||||
public _TakeUntilCanceled(IUniTaskAsyncEnumerable<TSource> source, CancellationToken cancellationToken1, CancellationToken cancellationToken2)
|
||||
{
|
||||
this.source = source;
|
||||
this.cancellationToken1 = cancellationToken1;
|
||||
this.cancellationToken2 = cancellationToken2;
|
||||
|
||||
if (cancellationToken1.CanBeCanceled)
|
||||
{
|
||||
this.cancellationTokenRegistration1 = cancellationToken1.RegisterWithoutCaptureExecutionContext(CancelDelegate1, this);
|
||||
}
|
||||
|
||||
if (cancellationToken1 != cancellationToken2 && cancellationToken2.CanBeCanceled)
|
||||
{
|
||||
this.cancellationTokenRegistration2 = cancellationToken2.RegisterWithoutCaptureExecutionContext(CancelDelegate2, this);
|
||||
}
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TSource Current { get; private set; }
|
||||
|
||||
public UniTask<bool> MoveNextAsync()
|
||||
{
|
||||
if (cancellationToken1.IsCancellationRequested) isCanceled = true;
|
||||
if (cancellationToken2.IsCancellationRequested) isCanceled = true;
|
||||
|
||||
if (enumerator == null)
|
||||
{
|
||||
enumerator = source.GetAsyncEnumerator(cancellationToken2); // use only AsyncEnumerator provided token.
|
||||
}
|
||||
|
||||
if (isCanceled) return CompletedTasks.False;
|
||||
|
||||
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 = (_TakeUntilCanceled)state;
|
||||
|
||||
if (self.TryGetResult(self.awaiter, out var result))
|
||||
{
|
||||
if (result)
|
||||
{
|
||||
if (self.isCanceled)
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
self.Current = self.enumerator.Current;
|
||||
self.completionSource.TrySetResult(true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void OnCanceled1(object state)
|
||||
{
|
||||
var self = (_TakeUntilCanceled)state;
|
||||
if (!self.isCanceled)
|
||||
{
|
||||
self.cancellationTokenRegistration2.Dispose();
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
static void OnCanceled2(object state)
|
||||
{
|
||||
var self = (_TakeUntilCanceled)state;
|
||||
if (!self.isCanceled)
|
||||
{
|
||||
self.cancellationTokenRegistration1.Dispose();
|
||||
self.completionSource.TrySetResult(false);
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
cancellationTokenRegistration1.Dispose();
|
||||
cancellationTokenRegistration2.Dispose();
|
||||
if (enumerator != null)
|
||||
{
|
||||
return enumerator.DisposeAsync();
|
||||
}
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e82f498cf3a1df04cbf646773fc11319
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -84,6 +84,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.second = second;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -181,6 +182,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (firstEnumerator != null)
|
||||
{
|
||||
await firstEnumerator.DisposeAsync();
|
||||
@@ -236,6 +238,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.second = second;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -351,6 +354,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (firstEnumerator != null)
|
||||
{
|
||||
await firstEnumerator.DisposeAsync();
|
||||
@@ -406,6 +410,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
this.second = second;
|
||||
this.resultSelector = resultSelector;
|
||||
this.cancellationToken = cancellationToken;
|
||||
TaskTracker.TrackActiveTask(this, 3);
|
||||
}
|
||||
|
||||
public TResult Current { get; private set; }
|
||||
@@ -521,6 +526,7 @@ namespace Cysharp.Threading.Tasks.Linq
|
||||
|
||||
public async UniTask DisposeAsync()
|
||||
{
|
||||
TaskTracker.RemoveTracking(this);
|
||||
if (firstEnumerator != null)
|
||||
{
|
||||
await firstEnumerator.DisposeAsync();
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
#if NET_4_6 || NET_STANDARD_2_0 || CSHARP_7_OR_LATER
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks.Internal;
|
||||
|
||||
@@ -21,7 +18,11 @@ namespace Cysharp.Threading.Tasks
|
||||
public static IProgress<T> CreateOnlyValueChanged<T>(Action<T> handler, IEqualityComparer<T> comparer = null)
|
||||
{
|
||||
if (handler == null) return NullProgress<T>.Instance;
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
return new OnlyValueChangedProgress<T>(handler, comparer ?? UnityEqualityComparer.GetDefault<T>());
|
||||
#else
|
||||
return new OnlyValueChangedProgress<T>(handler, comparer ?? EqualityComparer<T>.Default);
|
||||
#endif
|
||||
}
|
||||
|
||||
sealed class NullProgress<T> : IProgress<T>
|
||||
@@ -83,6 +84,4 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
@@ -4,26 +4,26 @@ using System.Threading;
|
||||
|
||||
namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
public interface ITriggerEvent<T>
|
||||
public interface ITriggerHandler<T>
|
||||
{
|
||||
void SetResult(T value);
|
||||
void SetCanceled(CancellationToken cancellationToken);
|
||||
void Add(IResolveCancelPromise<T> handler);
|
||||
void Remove(IResolveCancelPromise<T> handler);
|
||||
void OnNext(T value);
|
||||
void OnError(Exception ex);
|
||||
void OnCompleted();
|
||||
void OnCanceled(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
// be careful to use, itself is struct.
|
||||
public struct TriggerEvent<T> : ITriggerEvent<T>
|
||||
public struct TriggerEvent<T>
|
||||
{
|
||||
// optimize: many cases, handler is single.
|
||||
IResolveCancelPromise<T> singleHandler;
|
||||
ITriggerHandler<T> singleHandler;
|
||||
|
||||
IResolveCancelPromise<T>[] handlers;
|
||||
ITriggerHandler<T>[] handlers;
|
||||
|
||||
// when running(in TrySetResult), does not add immediately.
|
||||
// when running(in TrySetResult), does not add immediately(trampoline).
|
||||
bool isRunning;
|
||||
IResolveCancelPromise<T> waitHandler;
|
||||
MinimumQueue<IResolveCancelPromise<T>> waitQueue;
|
||||
ITriggerHandler<T> waitHandler;
|
||||
MinimumQueue<ITriggerHandler<T>> waitQueue;
|
||||
|
||||
public void SetResult(T value)
|
||||
{
|
||||
@@ -33,7 +33,7 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
try
|
||||
{
|
||||
singleHandler.TrySetResult(value);
|
||||
singleHandler.OnNext(value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -53,7 +53,7 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
try
|
||||
{
|
||||
handlers[i].TrySetResult(value);
|
||||
handlers[i].OnNext(value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -94,7 +94,7 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
try
|
||||
{
|
||||
((ICancelPromise)singleHandler).TrySetCanceled(cancellationToken);
|
||||
(singleHandler).OnCanceled(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -114,7 +114,7 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
try
|
||||
{
|
||||
((ICancelPromise)handlers[i]).TrySetCanceled(cancellationToken);
|
||||
(handlers[i]).OnCanceled(cancellationToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -147,7 +147,129 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(IResolveCancelPromise<T> handler)
|
||||
public void SetCompleted()
|
||||
{
|
||||
isRunning = true;
|
||||
|
||||
if (singleHandler != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
(singleHandler).OnCompleted();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
UnityEngine.Debug.LogException(ex);
|
||||
#else
|
||||
Console.WriteLine(ex);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (handlers != null)
|
||||
{
|
||||
for (int i = 0; i < handlers.Length; i++)
|
||||
{
|
||||
if (handlers[i] != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
(handlers[i]).OnCompleted();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
UnityEngine.Debug.LogException(ex);
|
||||
#else
|
||||
Console.WriteLine(ex);
|
||||
#endif
|
||||
handlers[i] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isRunning = false;
|
||||
|
||||
if (waitHandler != null)
|
||||
{
|
||||
var h = waitHandler;
|
||||
waitHandler = null;
|
||||
Add(h);
|
||||
}
|
||||
|
||||
if (waitQueue != null)
|
||||
{
|
||||
while (waitQueue.Count != 0)
|
||||
{
|
||||
Add(waitQueue.Dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void SetError(Exception exception)
|
||||
{
|
||||
isRunning = true;
|
||||
|
||||
if (singleHandler != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
singleHandler.OnError(exception);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
UnityEngine.Debug.LogException(ex);
|
||||
#else
|
||||
Console.WriteLine(ex);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
if (handlers != null)
|
||||
{
|
||||
for (int i = 0; i < handlers.Length; i++)
|
||||
{
|
||||
if (handlers[i] != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
handlers[i].OnError(exception);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
handlers[i] = null;
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
UnityEngine.Debug.LogException(ex);
|
||||
#else
|
||||
Console.WriteLine(ex);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isRunning = false;
|
||||
|
||||
if (waitHandler != null)
|
||||
{
|
||||
var h = waitHandler;
|
||||
waitHandler = null;
|
||||
Add(h);
|
||||
}
|
||||
|
||||
if (waitQueue != null)
|
||||
{
|
||||
while (waitQueue.Count != 0)
|
||||
{
|
||||
Add(waitQueue.Dequeue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (isRunning)
|
||||
{
|
||||
@@ -159,7 +281,7 @@ namespace Cysharp.Threading.Tasks
|
||||
|
||||
if (waitQueue == null)
|
||||
{
|
||||
waitQueue = new MinimumQueue<IResolveCancelPromise<T>>(4);
|
||||
waitQueue = new MinimumQueue<ITriggerHandler<T>>(4);
|
||||
}
|
||||
waitQueue.Enqueue(handler);
|
||||
return;
|
||||
@@ -173,7 +295,7 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
if (handlers == null)
|
||||
{
|
||||
handlers = new IResolveCancelPromise<T>[4];
|
||||
handlers = new ITriggerHandler<T>[4];
|
||||
}
|
||||
|
||||
// check empty
|
||||
@@ -195,15 +317,15 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
static void EnsureCapacity(ref IResolveCancelPromise<T>[] array)
|
||||
static void EnsureCapacity(ref ITriggerHandler<T>[] array)
|
||||
{
|
||||
var newSize = array.Length * 2;
|
||||
var newArray = new IResolveCancelPromise<T>[newSize];
|
||||
var newArray = new ITriggerHandler<T>[newSize];
|
||||
Array.Copy(array, 0, newArray, 0, array.Length);
|
||||
array = newArray;
|
||||
}
|
||||
|
||||
public void Remove(IResolveCancelPromise<T> handler)
|
||||
public void Remove(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (singleHandler == handler)
|
||||
{
|
||||
|
||||
@@ -24,10 +24,10 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
if (calledDestroy) return;
|
||||
calledDestroy = true;
|
||||
|
||||
triggerEvent.SetCanceled(CancellationToken.None);
|
||||
triggerEvent.SetCompleted();
|
||||
}
|
||||
|
||||
internal void AddHandler(IResolveCancelPromise<T> handler)
|
||||
internal void AddHandler(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (!calledAwake)
|
||||
{
|
||||
@@ -37,7 +37,7 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
triggerEvent.Add(handler);
|
||||
}
|
||||
|
||||
internal void RemoveHandler(IResolveCancelPromise<T> handler)
|
||||
internal void RemoveHandler(ITriggerHandler<T> handler)
|
||||
{
|
||||
if (!calledAwake)
|
||||
{
|
||||
@@ -57,7 +57,7 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
return new AsyncTriggerEnumerator(this, cancellationToken);
|
||||
}
|
||||
|
||||
sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, IResolveCancelPromise<T>
|
||||
sealed class AsyncTriggerEnumerator : MoveNextSource, IUniTaskAsyncEnumerator<T>, ITriggerHandler<T>
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
@@ -73,15 +73,25 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
this.cancellationToken = cancellationToken;
|
||||
}
|
||||
|
||||
public bool TrySetCanceled(CancellationToken cancellationToken = default)
|
||||
public void OnCanceled(CancellationToken cancellationToken = default)
|
||||
{
|
||||
return completionSource.TrySetCanceled(cancellationToken);
|
||||
completionSource.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
public bool TrySetResult(T value)
|
||||
public void OnNext(T value)
|
||||
{
|
||||
Current = value;
|
||||
return completionSource.TrySetResult(true);
|
||||
completionSource.TrySetResult(true);
|
||||
}
|
||||
|
||||
public void OnCompleted()
|
||||
{
|
||||
completionSource.TrySetResult(false);
|
||||
}
|
||||
|
||||
public void OnError(Exception ex)
|
||||
{
|
||||
completionSource.TrySetException(ex);
|
||||
}
|
||||
|
||||
static void CancellationCallback(object state)
|
||||
@@ -164,7 +174,7 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
}
|
||||
}
|
||||
|
||||
public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, IResolveCancelPromise<T>, IDisposable
|
||||
public sealed partial class AsyncTriggerHandler<T> : IUniTaskSource<T>, ITriggerHandler<T>, IDisposable
|
||||
{
|
||||
static Action<object> cancellationCallback = CancellationCallback;
|
||||
|
||||
@@ -253,14 +263,24 @@ namespace Cysharp.Threading.Tasks.Triggers
|
||||
}
|
||||
}
|
||||
|
||||
bool IResolvePromise<T>.TrySetResult(T result)
|
||||
void ITriggerHandler<T>.OnNext(T value)
|
||||
{
|
||||
return core.TrySetResult(result);
|
||||
core.TrySetResult(value);
|
||||
}
|
||||
|
||||
bool ICancelPromise.TrySetCanceled(CancellationToken cancellationToken)
|
||||
void ITriggerHandler<T>.OnCanceled(CancellationToken cancellationToken)
|
||||
{
|
||||
return core.TrySetCanceled(cancellationToken);
|
||||
core.TrySetCanceled(cancellationToken);
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnCompleted()
|
||||
{
|
||||
core.TrySetCanceled(CancellationToken.None);
|
||||
}
|
||||
|
||||
void ITriggerHandler<T>.OnError(Exception ex)
|
||||
{
|
||||
core.TrySetException(ex);
|
||||
}
|
||||
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
|
||||
@@ -132,6 +132,101 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
asyncAction(state).Forget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defer the task creation just before call await.
|
||||
/// </summary>
|
||||
public static UniTask Defer(Func<UniTask> factory)
|
||||
{
|
||||
return new UniTask(new DeferPromise(factory), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defer the task creation just before call await.
|
||||
/// </summary>
|
||||
public static UniTask<T> Defer<T>(Func<UniTask<T>> factory)
|
||||
{
|
||||
return new UniTask<T>(new DeferPromise<T>(factory), 0);
|
||||
}
|
||||
|
||||
sealed class DeferPromise : IUniTaskSource
|
||||
{
|
||||
Func<UniTask> factory;
|
||||
UniTask task;
|
||||
UniTask.Awaiter awaiter;
|
||||
|
||||
public DeferPromise(Func<UniTask> factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public void GetResult(short token)
|
||||
{
|
||||
awaiter.GetResult();
|
||||
}
|
||||
|
||||
public UniTaskStatus GetStatus(short token)
|
||||
{
|
||||
var f = Interlocked.Exchange(ref factory, null);
|
||||
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
||||
|
||||
task = f();
|
||||
awaiter = f().GetAwaiter();
|
||||
return task.Status;
|
||||
}
|
||||
|
||||
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
awaiter.SourceOnCompleted(continuation, state);
|
||||
}
|
||||
|
||||
public UniTaskStatus UnsafeGetStatus()
|
||||
{
|
||||
return task.Status;
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DeferPromise<T> : IUniTaskSource<T>
|
||||
{
|
||||
Func<UniTask<T>> factory;
|
||||
UniTask<T> task;
|
||||
UniTask<T>.Awaiter awaiter;
|
||||
|
||||
public DeferPromise(Func<UniTask<T>> factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public T GetResult(short token)
|
||||
{
|
||||
return awaiter.GetResult();
|
||||
}
|
||||
|
||||
void IUniTaskSource.GetResult(short token)
|
||||
{
|
||||
awaiter.GetResult();
|
||||
}
|
||||
|
||||
public UniTaskStatus GetStatus(short token)
|
||||
{
|
||||
var f = Interlocked.Exchange(ref factory, null);
|
||||
if (f == null) throw new InvalidOperationException("Can't call twice.");
|
||||
|
||||
task = f();
|
||||
awaiter = f().GetAwaiter();
|
||||
return task.Status;
|
||||
}
|
||||
|
||||
public void OnCompleted(Action<object> continuation, object state, short token)
|
||||
{
|
||||
awaiter.SourceOnCompleted(continuation, state);
|
||||
}
|
||||
|
||||
public UniTaskStatus UnsafeGetStatus()
|
||||
{
|
||||
return task.Status;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static class CompletedTasks
|
||||
|
||||
@@ -19,6 +19,11 @@ namespace Cysharp.Threading.Tasks
|
||||
return new UniTask(WaitWhilePromise.Create(predicate, timing, cancellationToken, out var token), token);
|
||||
}
|
||||
|
||||
public static UniTask WaitUntilCanceled(CancellationToken cancellationToken, PlayerLoopTiming timing = PlayerLoopTiming.Update)
|
||||
{
|
||||
return new UniTask(WaitUntilCanceledPromise.Create(cancellationToken, timing, out var token), token);
|
||||
}
|
||||
|
||||
public static UniTask<U> WaitUntilValueChanged<T, U>(T target, Func<T, U> monitorFunction, PlayerLoopTiming monitorTiming = PlayerLoopTiming.Update, IEqualityComparer<U> equalityComparer = null, CancellationToken cancellationToken = default(CancellationToken))
|
||||
where T : class
|
||||
{
|
||||
@@ -234,6 +239,91 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
sealed class WaitUntilCanceledPromise : IUniTaskSource, IPlayerLoopItem, IPromisePoolItem
|
||||
{
|
||||
static readonly PromisePool<WaitUntilCanceledPromise> pool = new PromisePool<WaitUntilCanceledPromise>();
|
||||
|
||||
CancellationToken cancellationToken;
|
||||
|
||||
UniTaskCompletionSourceCore<object> core;
|
||||
|
||||
WaitUntilCanceledPromise()
|
||||
{
|
||||
}
|
||||
|
||||
public static IUniTaskSource Create(CancellationToken cancellationToken, PlayerLoopTiming timing, out short token)
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return AutoResetUniTaskCompletionSource.CreateFromCanceled(cancellationToken, out token);
|
||||
}
|
||||
|
||||
var result = pool.TryRent() ?? new WaitUntilCanceledPromise();
|
||||
|
||||
result.cancellationToken = cancellationToken;
|
||||
|
||||
TaskTracker.TrackActiveTask(result, 3);
|
||||
|
||||
PlayerLoopHelper.AddAction(timing, result);
|
||||
|
||||
token = result.core.Version;
|
||||
return result;
|
||||
}
|
||||
|
||||
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 bool MoveNext()
|
||||
{
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
core.TrySetResult(null);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
core.Reset();
|
||||
cancellationToken = default;
|
||||
}
|
||||
|
||||
~WaitUntilCanceledPromise()
|
||||
{
|
||||
if (pool.TryReturn(this))
|
||||
{
|
||||
GC.ReRegisterForFinalize(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// where T : UnityEngine.Object, can not add constraint
|
||||
sealed class WaitUntilValueChangedUnityObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
|
||||
{
|
||||
@@ -353,7 +443,6 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sealed class WaitUntilValueChangedStandardObjectPromise<T, U> : IUniTaskSource<U>, IPlayerLoopItem, IPromisePoolItem
|
||||
where T : class
|
||||
{
|
||||
|
||||
@@ -38,14 +38,6 @@ namespace Cysharp.Threading.Tasks
|
||||
{
|
||||
}
|
||||
|
||||
public interface IResolveCancelPromise : IResolvePromise, ICancelPromise
|
||||
{
|
||||
}
|
||||
|
||||
public interface IResolveCancelPromise<T> : IResolvePromise<T>, ICancelPromise
|
||||
{
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Auto)]
|
||||
public struct UniTaskCompletionSourceCore<TResult>
|
||||
{
|
||||
@@ -97,6 +89,11 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
}
|
||||
|
||||
internal void MarkHandled()
|
||||
{
|
||||
hasUnhandledError = false;
|
||||
}
|
||||
|
||||
/// <summary>Completes with a successful result.</summary>
|
||||
/// <param name="result">The result.</param>
|
||||
[DebuggerHidden]
|
||||
@@ -293,12 +290,12 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[Conditional("UNITY_EDITOR")]
|
||||
internal void MarkHandled()
|
||||
{
|
||||
if (!handled)
|
||||
{
|
||||
handled = true;
|
||||
core.MarkHandled();
|
||||
TaskTracker.RemoveTracking(this);
|
||||
}
|
||||
}
|
||||
@@ -504,12 +501,12 @@ namespace Cysharp.Threading.Tasks
|
||||
}
|
||||
|
||||
[DebuggerHidden]
|
||||
[Conditional("UNITY_EDITOR")]
|
||||
internal void MarkHandled()
|
||||
{
|
||||
if (!handled)
|
||||
{
|
||||
handled = true;
|
||||
core.MarkHandled();
|
||||
TaskTracker.RemoveTracking(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "com.cysharp.unitask",
|
||||
"displayName": "UniTask",
|
||||
"version": "2.0.5-rc2",
|
||||
"version": "2.0.7-rc4",
|
||||
"unity": "2018.3",
|
||||
"description": "Provides an efficient async/await integration to Unity.",
|
||||
"keywords": ["async/await", "async", "Task", "UniTask"],
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
using System;
|
||||
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks.Linq;
|
||||
using Cysharp.Threading.Tasks.Triggers;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Collections;
|
||||
using Unity.Jobs;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
|
||||
public struct MyJob : IJob
|
||||
{
|
||||
@@ -42,6 +38,11 @@ public enum MyEnum
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static partial class UnityUIComponentExtensions
|
||||
{
|
||||
|
||||
@@ -50,7 +51,36 @@ public static partial class UnityUIComponentExtensions
|
||||
|
||||
|
||||
|
||||
public class AsyncMessageBroker<T> : IDisposable
|
||||
{
|
||||
Channel<T> channel;
|
||||
|
||||
IConnectableUniTaskAsyncEnumerable<T> multicastSource;
|
||||
IDisposable connection;
|
||||
|
||||
public AsyncMessageBroker()
|
||||
{
|
||||
channel = Channel.CreateSingleConsumerUnbounded<T>();
|
||||
multicastSource = channel.Reader.ReadAllAsync().Publish();
|
||||
connection = multicastSource.Connect();
|
||||
}
|
||||
|
||||
public void Publish(T value)
|
||||
{
|
||||
channel.Writer.TryWrite(value);
|
||||
}
|
||||
|
||||
public IUniTaskAsyncEnumerable<T> Subscribe()
|
||||
{
|
||||
return multicastSource;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
channel.Writer.TryComplete();
|
||||
connection.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class SandboxMain : MonoBehaviour
|
||||
@@ -98,6 +128,7 @@ public class SandboxMain : MonoBehaviour
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
UnityEngine.Debug.Log("OK");
|
||||
await scheduled; // .ConfigureAwait(PlayerLoopTiming.Update); // .WaitAsync(PlayerLoopTiming.Update);
|
||||
@@ -141,150 +172,61 @@ public class SandboxMain : MonoBehaviour
|
||||
|
||||
void Start()
|
||||
{
|
||||
Application.SetStackTraceLogType(LogType.Error, StackTraceLogType.Full);
|
||||
Application.SetStackTraceLogType(LogType.Exception, StackTraceLogType.Full);
|
||||
//var rp = new AsyncReactiveProperty<int>(10);
|
||||
|
||||
var playerLoop = UnityEngine.LowLevel.PlayerLoop.GetCurrentPlayerLoop();
|
||||
//ShowPlayerLoop.DumpPlayerLoop("Current", playerLoop);
|
||||
//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(_ => { });
|
||||
|
||||
|
||||
RP1 = new AsyncReactiveProperty<int>(999);
|
||||
var pubsub = new AsyncMessageBroker<int>();
|
||||
|
||||
HogeAsync().Forget();
|
||||
|
||||
RP1.Select(x => x * x).BindTo(text);
|
||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("A:" + x)).Forget();
|
||||
pubsub.Subscribe().ForEachAsync(x => Debug.Log("B:" + x)).Forget();
|
||||
|
||||
|
||||
//Update2().Forget();
|
||||
|
||||
//RunStandardDelayAsync().Forget();
|
||||
|
||||
//for (int i = 0; i < 14; i++)
|
||||
//{
|
||||
// TimingDump((PlayerLoopTiming)i).Forget();
|
||||
//}
|
||||
|
||||
//StartCoroutine(CoroutineDump("yield WaitForEndOfFrame", new WaitForEndOfFrame()));
|
||||
//StartCoroutine(CoroutineDump("yield WaitForFixedUpdate", new WaitForFixedUpdate()));
|
||||
//StartCoroutine(CoroutineDump("yield null", null));
|
||||
|
||||
// -----
|
||||
|
||||
// RunJobAsync().Forget();
|
||||
|
||||
//ClickOnce().Forget();
|
||||
//ClickForever().Forget();
|
||||
|
||||
//var cor = UniTask.ToCoroutine(async () =>
|
||||
// {
|
||||
// var job = new MyJob() { loopCount = 999, inOut = new NativeArray<int>(1, Allocator.TempJob) };
|
||||
// JobHandle.ScheduleBatchedJobs();
|
||||
// await job.Schedule().WaitAsync(PlayerLoopTiming.Update);
|
||||
// job.inOut.Dispose();
|
||||
// });
|
||||
|
||||
//StartCoroutine(cor);
|
||||
|
||||
// UniTaskAsyncEnumerable.EveryUpdate(PlayerLoopTiming.FixedUpdate)
|
||||
|
||||
|
||||
//await UniTask.Yield(PlayerLoopTiming.Update);
|
||||
//Debug.Log("Start:" + Time.frameCount);
|
||||
|
||||
//await UniTaskAsyncEnumerable.TimerFrame(3, 5, PlayerLoopTiming.PostLateUpdate)
|
||||
// .Select(x => x)
|
||||
// .Do(x => Debug.Log("DODODO"))
|
||||
// .ForEachAsync(_ =>
|
||||
// {
|
||||
// Debug.Log("Call:" + Time.frameCount);
|
||||
// }, cancellationToken: this.GetCancellationTokenOnDestroy());
|
||||
|
||||
//try
|
||||
//{
|
||||
// await this.GetAsyncUpdateTrigger().ForEachAsync(_ =>
|
||||
// {
|
||||
// UnityEngine.Debug.Log("EveryUpdate:" + Time.frameCount);
|
||||
// });
|
||||
//}
|
||||
//catch (OperationCanceledException ex)
|
||||
//{
|
||||
// UnityEngine.Debug.Log("END");
|
||||
//}
|
||||
|
||||
|
||||
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
|
||||
//var trigger = this.GetAsyncUpdateTrigger();
|
||||
//Go(trigger, 1, cts.Token).Forget();
|
||||
//Go(trigger, 2, cts.Token).Forget();
|
||||
//Go(trigger, 3, cts.Token).Forget();
|
||||
//Go(trigger, 4, cts.Token).Forget();
|
||||
//Go(trigger, 5, cts.Token).Forget();
|
||||
|
||||
|
||||
Application.logMessageReceived += Application_logMessageReceived;
|
||||
|
||||
|
||||
|
||||
// foo.Status.IsCanceled
|
||||
|
||||
|
||||
// 5回クリックされるまで待つ、とか。
|
||||
//Debug.Log("Await start.");
|
||||
|
||||
|
||||
|
||||
//await okButton.GetAsyncClickEventHandler().DisableAutoClose()
|
||||
// .Select((_, clickCount) => clickCount + 1)
|
||||
// .FirstAsync(x => x == 5);
|
||||
|
||||
//Debug.Log("Click 5 times.");
|
||||
|
||||
|
||||
|
||||
// await this.GetAsyncUpdateTrigger().UpdateAsAsyncEnumerable()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//ucs = new UniTaskCompletionSource();
|
||||
|
||||
//okButton.onClick.AddListener(async () =>
|
||||
//{
|
||||
// await InnerAsync(false);
|
||||
//});
|
||||
|
||||
okButton.onClick.AddListener(() =>
|
||||
int i = 0;
|
||||
okButton.OnClickAsAsyncEnumerable().ForEachAsync(_ =>
|
||||
{
|
||||
// FooAsync().Forget();
|
||||
|
||||
RP1.Value += 3;
|
||||
Debug.Log("foo");
|
||||
pubsub.Publish(i++);
|
||||
|
||||
});
|
||||
|
||||
cancelButton.onClick.AddListener(() =>
|
||||
{
|
||||
text.text = "";
|
||||
}).Forget();
|
||||
|
||||
// ucs.TrySetResult();
|
||||
|
||||
cts.Cancel();
|
||||
});
|
||||
}
|
||||
|
||||
static void Foo(UniTask t)
|
||||
async UniTaskVoid Running(CancellationToken ct)
|
||||
{
|
||||
Debug.Log("BEGIN");
|
||||
await UniTask.WaitUntilCanceled(ct);
|
||||
Debug.Log("DONE");
|
||||
}
|
||||
|
||||
async UniTaskVoid WaitForChannelAsync(ChannelReader<int> reader, CancellationToken token)
|
||||
{
|
||||
try
|
||||
{
|
||||
//var result1 = await reader.ReadAsync(token);
|
||||
//Debug.Log(result1);
|
||||
|
||||
await reader.ReadAllAsync().ForEachAsync(x => Debug.Log(x)/*, token*/);
|
||||
|
||||
Debug.Log("done");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.Log("here");
|
||||
Debug.LogException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
async UniTaskVoid Go(AsyncUpdateTrigger trigger, int i, CancellationToken ct)
|
||||
|
||||
Reference in New Issue
Block a user