mirror of https://github.com/Cysharp/UniTask
Update UniTaskExtensions.cs
Added an extension to run single-instance unitasks using CancellationTokenSourceReferences This avoids running several instances of unitasks using the same CancellationToenSource referencepull/173/head
parent
2bf9f4f062
commit
3eb14dfe4b
|
@ -235,6 +235,16 @@ namespace Cysharp.Threading.Tasks
|
|||
return new UniTask<T>(new WithCancellationSource<T>(task, cancellationToken), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancels last running instance to ensure there are not 2 or more instances of this UniTask running and regenerates the token source
|
||||
/// </summary>
|
||||
public static UniTask WithLastInstanceCancellation(this UniTask task, ref CancellationTokenSource cancellationTokenSource)
|
||||
{
|
||||
cancellationTokenSource.Cancel();
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
return task.WithCancellation(cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
sealed class WithCancellationSource : IUniTaskSource
|
||||
{
|
||||
static readonly Action<object> cancellationCallbackDelegate = CancellationCallback;
|
||||
|
|
Loading…
Reference in New Issue