Created
February 5, 2020 13:04
-
-
Save KumoKairo/a30521709cba9ac8bb65d6e0780a5ebe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class UnityWebRequestExtensions | |
{ | |
public static UnityWebRequestAwaiter GetAwaiter(this UnityWebRequestAsyncOperation asyncOperation) | |
{ | |
return new UnityWebRequestAwaiter(asyncOperation); | |
} | |
} | |
public class UnityWebRequestAwaiter : INotifyCompletion | |
{ | |
private Action _continuation; | |
private bool _isPendingContinuation; | |
private bool _isDone; | |
public UnityWebRequestAwaiter(UnityWebRequestAsyncOperation asyncOperation) | |
{ | |
asyncOperation.completed += OnRequestCompleted; | |
} | |
public bool IsCompleted => _isDone; | |
public void GetResult() { } | |
public void OnCompleted(Action continuation) | |
{ | |
if (_isPendingContinuation) | |
{ | |
CallNonNullContinuation(continuation); | |
} | |
else | |
{ | |
_continuation = continuation; | |
} | |
} | |
private void OnRequestCompleted(AsyncOperation asyncOp) | |
{ | |
if (_continuation == null) | |
{ | |
_isPendingContinuation = true; | |
} | |
else | |
{ | |
CallNonNullContinuation(_continuation); | |
} | |
} | |
private void CallNonNullContinuation(Action continuation) | |
{ | |
_isDone = true; | |
continuation(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment