Change AsyncEnumerable factory to use OperationCanceledException

master
hadashiA 2023-10-27 16:45:41 +09:00
parent 94be2e748b
commit 2cf06af433
3 changed files with 69 additions and 19 deletions

View File

@ -56,10 +56,14 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
// return false instead of throw if (disposed) return CompletedTasks.False;
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
completionSource.Reset(); completionSource.Reset();
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
}
return new UniTask<bool>(this, completionSource.Version); return new UniTask<bool>(this, completionSource.Version);
} }
@ -76,7 +80,13 @@ namespace Cysharp.Threading.Tasks.Linq
public bool MoveNext() public bool MoveNext()
{ {
if (disposed || cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return false;
}
if (disposed)
{ {
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
return false; return false;

View File

@ -85,8 +85,15 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
// return false instead of throw if (disposed) return CompletedTasks.False;
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False;
completionSource.Reset();
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return new UniTask<bool>(this, completionSource.Version);
}
if (first) if (first)
{ {
@ -99,7 +106,6 @@ namespace Cysharp.Threading.Tasks.Linq
return CompletedTasks.True; return CompletedTasks.True;
} }
completionSource.Reset();
return new UniTask<bool>(this, completionSource.Version); return new UniTask<bool>(this, completionSource.Version);
} }
@ -107,6 +113,7 @@ namespace Cysharp.Threading.Tasks.Linq
{ {
if (!disposed) if (!disposed)
{ {
cancellationTokenRegistration.Dispose();
disposed = true; disposed = true;
TaskTracker.RemoveTracking(this); TaskTracker.RemoveTracking(this);
} }
@ -115,13 +122,18 @@ namespace Cysharp.Threading.Tasks.Linq
public bool MoveNext() public bool MoveNext()
{ {
if (disposed || cancellationToken.IsCancellationRequested || targetAsUnityObject == null) // destroyed = cancel. if (disposed || targetAsUnityObject == null)
{ {
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
DisposeAsync().Forget(); DisposeAsync().Forget();
return false; return false;
} }
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return false;
}
TProperty nextValue = default(TProperty); TProperty nextValue = default(TProperty);
try try
{ {
@ -205,8 +217,16 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
if (disposed || cancellationToken.IsCancellationRequested) return CompletedTasks.False; if (disposed) return CompletedTasks.False;
completionSource.Reset();
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return new UniTask<bool>(this, completionSource.Version);
}
if (first) if (first)
{ {
first = false; first = false;
@ -218,7 +238,6 @@ namespace Cysharp.Threading.Tasks.Linq
return CompletedTasks.True; return CompletedTasks.True;
} }
completionSource.Reset();
return new UniTask<bool>(this, completionSource.Version); return new UniTask<bool>(this, completionSource.Version);
} }
@ -235,13 +254,19 @@ namespace Cysharp.Threading.Tasks.Linq
public bool MoveNext() public bool MoveNext()
{ {
if (disposed || cancellationToken.IsCancellationRequested || !target.TryGetTarget(out var t)) if (disposed || !target.TryGetTarget(out var t))
{ {
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
DisposeAsync().Forget(); DisposeAsync().Forget();
return false; return false;
} }
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return false;
}
TProperty nextValue = default(TProperty); TProperty nextValue = default(TProperty);
try try
{ {

View File

@ -109,7 +109,6 @@ namespace Cysharp.Threading.Tasks.Linq
this.ignoreTimeScale = ignoreTimeScale; this.ignoreTimeScale = ignoreTimeScale;
this.cancellationToken = cancellationToken; this.cancellationToken = cancellationToken;
if (cancelImmediately && cancellationToken.CanBeCanceled) if (cancelImmediately && cancellationToken.CanBeCanceled)
{ {
cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state => cancellationTokenRegistration = cancellationToken.RegisterWithoutCaptureExecutionContext(state =>
@ -128,12 +127,16 @@ 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 || completed) return CompletedTasks.False; if (disposed || completed) return CompletedTasks.False;
// reset value here. // reset value here.
this.elapsed = 0; this.elapsed = 0;
completionSource.Reset(); completionSource.Reset();
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
}
return new UniTask<bool>(this, completionSource.Version); return new UniTask<bool>(this, completionSource.Version);
} }
@ -150,11 +153,16 @@ namespace Cysharp.Threading.Tasks.Linq
public bool MoveNext() public bool MoveNext()
{ {
if (disposed || cancellationToken.IsCancellationRequested) if (disposed)
{ {
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
return false; return false;
} }
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return false;
}
if (dueTimePhase) if (dueTimePhase)
{ {
@ -261,13 +269,15 @@ namespace Cysharp.Threading.Tasks.Linq
public UniTask<bool> MoveNextAsync() public UniTask<bool> MoveNextAsync()
{ {
// return false instead of throw if (disposed || completed) return CompletedTasks.False;
if (disposed || cancellationToken.IsCancellationRequested || completed) return CompletedTasks.False;
if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
}
// reset value here. // reset value here.
this.currentFrame = 0; this.currentFrame = 0;
completionSource.Reset(); completionSource.Reset();
return new UniTask<bool>(this, completionSource.Version); return new UniTask<bool>(this, completionSource.Version);
} }
@ -285,7 +295,12 @@ namespace Cysharp.Threading.Tasks.Linq
public bool MoveNext() public bool MoveNext()
{ {
if (disposed || cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{
completionSource.TrySetCanceled(cancellationToken);
return false;
}
if (disposed)
{ {
completionSource.TrySetResult(false); completionSource.TrySetResult(false);
return false; return false;