Class: Cura::Event::Dispatcher
- Inherits:
-
Object
- Object
- Cura::Event::Dispatcher
- Defined in:
- lib/cura/event/dispatcher.rb
Overview
Polls or peeks for events since the last execution and dispatches them to the appropriate component.
Instance Attribute Summary collapse
-
#middleware ⇒ Array
readonly
The middleware stack which an event will pass through before being dispatched.
-
#target ⇒ Cura::Attributes::HasEvents
Get the object with an event handler to dispatch events to.
Attributes included from Attributes::HasApplication
Instance Method Summary collapse
-
#dispatch_event(event, options = {}) ⇒ Event::Base
Send the event through the middleware stack and dispatch all events on the queue.
-
#initialize(attributes = {}) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
-
#peek(milliseconds = 100) ⇒ nil, Event::Base
Wait a set amount of time for an event.
-
#poll ⇒ Event::Base
Wait forever for an event.
-
#run ⇒ Event::Dispatcher
Poll or peek for events and dispatch it if one was found.
-
#wait_time ⇒ Integer
Get the time to wait for events in milliseconds in the run loop.
-
#wait_time=(value) ⇒ Integer
Set the time to wait for events in milliseconds in the run loop.
Methods included from Attributes::HasAttributes
Constructor Details
#initialize(attributes = {}) ⇒ Dispatcher
Returns a new instance of Dispatcher.
18 19 20 21 22 23 24 25 26 |
# File 'lib/cura/event/dispatcher.rb', line 18 def initialize(attributes={}) super raise ArgumentError, "application must be set" if @application.nil? @wait_time = 100 @target = @application if @target.nil? @middleware = [] end |
Instance Attribute Details
#middleware ⇒ Array (readonly)
The middleware stack which an event will pass through before being dispatched. Middleware must be an object responding to #call.
73 74 75 |
# File 'lib/cura/event/dispatcher.rb', line 73 def middleware @middleware end |
#target ⇒ Cura::Attributes::HasEvents
Get the object with an event handler to dispatch events to.
51 52 53 |
# File 'lib/cura/event/dispatcher.rb', line 51 def target @target end |
Instance Method Details
#dispatch_event(event, options = {}) ⇒ Event::Base
Send the event through the middleware stack and dispatch all events on the queue.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cura/event/dispatcher.rb', line 104 def dispatch_event(event, ={}) event = Event.new_from_name(event, ) if event.respond_to?(:to_sym) raise TypeError, "event must be an Event::Base" unless event.is_a?(Event::Base) = { dispatcher: self, event: event, dispatch_queue: [] }.merge(.to_h) @middleware.each { |middleware| middleware.call() } [:dispatch_queue].each(&:dispatch) end |
#peek(milliseconds = 100) ⇒ nil, Event::Base
Wait a set amount of time for an event.
95 96 97 |
# File 'lib/cura/event/dispatcher.rb', line 95 def peek(milliseconds=100) @application.adapter.peek_event(milliseconds.to_i) end |
#poll ⇒ Event::Base
Wait forever for an event.
87 88 89 |
# File 'lib/cura/event/dispatcher.rb', line 87 def poll @application.adapter.poll_event end |
#run ⇒ Event::Dispatcher
Poll or peek for events and dispatch it if one was found.
78 79 80 81 82 |
# File 'lib/cura/event/dispatcher.rb', line 78 def run event = @wait_time == 0 ? poll : peek(@wait_time) dispatch_event(event) unless event.nil? end |
#wait_time ⇒ Integer
Get the time to wait for events in milliseconds in the run loop.
|
# File 'lib/cura/event/dispatcher.rb', line 28
|
#wait_time=(value) ⇒ Integer
Set the time to wait for events in milliseconds in the run loop. Set to 0 to wait forever (poll instead of peek).
40 41 42 43 44 45 |
# File 'lib/cura/event/dispatcher.rb', line 40 attribute(:wait_time) do |value| value = value.to_i value = 0 if value < 0 value end |