Module: Blur::Callbacks

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

Instance Method Summary collapse

Instance Method Details

#callbacksObject

Get a list of callbacks registered.



8
9
10
# File 'library/blur/callbacks.rb', line 8

def callbacks
  @callbacks ||= {}
end

#emit(name, *args) ⇒ true, false

Emit a new event with given arguments.



17
18
19
20
21
22
23
24
25
# File 'library/blur/callbacks.rb', line 17

def emit name, *args
  # Trigger callbacks in scripts before triggering events in the client.
  notify_scripts name, *args

  matching_callbacks = callbacks[name]
  return false unless matching_callbacks&.any?

  matching_callbacks.each { |callback| callback.call *args }
end

#notify_scripts(name, *args) ⇒ Object (protected)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'library/blur/callbacks.rb', line 37

def notify_scripts name, *args
  scripts = @scripts.values.select { |script| script.class.events.key? name }
  scripts.each do |script|
    script.class.events[name].each do |method|
      if method.is_a? Proc
        method.call script, *args
      else
        script.__send__ method, *args
      end
    end
  rescue StandardError => e
    warn "#{e.class}: #{e.message}"
    warn nil, 'Backtrace:', '---', e.backtrace
  end
end

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

Add a new event callback.

Yields:

  • (args, ...)

    The arguments passed from #emit.



31
32
33
# File 'library/blur/callbacks.rb', line 31

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