Class: Startback::Event::Bus::Memory::Async

Inherits:
Object
  • Object
show all
Includes:
Support::Robustness
Defined in:
lib/startback/event/bus/memory/async.rb

Overview

Asynchronous implementation of the Bus abstraction, for use between components sharing the same process.

This implementation actually calls listeners synchronously (it mays) but hides error raised by them. See Bus::Bunny::Async for another implementation that is truly asynchronous and relies on RabbitMQ.

Constant Summary collapse

DEFAULT_OPTIONS =
{
}

Instance Method Summary collapse

Methods included from Support::Robustness

#log, #monitor, #stop_errors, #try_max_times

Constructor Details

#initialize(options = {}) ⇒ Async

Returns a new instance of Async.



19
20
21
22
# File 'lib/startback/event/bus/memory/async.rb', line 19

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @listeners = {}
end

Instance Method Details

#connectObject



24
25
# File 'lib/startback/event/bus/memory/async.rb', line 24

def connect
end

#emit(event) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/startback/event/bus/memory/async.rb', line 27

def emit(event)
  (@listeners[event.type.to_s] || []).each do |l|
    stop_errors(self, "emit", event) {
      l.call(event)
    }
  end
end

#listen(type, processor = nil, listener = nil, &bl) ⇒ Object

Raises:

  • (ArgumentError)


35
36
37
38
39
# File 'lib/startback/event/bus/memory/async.rb', line 35

def listen(type, processor = nil, listener = nil, &bl)
  raise ArgumentError, "A listener must be provided" unless listener || bl
  @listeners[type.to_s] ||= []
  @listeners[type.to_s] << (listener || bl)
end