class Syndi::Events

A simple event management system, designed particularly for Syndi.

@!attribute [r] events

@return [Hash{Symbol => Array<Syndi::Events::Listener>}] The collection of events and associated listeners.

Attributes

events[R]

Public Class Methods

new() click to toggle source
# File lib/syndi/events.rb, line 16
def initialize
  @events = Hash.new
end

Public Instance Methods

emit(event, *parameters) click to toggle source

This will broadcast the given event, executing each listener and passing to it the parameters supplied here, respecting priority and operating in a thread.

@param [Symbol] event @param […] parameters The arguments to be passed to each listener.

# File lib/syndi/events.rb, line 49
def emit event, *parameters
  if @events[event]

    # collect the listeners with respect to priority
    one, two, three, four, five = gather @events[event]

    Syndi.log.verbose "event *#{event}* is being broadcasted on #{self}", VNOISY

    # spawn a thread and perform the executions
    Thread.new do
      begin
        # cease if status ever becomes false/nil
        status = true
        
        one.each   { |code| status = code.call *parameters if status }
        two.each   { |code| status = code.call *parameters if status }
        three.each { |code| status = code.call *parameters if status }
        four.each  { |code| status = code.call *parameters if status }
        five.each  { |code| status = code.call *parameters if status }
      rescue => e
        # catch thread errors
        Syndi.log.error "A listener to a broadcast of #{event} on #{self} caused an exception to rise (#{e})", true
      end
    end

  end
end
inspect() click to toggle source
# File lib/syndi/events.rb, line 77
def inspect
  "<#Syndi::Events: obj_id=#{object_id} event_count=#{@events.length}>"
end
Also aliased as: to_s
on(event, priority = 3, &prc) click to toggle source

Create a new listener on a given event, which will have the given block attached and executed upon the event's occurrence.

@param [Symbol] event The event which to await. @param [Integer] priority The priority this listener should have in the hook

execution procedure. Must be 1-5 with 1 being of utmost priority.

@yield […] The parameters passed when the event was broadcasted. This varies

by event. See the official event reference for Syndi events.

@return [Syndi::Events::Listener] The listener.

# File lib/syndi/events.rb, line 31
def on event, priority = 3, &prc
  @events[event] ||= Array.new

  if priority < 1 || priority > 5
    raise ArgumentError, "invalid event priority specified"
  end

  hook = Listener.new self, event, priority, prc
  @events[event] << hook
  hook
end
to_s()
Alias for: inspect

Private Instance Methods

gather(list) click to toggle source

Gather hooks.

# File lib/syndi/events.rb, line 85
def gather list
  [list.collect { |hook| (hook.priority == 1 ? hook : nil) }.compact,
   list.collect { |hook| (hook.priority == 2 ? hook : nil) }.compact,
   list.collect { |hook| (hook.priority == 3 ? hook : nil) }.compact,
   list.collect { |hook| (hook.priority == 4 ? hook : nil) }.compact,
   list.collect { |hook| (hook.priority == 5 ? hook : nil) }.compact]
end