Fix using AsyncUnityEventHandler with a already canceled token

pull/220/head
taigacon 2021-01-25 20:02:56 +08:00
parent 7d21a75ea8
commit 3f4a46bca6
1 changed files with 10 additions and 2 deletions

View File

@ -364,6 +364,7 @@ namespace Cysharp.Threading.Tasks
public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce) public AsyncUnityEventHandler(UnityEvent unityEvent, CancellationToken cancellationToken, bool callOnce)
{ {
this.cancellationToken = cancellationToken;
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
isDisposed = true; isDisposed = true;
@ -372,7 +373,6 @@ namespace Cysharp.Threading.Tasks
this.action = Invoke; this.action = Invoke;
this.unityEvent = unityEvent; this.unityEvent = unityEvent;
this.cancellationToken = cancellationToken;
this.callOnce = callOnce; this.callOnce = callOnce;
unityEvent.AddListener(action); unityEvent.AddListener(action);
@ -388,6 +388,10 @@ namespace Cysharp.Threading.Tasks
public UniTask OnInvokeAsync() public UniTask OnInvokeAsync()
{ {
core.Reset(); core.Reset();
if (isDisposed)
{
core.TrySetCanceled(this.cancellationToken);
}
return new UniTask(this, core.Version); return new UniTask(this, core.Version);
} }
@ -470,6 +474,7 @@ namespace Cysharp.Threading.Tasks
public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce) public AsyncUnityEventHandler(UnityEvent<T> unityEvent, CancellationToken cancellationToken, bool callOnce)
{ {
this.cancellationToken = cancellationToken;
if (cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ {
isDisposed = true; isDisposed = true;
@ -478,7 +483,6 @@ namespace Cysharp.Threading.Tasks
this.action = Invoke; this.action = Invoke;
this.unityEvent = unityEvent; this.unityEvent = unityEvent;
this.cancellationToken = cancellationToken;
this.callOnce = callOnce; this.callOnce = callOnce;
unityEvent.AddListener(action); unityEvent.AddListener(action);
@ -494,6 +498,10 @@ namespace Cysharp.Threading.Tasks
public UniTask<T> OnInvokeAsync() public UniTask<T> OnInvokeAsync()
{ {
core.Reset(); core.Reset();
if (isDisposed)
{
core.TrySetCanceled(this.cancellationToken);
}
return new UniTask<T>(this, core.Version); return new UniTask<T>(this, core.Version);
} }