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)


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

#middlewareArray (readonly)

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

Returns:

  • (Array)


73
74
75
# File 'lib/cura/event/dispatcher.rb', line 73

def middleware
  @middleware
end

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

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)


104
105
106
107
108
109
110
111
112
113
# File 'lib/cura/event/dispatcher.rb', line 104

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:



95
96
97
# File 'lib/cura/event/dispatcher.rb', line 95

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

#pollEvent::Base

Wait forever for an event.

Returns:



87
88
89
# File 'lib/cura/event/dispatcher.rb', line 87

def poll
  @application.adapter.poll_event
end

#runEvent::Dispatcher

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

Returns:



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_timeInteger

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

Returns:

  • (Integer)


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

Parameters:

  • value (#to_i)

Returns:

  • (Integer)


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