Class: Akasha::Storage::MemoryEventStore::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/akasha/storage/memory_event_store/stream.rb

Overview

Memory-based event stream.

Instance Method Summary collapse

Constructor Details

#initialize(&before_write) ⇒ Stream

Creates a new event stream. Accepts an optional block, allowing for filtering new events and triggering side-effects, before new events are appended to the stream,


9
10
11
12
# File 'lib/akasha/storage/memory_event_store/stream.rb', line 9

def initialize(&before_write)
  @before_write = before_write || identity
  @events = []
end

Instance Method Details

#read_events(start, page_size, &block) ⇒ Object

Reads events from the stream starting from `start` inclusive. If block given, reads all events from the start in pages of `page_size`. If block not given, reads `page_size` events from the start.


22
23
24
25
26
27
28
# File 'lib/akasha/storage/memory_event_store/stream.rb', line 22

def read_events(start, page_size, &block)
  if block_given?
    @events.lazy.drop(start).each_slice(page_size, &block)
  else
    @events[start..start + page_size]
  end
end

#write_events(events) ⇒ Object

Appends events to the stream.


15
16
17
# File 'lib/akasha/storage/memory_event_store/stream.rb', line 15

def write_events(events)
  @events += @before_write.call(events)
end