tarcieri/cool.io — Issue #21: Fiber-aware deferred operation
Author: godfat
Created: 2012-01-01 14:46:13+00:00
State: open

I wonder if something like this would be worth to put into cool.io?
Otherwise maybe I'll put this into a gem.
Thanks!

``` ruby

require 'fiber'
require 'coolio'

class Defer < Coolio::AsyncWatcher
def self.defer loop=Coolio::Loop.default, &block
new(loop, &block).result
end

attr_reader :result

def initialize loop=Coolio::Loop.default
super()
@fiber = Fiber.current
attach(loop)
Thread.new{
@result = yield
signal
}
Fiber.yield
end

def on_signal
puts "ON SIGNAL"
@fiber.resume
detach
end
end

Fiber.new{
puts Defer.defer{ sleep 1; "DONE" }
}.resume

puts "GO"
Coolio::Loop.default.run

```