UnityEvent.AsAsyncEnumerable handle cancel correctly #365

master
neuecc 2022-10-24 21:51:23 +09:00
parent dc804ffb13
commit 340736795c
1 changed files with 36 additions and 4 deletions

View File

@ -688,14 +688,28 @@ namespace Cysharp.Threading.Tasks
static void OnCanceled1(object state) static void OnCanceled1(object state)
{ {
var self = (UnityEventHandlerAsyncEnumerator)state; var self = (UnityEventHandlerAsyncEnumerator)state;
try
{
self.completionSource.TrySetCanceled(self.cancellationToken1);
}
finally
{
self.DisposeAsync().Forget(); self.DisposeAsync().Forget();
} }
}
static void OnCanceled2(object state) static void OnCanceled2(object state)
{ {
var self = (UnityEventHandlerAsyncEnumerator)state; var self = (UnityEventHandlerAsyncEnumerator)state;
try
{
self.completionSource.TrySetCanceled(self.cancellationToken2);
}
finally
{
self.DisposeAsync().Forget(); self.DisposeAsync().Forget();
} }
}
public UniTask DisposeAsync() public UniTask DisposeAsync()
{ {
@ -706,6 +720,8 @@ namespace Cysharp.Threading.Tasks
registration1.Dispose(); registration1.Dispose();
registration2.Dispose(); registration2.Dispose();
unityEvent.RemoveListener(unityAction); unityEvent.RemoveListener(unityAction);
completionSource.TrySetCanceled();
} }
return default; return default;
@ -793,14 +809,28 @@ namespace Cysharp.Threading.Tasks
static void OnCanceled1(object state) static void OnCanceled1(object state)
{ {
var self = (UnityEventHandlerAsyncEnumerator)state; var self = (UnityEventHandlerAsyncEnumerator)state;
try
{
self.completionSource.TrySetCanceled(self.cancellationToken1);
}
finally
{
self.DisposeAsync().Forget(); self.DisposeAsync().Forget();
} }
}
static void OnCanceled2(object state) static void OnCanceled2(object state)
{ {
var self = (UnityEventHandlerAsyncEnumerator)state; var self = (UnityEventHandlerAsyncEnumerator)state;
try
{
self.completionSource.TrySetCanceled(self.cancellationToken2);
}
finally
{
self.DisposeAsync().Forget(); self.DisposeAsync().Forget();
} }
}
public UniTask DisposeAsync() public UniTask DisposeAsync()
{ {
@ -815,6 +845,8 @@ namespace Cysharp.Threading.Tasks
disp.Dispose(); disp.Dispose();
} }
unityEvent.RemoveListener(unityAction); unityEvent.RemoveListener(unityAction);
completionSource.TrySetCanceled();
} }
return default; return default;