Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created July 13, 2011 10:17
Show Gist options
  • Save mashiro/1080043 to your computer and use it in GitHub Desktop.
Save mashiro/1080043 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'eventmachine'
class UpdateEvent
include EM::Deferrable
def fire(msg)
p "update: #{msg}"
end
def canceled(msg)
p "cancel: #{msg}"
end
end
def enqueue(msg, sec = 3)
defer = UpdateEvent.new
defer.callback do |msg|
defer.fire msg
end
defer.errback do
defer.canceled msg
end
EM.add_timer(sec) do
defer.succeed msg
end
defer
end
EM.run do
d1 = enqueue 'hogehoge'
d2 = enqueue 'fugafuga'
sleep 2
d2.fail # cancel
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment