Skip to content

Instantly share code, notes, and snippets.

@digitalextremist
Forked from chuckremes/multi.rb
Created June 5, 2016 20:45
Show Gist options
  • Save digitalextremist/3ba6b4a1b9852080165c5e6cc1fe697b to your computer and use it in GitHub Desktop.
Save digitalextremist/3ba6b4a1b9852080165c5e6cc1fe697b to your computer and use it in GitHub Desktop.
# 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