Created
July 13, 2011 10:17
-
-
Save mashiro/1080043 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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