Last active
March 12, 2018 15:45
-
-
Save vladpazych/dcedf4a6b35c29b69bc0f29094848db7 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
| using UnityEngine; | |
| using System; | |
| using System.Collections; | |
| using RSG; | |
| public partial class WaitManager { | |
| private WaitManager () { | |
| } | |
| private static WaitManager _instance; | |
| public static WaitManager instance { | |
| get { | |
| if (_instance == null) _instance = new WaitManager (); | |
| return _instance; | |
| } | |
| } | |
| public IPromise WaitUntil (Func<bool> condition) { | |
| var promise = new Promise (); | |
| var routine = ResolveWhen (promise, condition); | |
| CoroutineManager.instance.Run (routine); | |
| return promise; | |
| } | |
| public IPromise WaitUntil (Func<bool> condition, float secondsToReject) { | |
| var promise = new Promise (); | |
| var routine = ResolveWhen (promise, condition); | |
| WaitForSeconds (secondsToReject) | |
| .Then (() => { | |
| if (routine != null) CoroutineManager.instance.Break (routine); | |
| promise.SoftReject (new Exception ("Promise rejected because of timeout in " + secondsToReject + " seconds")); | |
| }); | |
| CoroutineManager.instance.Run (routine); | |
| return promise; | |
| } | |
| public IPromise WaitForSeconds (float seconds) { | |
| var promise = new Promise (); | |
| var routine = ResolveAfter (promise, seconds); | |
| CoroutineManager.instance.Run (routine); | |
| return promise; | |
| } | |
| IEnumerator ResolveWhen (Promise promise, Func<bool> condition) { | |
| yield return new WaitUntil (condition); | |
| promise.Resolve (); | |
| } | |
| IEnumerator ResolveAfter (Promise promise, float seconds) { | |
| yield return new WaitForSeconds (seconds); | |
| promise.Resolve (); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment