Module Rubygame::MailQueue
In: lib/rubygame/queue.rb

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.

Methods

deliver   list   new   push   setup   subscribe   subscribed?   unsubscribe  

Attributes

autodeliver  [RW]  Whether to automatically deliver events as they are received. Enabled by default.

Public Class methods

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

[Source]

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

Public Instance methods

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.

[Source]

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

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

[Source]

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

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

[Source]

# 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

Create the necessary internal variables for the MailQueue.

[Source]

# File lib/rubygame/queue.rb, line 210
    def setup
      @subscribe = Hash.new
      @autodeliver = true
    end

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.

[Source]

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

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

[Source]

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

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.

[Source]

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

[Validate]