You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.9 KiB
44 lines
1.9 KiB
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member |
|
|
|
using System.Threading; |
|
using UnityEngine; |
|
using Cysharp.Threading.Tasks.Triggers; |
|
using System; |
|
using Cysharp.Threading.Tasks.Internal; |
|
|
|
namespace Cysharp.Threading.Tasks |
|
{ |
|
|
|
public static partial class CancellationTokenSourceExtensions |
|
{ |
|
readonly static Action<object> CancelCancellationTokenSourceStateDelegate = new Action<object>(CancelCancellationTokenSourceState); |
|
|
|
static void CancelCancellationTokenSourceState(object state) |
|
{ |
|
var cts = (CancellationTokenSource)state; |
|
cts.Cancel(); |
|
} |
|
|
|
public static IDisposable CancelAfterSlim(this CancellationTokenSource cts, int millisecondsDelay, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) |
|
{ |
|
return CancelAfterSlim(cts, TimeSpan.FromMilliseconds(millisecondsDelay), delayType, delayTiming); |
|
} |
|
|
|
public static IDisposable CancelAfterSlim(this CancellationTokenSource cts, TimeSpan delayTimeSpan, DelayType delayType = DelayType.DeltaTime, PlayerLoopTiming delayTiming = PlayerLoopTiming.Update) |
|
{ |
|
return PlayerLoopTimer.StartNew(delayTimeSpan, false, delayType, delayTiming, cts.Token, CancelCancellationTokenSourceStateDelegate, cts); |
|
} |
|
|
|
public static void RegisterRaiseCancelOnDestroy(this CancellationTokenSource cts, Component component) |
|
{ |
|
RegisterRaiseCancelOnDestroy(cts, component.gameObject); |
|
} |
|
|
|
public static void RegisterRaiseCancelOnDestroy(this CancellationTokenSource cts, GameObject gameObject) |
|
{ |
|
var trigger = gameObject.GetAsyncDestroyTrigger(); |
|
trigger.CancellationToken.RegisterWithoutCaptureExecutionContext(CancelCancellationTokenSourceStateDelegate, cts); |
|
} |
|
} |
|
} |
|
|
|
|