Class: InstantRecord::EventsController::EventStream

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/instant_record/events_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(cursor:, deadline:) ⇒ EventStream

Returns a new instance of EventStream.



22
23
24
25
# File 'app/controllers/instant_record/events_controller.rb', line 22

def initialize(cursor:, deadline:)
  @cursor = cursor
  @deadline = deadline
end

Instance Method Details

#eachObject



27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/instant_record/events_controller.rb', line 27

def each
  loop do
    Change.poll(@cursor).each do |change|
      @cursor = change.id
      yield "id: #{change.id}\nevent: change\ndata: #{change.as_event.to_json}\n\n"
    end

    break if Time.current >= @deadline
    sleep 0.5
  end
end