Created
August 14, 2021 09:30
-
-
Save skclusive/6542c2ef3985906551bc05dd44262c95 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
class UberQueue<T> : IAsyncQueue<T> | |
{ | |
private IAsyncQueue<T>[] _queues; | |
private Task<T>[] _current; | |
public UberQueue(IAsyncQueue<T>[] queues) | |
{ | |
_queues = queues; | |
_current = new Task<T>[_queues.Length]; | |
} | |
public async Task<T> DequeueAsync() | |
{ | |
_current = _queues.Select((queue, index) => _current[index] ?? queue.DequeueAsync()).ToArray(); | |
var result = await Task.WhenAny(_current); | |
for (var i = 0; i < _current.Length; i++) | |
{ | |
if (_current[i] == result) | |
{ | |
_current[i] = null; | |
} | |
} | |
return result.Result; | |
} | |
} | |
interface IAsyncQueue<T> | |
{ | |
Task<T> DequeueAsync(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment