Created
February 21, 2014 19:12
-
-
Save JAndritsch/9141200 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
class EventBus | |
@@events = {} | |
def self.register(name, callback) | |
@@events[name] ||= [] | |
@@events[name] << callback | |
end | |
def self.publish(name, *args) | |
return if @@events[name].nil? | |
@@events[name].each do |callback| | |
callback.call(*args) | |
end | |
end | |
end | |
# register | |
EventBus.register("testing", Proc.new { |a| puts "testing was executed once: #{a}" }) | |
EventBus.register("testing", Proc.new { |a, b| puts "testing was executed again: #{a}, #{b}" }) | |
# publish | |
EventBus.publish("testing", "arg 1", "arg2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment