Skip to content

Instantly share code, notes, and snippets.

@godfat godfat/defer.rb
Created Jan 1, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.