Module: Blur::Callbacks

Included in:
Client
Defined in:
library/blur/callbacks.rb

Constant Summary collapse

@@callbacks =

Our list of callbacks.

{}

Instance Method Summary collapse

Instance Method Details

#callbacksObject

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.

Parameters:

  • name (Symbol)

    The event name.

  • args (optional, Array)

    The list of arguments to pass.



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.message}"
      STDERR.puts nil, 'Backtrace:', '---', exception.backtrace
    end
  end
end

#on(name) {|args, ...| ... } ⇒ Object

Add a new event callback.

Parameters:

  • name (Symbol)

    The event name.

Yields:

  • (args, ...)

    The arguments passed from #emit.



35
36
37
# File 'library/blur/callbacks.rb', line 35

def on name, &block
  (@@callbacks[name] ||= []) << block
end