Created
January 12, 2012 07:29
-
-
Save sdether/1599256 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 class Coordinator<T> { | |
| private readonly Queue<IEnumerator> _coroutines = new Queue<IEnumerator>(); | |
| public Coordinator(IEnumerable<Func<Coordinator<T>, IEnumerator>> coroutines) { | |
| // add all coroutines to our queue of coroutines to execute | |
| foreach(var coroutine in coroutines) { | |
| _coroutines.Enqueue(coroutine(this)); | |
| } | |
| } | |
| public void Execute() { | |
| // iterate over coroutines | |
| while(_coroutines.Count > 0) { | |
| // get next coroutine | |
| var coroutine = _coroutines.Dequeue(); | |
| if(coroutine.MoveNext()) { | |
| // if MoveNext was true, the coroutine expects to be resumed, so we put the coroutine back in the queue to | |
| // call MoveNext again once we've given the other coroutines a chance | |
| _coroutines.Enqueue(coroutine); | |
| } | |
| } | |
| } | |
| public T State { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment