Class: BBK::App::Dispatcher::MessageStream

Inherits:
Object
  • Object
show all
Defined in:
lib/bbk/app/dispatcher/message_stream.rb

Constant Summary collapse

CLOSE_VALUE =
:close

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: 10) ⇒ MessageStream

Returns a new instance of MessageStream.



11
12
13
14
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 11

def initialize(size: 10)
  @queue = SizedQueue.new(size)
  @closed = false
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 9

def queue
  @queue
end

#streamObject (readonly)

Returns the value of attribute stream.



9
10
11
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 9

def stream
  @stream
end

Instance Method Details

#closeObject



33
34
35
36
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 33

def close
  @closed = true
  @queue << CLOSE_VALUE
end

#eachObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 21

def each
  return to_enum unless block_given?
  return nil if @closed

  loop do
    value = @queue.pop
    break if value == CLOSE_VALUE

    yield(value)
  end
end

#push(message) ⇒ Object Also known as: <<



16
17
18
# File 'lib/bbk/app/dispatcher/message_stream.rb', line 16

def push(message)
  @queue.push(message) unless @closed
end