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

#connected?Boolean

Returns:

  • (Boolean)


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

def connected?
  true
end

#emit(event) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/startback/event/bus/memory/async.rb', line 31

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)


39
40
41
42
43
# File 'lib/startback/event/bus/memory/async.rb', line 39

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