Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
added cancellation if playstate changes while awaiting indefinite cha…
Browse files Browse the repository at this point in the history
…nges (#211)
  • Loading branch information
StephenHodgson authored Jun 8, 2019
1 parent ba3accb commit c358d59
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions XRTK-Core/Packages/com.xrtk.core/Utilities/Async/Awaiters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,21 @@ void Exception()
}

cancellationTokenSource.Token.Register(Exception);
#if UNITY_EDITOR
var editorCancelled = false;
UnityEditor.EditorApplication.playModeStateChanged += playModeStateChanged => editorCancelled = true;
#endif

while (!cancellationTokenSource.IsCancellationRequested)
{

#if UNITY_EDITOR
if (editorCancelled)
{
tcs.TrySetCanceled(CancellationToken.None);
}
#endif

try
{
if (!predicate(element))
Expand All @@ -103,12 +115,31 @@ private static async Task WaitUntil_Indefinite<T>(T element, Func<T, bool> predi
{
var tcs = new TaskCompletionSource<object>();

#if UNITY_EDITOR
var editorCancelled = false;
UnityEditor.EditorApplication.playModeStateChanged += playModeStateChanged => editorCancelled = true;
#endif

while (true)
{
if (!predicate(element))
#if UNITY_EDITOR
if (editorCancelled)
{
tcs.TrySetCanceled(CancellationToken.None);
}
#endif

try
{
if (!predicate(element))
{
await Task.Delay(1);
continue;
}
}
catch (Exception e)
{
await Task.Delay(1);
continue;
tcs.TrySetException(e);
}

tcs.TrySetResult(Task.CompletedTask);
Expand Down

0 comments on commit c358d59

Please sign in to comment.