Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created September 5, 2008 19:40
Show Gist options
  • Save tmm1/9008 to your computer and use it in GitHub Desktop.
Save tmm1/9008 to your computer and use it in GitHub Desktop.
EM::Timer#reset and #postpone (don't use these, they leak memory)
require 'rubygems'
require 'eventmachine'
class EventMachine::Timer
def reset seconds
prev = EM.instance_variable_get('@timers')[@signature]
cancel
initialize(seconds){ prev.call }
end
def postpone seconds
prev = EM.instance_variable_get('@timers')[@signature]
EM.instance_variable_get('@timers')[@signature] = proc{
EM.add_timer(seconds){ prev.call }
}
end
end
EM.run{
p Time.now
t = EM::Timer.new(1){ p Time.now }
t.postpone(1)
t.postpone(1)
u = EM::Timer.new(5){ p Time.now }
u.reset(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment