module Rubygame::MailQueue

A mixin module to extend EventQueue with the ability to 'deliver' specific types of events to subscribed objects, a la a mailing list. Each object must subscribe for the classes of events it wishes to receive; when the MailQueue receives an event of that type, it will deliver it to the subscribers. See subscribe for more information.

Please note that if you extend an already-existing EventQueue object with this mixin module (rather than including it in a class), you must call setup before using the object. This will create the necessary internal variables for the MailQueue to work.

Attributes

autodeliver[RW]

Whether to automatically deliver events as they are received. Enabled by default.

Public Class Methods

new() click to toggle source

Create a new MailQueue object. Like Rubygame::EventQueue.new, this method will yield self if a block is given.

Calls superclass method
# File lib/rubygame/queue.rb, line 204
def initialize()
  setup()
  super
end

Public Instance Methods

deliver() click to toggle source

Deliver each pending event to all objects which are subscribed to that event class. Every client object MUST have a push method, or events can't be delivered to it, and it will become very lonely!

The queue will be cleared of all events after all deliveries are done.

# File lib/rubygame/queue.rb, line 290
def deliver()
  each() { |event|  deliver_event(event) }
  clear()
end
list() click to toggle source

Returns an Array of all event classes which have at least one subscriber.

# File lib/rubygame/queue.rb, line 216
def list
  @subscribe.collect { |k, v|  
    (v.length > 0) ? k : nil  rescue NoMethodError nil
  }.compact
end
push(*args) click to toggle source

Append events to the queue. If @autodeliver is enabled, all events on the queue will be delivered to subscribed client objects immediately.

Calls superclass method
# File lib/rubygame/queue.rb, line 297
def push(*args)
  # Temporarily disable autofetch to avoid infinite loop
  a, @autofetch = @autofetch, false
  # Fetch once to emulate autofetch, if it was enabled before
  fetch_sdl_events() if a

  super
  deliver() if @autodeliver

  @autofetch = a
  return
end
setup() click to toggle source

Create the necessary internal variables for the MailQueue.

# File lib/rubygame/queue.rb, line 210
def setup
  @subscribe = Hash.new
  @autodeliver = true
end
subscribe(client,klass) click to toggle source

Subscribe client to receive events that match klass.

After the client object has been subscribed, the MailQueue will push along any event for which “klass === event” is true. This usually means that the event is an instance of klass or one of klass's child classes; however, note that klass may have changed its own === operator to have different behavior, so this is not always the case.

Important: the MailQueue uses the client's push method to deliver events! If the client does not have such a method, MailQueue will silently catch the error and move on to the next client.

A client object may be subscribed for many different types of events simultaneously, and more than one client object may be subscribed to any type of event (in which case each object will receive the event). A client may also be subscribed multiple times for the same type (in which case it will receive duplicate events). Likewise, the client will receive duplicates if it is subscribed to multiple classes which share ancestry, for example Numeric and Float.

If a client wishes to receive ALL types of events, it can subscribe to Object, which is a parent class of all objects.

If the queue's @autodeliver is true, it will deliver events to subscribers immediately after they are posted, rather than waiting for deliver to be called.

# File lib/rubygame/queue.rb, line 248
def subscribe(client,klass)
  @subscribe[klass] << client
rescue NoMethodError
  @subscribe[klass] = [client] if @subscribe[klass].nil?
end
subscribed?(client,klass) click to toggle source

Returns true if client is currently subscribed to receive events of type klass.

# File lib/rubygame/queue.rb, line 256
def subscribed?(client,klass)
  return true if @subscribe[klass].include?(client)  rescue NoMethodError
  return false
end
unsubscribe(client,klass) click to toggle source

Unsubscribes the client to stop receiving events of type klass. It is safe (has no effect) to unsubscribe for an event type you are not subscribed to receive.

# File lib/rubygame/queue.rb, line 264
def unsubscribe(client,klass)
  @subscribe[klass] -= [client]  rescue NoMethodError
ensure
  return
end

Private Instance Methods

deliver_event(event) click to toggle source

This private method is used by deliver to do the real work.

# File lib/rubygame/queue.rb, line 271
def deliver_event(event)
  @subscribe.each_pair { |klass,clients|
    begin
      if klass === event
        clients.each do |client|
          client.push(event) rescue NoMethodError
        end 
      end
    rescue NoMethodError
    end
  }
end