Module: Blur::Callbacks
- Included in:
- Client
- Defined in:
- library/blur/callbacks.rb
Constant Summary collapse
- @@callbacks =
Our list of callbacks.
{}
Instance Method Summary collapse
-
#callbacks ⇒ Object
Get a list of callbacks registered.
-
#emit(name, *args) ⇒ Object
Emit a new event with given arguments.
- #notify_scripts(name, *args) ⇒ Object protected
-
#on(name) {|args, ...| ... } ⇒ Object
Add a new event callback.
Instance Method Details
#callbacks ⇒ Object
Get a list of callbacks registered.
11 12 13 |
# File 'library/blur/callbacks.rb', line 11 def callbacks @@callbacks end |
#emit(name, *args) ⇒ Object
Emit a new event with given arguments.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'library/blur/callbacks.rb', line 19 def emit name, *args EM.defer do notify_scripts name, *args end if (callbacks = @@callbacks[name]) and callbacks.any? EM.defer do callbacks.each{|callback| callback.call *args } end end end |
#notify_scripts(name, *args) ⇒ Object (protected)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'library/blur/callbacks.rb', line 41 def notify_scripts name, *args scripts = @scripts.values.select{|script| script.class.events.key? name } scripts.each do |script| begin script.class.events[name].each do |method| if method.is_a? Proc method.call script, *args else script.__send__ method, *args end end rescue => exception STDERR.puts "#{exception.class}: #{exception.}" STDERR.puts nil, 'Backtrace:', '---', exception.backtrace end end end |
#on(name) {|args, ...| ... } ⇒ Object
Add a new event callback.
35 36 37 |
# File 'library/blur/callbacks.rb', line 35 def on name, &block (@@callbacks[name] ||= []) << block end |