Class: Cura::Event::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Attributes::HasApplication, Attributes::HasAttributes, Attributes::HasInitialize
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

Attributes included from Attributes::HasApplication

#application

Instance Method Summary collapse

Methods included from Attributes::HasAttributes

included, #update_attributes

Constructor Details

#initialize(attributes = {}) ⇒ Dispatcher

Returns a new instance of Dispatcher.

Raises:

  • (ArgumentError)


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

#middlewareArray (readonly)

The middleware stack which an event will pass through before being dispatched. Middleware must be an object responding to #call.

Returns:

  • (Array)


77
78
79
# File 'lib/cura/event/dispatcher.rb', line 77

def middleware
  @middleware
end

#targetCura::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.

Parameters:

  • event (#to_sym)

    The name of the event class to create an instance of or an event instance.

  • options (#to_hash, #to_h) (defaults to: {})

    The options to pass through the middleware.

Returns:

Raises:

  • (TypeError)


108
109
110
111
112
113
114
115
116
117
# File 'lib/cura/event/dispatcher.rb', line 108

def dispatch_event(event, options={})
  event = Event.new_from_name(event, options) if event.respond_to?(:to_sym)
  raise TypeError, "event must be an Event::Base" unless event.is_a?(Event::Base)
  
  options = { dispatcher: self, event: event, dispatch_queue: [] }.merge(options.to_h)
  
  @middleware.each { |middleware| middleware.call(options) }
  
  options[:dispatch_queue].each(&:dispatch)
end

#peek(milliseconds = 100) ⇒ nil, Event::Base

Wait a set amount of time for an event.

Parameters:

  • milliseconds (#to_i) (defaults to: 100)

    The amount of time to wait in milliseconds.

Returns:



99
100
101
# File 'lib/cura/event/dispatcher.rb', line 99

def peek(milliseconds=100)
  @application.adapter.peek_event(milliseconds.to_i)
end

#pollEvent::Base

Wait forever for an event.

Returns:



91
92
93
# File 'lib/cura/event/dispatcher.rb', line 91

def poll
  @application.adapter.poll_event
end

#runEvent::Dispatcher

Poll or peek for events and dispatch it if one was found.

Returns:



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_timeInteger

Get the time to wait for events in milliseconds in the run loop.

Returns:

  • (Integer)


# 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).

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


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