Class: Async::Bus

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/async/bus.rb

Instance Method Summary collapse

Instance Method Details

#async_subscribe(pattern, parent: Async::Task.current) ⇒ Object

NON-BLOCKING, runs subscriber in a task



22
23
24
25
26
27
28
29
30
# File 'lib/async/bus.rb', line 22

def async_subscribe(pattern, parent: Async::Task.current)
  subscribe(pattern) do |event|
    parent.async  do
      yield(event)
    rescue StandardError => e
      log_error(pattern, e)
    end
  end
end

#convert(from_event, to_event) ⇒ Object



32
# File 'lib/async/bus.rb', line 32

def convert(from_event, to_event) = subscribe(from_event) { publish(to_event, **yield(_1)) }

#publish(name, *args, **params) ⇒ Object

BLOCKING unless subscribers run in tasks



8
9
10
11
12
# File 'lib/async/bus.rb', line 8

def publish(name, *args, **params)
  ActiveSupport::Notifications.instrument(name, payload: (args.first || params))
rescue StandardError => e
  log_error(name, e)
end

#subscribe(pattern) ⇒ Object

NON-BLOCKING



15
16
17
18
19
# File 'lib/async/bus.rb', line 15

def subscribe(pattern)
  ActiveSupport::Notifications.subscribe(pattern) do |name, _start, _finish, _id, params|
    yield params[:payload], name
  end
end