Created
June 2, 2014 17:00
-
-
Save WA9ACE/57341304bb38032dc438 to your computer and use it in GitHub Desktop.
Event Library Tutorial
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
module EventMapper | |
def on(event, &block) | |
if @events.nil? | |
@events = { event => [block] } | |
elsif @events[event].nil? | |
@events[event] = [block] | |
else | |
@events[event] << block | |
end | |
end | |
def fire(event, *args) | |
if args.empty? | |
@events[event].each do |proc| | |
Thread.new { proc.call } | |
end | |
else | |
@events[event].each do |proc| | |
Thread.new { proc.call *args } | |
end | |
end | |
end | |
end | |
class Thing | |
include EventMapper | |
end | |
t = Thing.new | |
t.on :test do | |
puts "testing" | |
end | |
t.on :test_with_params do |thing1, thing2| | |
puts "I was passed #{thing1}, and #{thing2}" | |
end | |
t.fire :test | |
t.fire :test_with_params, "Hallo", "hooman" | |
sleep 0.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment