Skip to content

Instantly share code, notes, and snippets.

@slyphon
Created October 29, 2011 02:51
Show Gist options
  • Save slyphon/1324024 to your computer and use it in GitHub Desktop.
Save slyphon/1324024 to your computer and use it in GitHub Desktop.
An example of argument handling change for em-synchrony
#!/usr/bin/env ruby
require 'rubygems'
require 'eventmachine'
require 'em-synchrony'
module EventMachine
module Synchrony
def self.new_sync(df)
f = Fiber.current
xback = proc {|*args|
if f == Fiber.current
return *args
else
f.resume(*args)
end
}
df.callback &xback
df.errback &xback
Fiber.yield
end
end
end
def gimme_deferreds
a, b = EM::DefaultDeferrable.new, EM::DefaultDeferrable.new
EM.next_tick do
a.succeed(1, 2, [:foo, :bar])
b.succeed(:single_arg)
end
return a, b
end
def current_implementation
EM.synchrony do
a, b = gimme_deferreds
rval = EM::Synchrony.sync(a)
puts "a rval: #{rval.inspect}"
rval = EM::Synchrony.sync(b)
puts "b rval: #{rval.inspect}"
EM.next_tick { EM.stop_event_loop }
end
end
def proposed_fix
EM.synchrony do
a, b = gimme_deferreds
rval = EM::Synchrony.new_sync(a)
puts "a rval: #{rval.inspect}"
rval = EM::Synchrony.new_sync(b)
puts "b rval: #{rval.inspect}"
EM.next_tick { EM.stop_event_loop }
end
end
if __FILE__ == $0
puts <<-EOS
call signature:
a.succeed(1, 2, [:foo, :bar])
b.succeed(:single_arg)
EOS
puts "this is the return value of the current implementation:"
current_implementation
puts "\nthis is the proposed change:"
proposed_fix
end
__END__
call signature:
a.succeed(1, 2, [:foo, :bar])
b.succeed(:single_arg)
this is the return value of the current implementation:
a rval: 1
b rval: :single_arg
this is the proposed change:
a rval: [1, 2, [:foo, :bar]]
b rval: :single_arg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment