mirror of https://github.com/Cysharp/UniTask
Add UniTask.ToCancellationToken
parent
8df44f2768
commit
0a447e43b0
|
@ -11,6 +11,33 @@ namespace Cysharp.Threading.Tasks
|
||||||
static readonly Action<object> cancellationTokenCallback = Callback;
|
static readonly Action<object> cancellationTokenCallback = Callback;
|
||||||
static readonly Action<object> disposeCallback = DisposeCallback;
|
static readonly Action<object> disposeCallback = DisposeCallback;
|
||||||
|
|
||||||
|
public static CancellationToken ToCancellationToken(this UniTask task)
|
||||||
|
{
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
ToCancellationTokenCore(task, cts).Forget();
|
||||||
|
return cts.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static CancellationToken ToCancellationToken<T>(this UniTask<T> task)
|
||||||
|
{
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
ToCancellationTokenCore(task, cts).Forget();
|
||||||
|
return cts.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async UniTaskVoid ToCancellationTokenCore(UniTask task, CancellationTokenSource cts)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await task;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
UniTaskScheduler.PublishUnobservedTaskException(ex);
|
||||||
|
}
|
||||||
|
cts.Cancel();
|
||||||
|
}
|
||||||
|
|
||||||
public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken)
|
public static (UniTask, CancellationTokenRegistration) ToUniTask(this CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (cancellationToken.IsCancellationRequested)
|
if (cancellationToken.IsCancellationRequested)
|
||||||
|
|
Loading…
Reference in New Issue