Skip to content

Instantly share code, notes, and snippets.

@godfat
Created January 1, 2012 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godfat/1546965 to your computer and use it in GitHub Desktop.
Save godfat/1546965 to your computer and use it in GitHub Desktop.
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
puts "SIGNAL"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment