Class: TCB::EventBus::RunningStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/tcb/event_bus/running_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(event_bus, sync: false) ⇒ RunningStrategy

Returns a new instance of RunningStrategy.



6
7
8
9
# File 'lib/tcb/event_bus/running_strategy.rb', line 6

def initialize(event_bus, sync: false)
  @event_bus = event_bus
  @sync = sync
end

Instance Method Details

#publish(event) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/tcb/event_bus/running_strategy.rb', line 25

def publish(event)
  if @sync
    @event_bus.dispatch(event)
  else
    @event_bus.queue << event
  end
  event
end

#shutdown?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/tcb/event_bus/running_strategy.rb', line 38

def shutdown?
  false
end

#startObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/tcb/event_bus/running_strategy.rb', line 11

def start
  return if @sync

  @event_bus.dispatcher = Thread.new do
    loop do
      event = @event_bus.queue.pop
      break if event == :shutdown_sentinel

      @event_bus.dispatch(event)
      @event_bus.dispatch(build_pressure_event) if @event_bus.high_water_mark_reached?
    end
  end
end

#subscribe(event_class, &block) ⇒ Object



34
35
36
# File 'lib/tcb/event_bus/running_strategy.rb', line 34

def subscribe(event_class, &block)
  @event_bus.registry.add(event_class, block)
end