module Blur::Callbacks

Public Instance Methods

callbacks() click to toggle source

Get a list of callbacks registered.

@returns [Array] the list of callbacks

# File library/blur/callbacks.rb, line 8
def callbacks
  @callbacks ||= {}
end
emit(name, *args) click to toggle source

Emit a new event with given arguments.

@param name [Symbol] The event name. @param args [optional, Array] The list of arguments to pass. @return [true, false] True if any callbacks were invoked, nil otherwise

# File library/blur/callbacks.rb, line 17
def emit name, *args
  # Trigger callbacks in scripts before triggering events in the client.
  EM.defer { notify_scripts name, *args }

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

  EM.defer do
    matching_callbacks.each { |callback| callback.call *args }
  end
end
on(name, &block) click to toggle source

Add a new event callback.

@param name [Symbol] The event name. @yield [args, …] The arguments passed from emit.

# File library/blur/callbacks.rb, line 33
def on name, &block
  (callbacks[name] ||= []) << block
end

Protected Instance Methods

notify_scripts(name, *args) click to toggle source
# File library/blur/callbacks.rb, line 39
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