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
Dispatch an event to the target or application, if the target is nil.
-
#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.
22 23 24 25 26 27 28 29 30 |
# File 'lib/cura/event/dispatcher.rb', line 22 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.
77 78 79 |
# File 'lib/cura/event/dispatcher.rb', line 77 def middleware @middleware end |
#target ⇒ Cura::Attributes::HasEvents
Get the object with an event handler to dispatch events to.
55 56 57 |
# File 'lib/cura/event/dispatcher.rb', line 55 def target @target end |
Instance Method Details
#dispatch_event(event, options = {}) ⇒ Event::Base
Dispatch an event to the target or application, if the target is nil.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cura/event/dispatcher.rb', line 108 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.
99 100 101 |
# File 'lib/cura/event/dispatcher.rb', line 99 def peek(milliseconds=100) @application.adapter.peek_event(milliseconds.to_i) end |
#poll ⇒ Event::Base
Wait forever for an event.
91 92 93 |
# File 'lib/cura/event/dispatcher.rb', line 91 def poll @application.adapter.poll_event end |
#run ⇒ Event::Dispatcher
Poll or peek for events and dispatch it if one was found.
82 83 84 85 86 |
# File 'lib/cura/event/dispatcher.rb', line 82 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 32
|
#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).
44 45 46 47 48 49 |
# File 'lib/cura/event/dispatcher.rb', line 44 attribute(:wait_time) do |value| value = value.to_i value = 0 if value < 0 value end |