igrigorik/em-synchrony — Issue #113: "Synchronous" em-http methods without overriding existing ones.
Author: oruen
Created: 2012-03-02 20:17:42+00:00
State: open

Overriding existing [EM::HttpMethods](https://github.com/igrigorik/em-synchrony/blob/master/lib/em-synchrony/em-http.rb) for transparent usage with em-synchrony is a good idea for applications built from scratch. But sometimes libs using em-http-request are not aware of em-synchrony and usage of overridden methods inside such libs leads to confusion.

I'm building an app using em-synchrony heavily and now I need to add the lib completely unaware of it: [em-eventsource](https://github.com/AF83/em-eventsource). It uses em-http-request [under the hood](https://github.com/AF83/em-eventsource/blob/master/lib/em-eventsource.rb#L160-168) and blocks forever.

I've dealt with it by excluding em-synchrony/em-http but adding module provided instead. I think something similar could be bundled with em-synchrony.

```ruby
module EventMachine
module HTTPMethods
%w[get head post delete put].each do |type|
class_eval %[
def sync_#{type}(options = {}, &blk)
f = Fiber.current

conn = setup_request(:#{type}, options, &blk)
if conn.error.nil?
conn.callback { f.resume(conn) }
conn.errback { f.resume(conn) }

Fiber.yield
else
conn
end
end
]
end
end
end
```