Skip to content

Instantly share code, notes, and snippets.

@alehander92 alehander92/a0.nim
Created Mar 26, 2020

What would you like to do?
import deques
AsyncQueue[Input, Output] = ref object
list: Deque[Input]
futures: Deque[Future[Output]]
running: bool
handler: proc(input: Input): Future[Output]
proc initAsyncQueue[Input, Output](handler: proc(input: Input): Future[Output]): AsyncQueue[Input, Output] =
AsyncQueue[Input, Output](list: initDeque[Input](), futures: initDeque[Future[Output]](), running: false, handler: handler)
proc run[Input, Output](queue: AsyncQueue[Input, Output]): Future[void] {.async.} =
queue.running = true
while queue.list.len > 0:
var next = queue.list.popLast
var future = queue.futures.popLast
# echo "next ", $next
future.complete(await queue.handler(next))
queue.running = false
proc pushOrSend[Input, Output](queue: AsyncQueue[Input, Output], input: Input): Future[Output] {.async.} =
var future = newFuture[Output]("pushOrSend " & $input)
# echo "pushOrSend " & $input
if not queue.running:
return await future
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.