Reactor - Fiber Handler

A fiber-based handler loop.

Usage

Subclass the Reactor::FiberHandler class and define the #run method.

The method accepts the dispatcher (Reactor::Dispatcher), event (Symbol) and handle (IO).

The dispatcher reference can be used to register new handlers for custom events.

Method should return nil or false if the handler should break out of the handling loop.

class Repl < Reactor::FiberHandler
  def run(reactor, handle, event)
    begin
      puts eval(handle.readline).inspect
    rescue EOFError
      puts "Bye."
      reactor.deregister(handle, :read)
      reactor.stop
      return
    rescue => e
      puts "#{e.class}: #{e.message}"
    end

    print "> "

    :ok
  end
end

dispatcher = Reactor::Dispatcher.new
dispatcher.register($stdin, :read, Repl.new)
dispatcher.run