Module: Rubygame::MailQueue
- Defined in:
- lib/rubygame/queue.rb
Overview
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.
Instance Attribute Summary collapse
-
#autodeliver ⇒ Object
Whether to automatically deliver events as they are received.
Instance Method Summary collapse
-
#deliver ⇒ Object
Deliver each pending event to all objects which are subscribed to that event class.
-
#initialize ⇒ Object
Create a new MailQueue object.
-
#list ⇒ Object
Returns an Array of all event classes which have at least one subscriber.
-
#push(*args) ⇒ Object
Append events to the queue.
-
#setup ⇒ Object
Create the necessary internal variables for the MailQueue.
-
#subscribe(client, klass) ⇒ Object
Subscribe
clientto receive events that matchklass. -
#subscribed?(client, klass) ⇒ Boolean
Returns true if
clientis currently subscribed to receive events of typeklass. -
#unsubscribe(client, klass) ⇒ Object
Unsubscribes the client to stop receiving events of type
klass.
Instance Attribute Details
#autodeliver ⇒ Object
Whether to automatically deliver events as they are received. Enabled by default.
200 201 202 |
# File 'lib/rubygame/queue.rb', line 200 def autodeliver @autodeliver end |
Instance Method Details
#deliver ⇒ Object
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.
290 291 292 293 |
# File 'lib/rubygame/queue.rb', line 290 def deliver() each() { |event| deliver_event(event) } clear() end |
#initialize ⇒ Object
Create a new MailQueue object. Like EventQueue.new, this method will yield self if a block is given.
204 205 206 207 |
# File 'lib/rubygame/queue.rb', line 204 def initialize() setup() super end |
#list ⇒ Object
Returns an Array of all event classes which have at least one subscriber.
216 217 218 219 220 |
# 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) ⇒ Object
Append events to the queue. If @autodeliver is enabled, all events on the queue will be delivered to subscribed client objects immediately.
297 298 299 300 301 302 303 304 305 306 307 308 |
# 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 ⇒ Object
Create the necessary internal variables for the MailQueue.
210 211 212 213 |
# File 'lib/rubygame/queue.rb', line 210 def setup @subscribe = Hash.new @autodeliver = true end |
#subscribe(client, klass) ⇒ Object
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.
248 249 250 251 252 |
# 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) ⇒ Boolean
Returns true if client is currently subscribed to receive events of type klass.
256 257 258 259 |
# 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) ⇒ Object
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.
264 265 266 267 268 |
# File 'lib/rubygame/queue.rb', line 264 def unsubscribe(client,klass) @subscribe[klass] -= [client] rescue NoMethodError ensure return end |