Last active
June 5, 2016 20:45
-
-
Save chuckremes/ed78d3c3066194ebf593b163d5631e3e to your computer and use it in GitHub Desktop.
This file contains 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
# Acts like an array and receives futures. Will yield them as | |
# they become ready. | |
class HackedMultiplexer | |
include Celluloid | |
include Enumerable | |
def initialize | |
@not_ready = [] | |
end | |
def push(obj) | |
@not_ready.push(obj) | |
end | |
alias_method :<<, :push | |
def each(&blk) | |
loop do | |
ready = @not_ready.select { |future| future.ready? } | |
@not_ready -= ready | |
ready.each { |future| yield(future) } | |
break if @not_ready.empty? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment