fix Await UniTaskAsyncEnumerable.Timer is not over. #76

master
neuecc 2020-05-17 16:49:44 +09:00
parent 3ef889e17d
commit 957adfad7a
4 changed files with 28 additions and 8 deletions

View File

@ -1,11 +1,9 @@
# UniTask UniTask
===
[![CircleCI](https://circleci.com/gh/Cysharp/UniTask.svg?style=svg)](https://circleci.com/gh/Cysharp/UniTask) [![GitHub Actions](https://github.com/Cysharp/UniTask/workflows/Build-Debug/badge.svg)](https://github.com/Cysharp/UniTask/actions) [![Releases](https://img.shields.io/github/release/Cysharp/UniTask.svg)](https://github.com/Cysharp/UniTask/releases)
Provides an efficient async/await integration to Unity. Provides an efficient async/await integration to Unity.
> UniTask was included in UniRx before v7 but now completely separated, it no dependent each other.
Getting started Getting started
--- ---
Install package(`UniRx.Async.unitypackage`) is available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page. Install package(`UniRx.Async.unitypackage`) is available in [UniTask/releases](https://github.com/Cysharp/UniTask/releases) page.

View File

@ -86,6 +86,10 @@ namespace NetCoreSandbox
await Task.Delay(10, cancellationToken); await Task.Delay(10, cancellationToken);
} }
private async UniTaskVoid HogeAsync()
{
}
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10) await foreach (var item in UniTaskAsyncEnumerable.Range(1, 10)

View File

@ -66,6 +66,7 @@ namespace Cysharp.Threading.Tasks.Linq
float elapsed; float elapsed;
bool dueTimePhase; bool dueTimePhase;
bool completed;
bool disposed; bool disposed;
public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken) public _Timer(TimeSpan dueTime, TimeSpan? period, PlayerLoopTiming updateTiming, bool ignoreTimeScale, CancellationToken cancellationToken)
@ -91,7 +92,7 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
// return false instead of throw // return false instead of throw
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False;
// reset value here. // reset value here.
this.elapsed = 0; this.elapsed = 0;
@ -131,6 +132,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
if (period == null) if (period == null)
{ {
completed = true;
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
return false; return false;
} }
@ -172,6 +174,7 @@ namespace Cysharp.Threading.Tasks.Linq
int currentFrame; int currentFrame;
bool dueTimePhase; bool dueTimePhase;
bool completed;
bool disposed; bool disposed;
public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken) public _TimerFrame(int dueTimeFrameCount, int? periodFrameCount, PlayerLoopTiming updateTiming, CancellationToken cancellationToken)
@ -195,7 +198,7 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
// return false instead of throw // return false instead of throw
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False;
// reset value here. // reset value here.
@ -235,6 +238,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
if (periodFrameCount == null) if (periodFrameCount == null)
{ {
completed = true;
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
return false; return false;
} }

View File

@ -124,6 +124,20 @@ public class SandboxMain : MonoBehaviour
} }
private async UniTaskVoid HogeAsync()
{
// await is not over
await UniTaskAsyncEnumerable
.TimerFrame(10)
.ForEachAwaitAsync(async _ =>
// .ForEachAsync(_ =>
{
await UniTask.Delay(1000);
Debug.Log(Time.time);
});
Debug.Log("Done");
}
void Start() void Start()
{ {
@ -136,7 +150,7 @@ public class SandboxMain : MonoBehaviour
RP1 = new AsyncReactiveProperty<int>(999); RP1 = new AsyncReactiveProperty<int>(999);
HogeAsync().Forget();
RP1.Select(x => x * x).BindTo(text); RP1.Select(x => x * x).BindTo(text);