Class: Startback::Event::Bus

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

Overview

Sync and async bus abstraction allowing to register listeners and emitting events towards them.

This bus actually decorates two busses, one in synchronous and the other one is asynchronous (optional).

  • A synchronous bus MUST call the listeners as part of emitting process, and MUST re-raise any error occuring during that process. See, e.g. Startback::Bus::Memory::Sync

  • An asynchronous bus MAY call the listeners later, but MUST hide errors to the emitter. See, e.g. Startback::Bus::Memory::Async

This bus facade emits events to both sync and async busses (if any), and listen on the sync one by default.

For emitters:

# This will synchronously call every listeners who `listen`
# on the synchronous bus (& reraise exceptions) then call
# (possibly later) all listeners who `listen` on the
# asynchronous bus if any (& hide exceptions).
bus.emit(event)

# This only reaches sync listeners
bus.sync.emit(event)

# This only reaches async listeners (an async bus must be set)
bus.async.emit(event)

Please note that there is currently no way to reach sync listeners without having to implement error handling on the emitter side.

For listeners:

# This will listen synchronously and make the emitter fail if
# anything goes wrong with the callback:
bus.listen(event_type) do |event|
  ...
end

# It is a shortcut for:
bus.sync.listen(event_type) do |event| ... end

This will listen asynchronously and could not make the emitter
fail if something goes wrong with the callback.
bus.async.listen(event_type) do |event|
  ...
end

Feel free to access the sync and async busses directly for specific cases though.

Defined Under Namespace

Modules: Bunny, Memory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Robustness

#log, #monitor, #stop_errors, #try_max_times

Constructor Details

#initialize(sync = Memory::Sync.new, async = nil) ⇒ Bus

Returns a new instance of Bus.



61
62
63
64
# File 'lib/startback/event/bus.rb', line 61

def initialize(sync = Memory::Sync.new, async = nil)
  @sync = sync
  @async = async
end

Instance Attribute Details

#asyncObject (readonly)

Returns the value of attribute async.



65
66
67
# File 'lib/startback/event/bus.rb', line 65

def async
  @async
end

#syncObject (readonly)

Returns the value of attribute sync.



65
66
67
# File 'lib/startback/event/bus.rb', line 65

def sync
  @sync
end

Instance Method Details

#connectObject



67
68
69
70
# File 'lib/startback/event/bus.rb', line 67

def connect
  sync.connect if sync
  async.connect if async
end

#connected?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'lib/startback/event/bus.rb', line 72

def connected?
  if sync && async
    sync.connected? && async.connected?
  elsif sync
    sync.connected?
  elsif async
    async.connected?
  end
end

#emit(event) ⇒ Object

Emits a particular event to the listeners.



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/startback/event/bus.rb', line 86

def emit(event)
  monitor({
    op: "Startback::Bus#emit",
    op_data: {
      event: { type: event.type }
    }
  }, event.context) do
    sync.emit(event)
    async.emit(event) if async
  end
end

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

Registers ‘listener` as being interested in receiving events of a specific type.



103
104
105
# File 'lib/startback/event/bus.rb', line 103

def listen(type, processor = nil, listener = nil, &bl)
  sync.listen(type, processor, listener, &bl)
end