Class: CodeSunset::EventBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/code_sunset/event_buffer.rb

Instance Method Summary collapse

Constructor Details

#initialize ⇒ EventBuffer

Returns a new instance of EventBuffer.



3
4
5
6
# File 'lib/code_sunset/event_buffer.rb', line 3

def initialize
  @payloads = []
  @mutex = Mutex.new
end

Instance Method Details

#drain(limit: nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/code_sunset/event_buffer.rb', line 17

def drain(limit: nil)
  @mutex.synchronize do
    count = limit ? [limit.to_i, 0].max : @payloads.size
    @payloads.shift(count)
  end
end

#size ⇒ Object



24
25
26
# File 'lib/code_sunset/event_buffer.rb', line 24

def size
  @mutex.synchronize { @payloads.size }
end

#store(payload, limit:) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/code_sunset/event_buffer.rb', line 8

def store(payload, limit:)
  @mutex.synchronize do
    return false if @payloads.size >= limit

    @payloads << payload.deep_dup
    true
  end
end