class Popro::Indicator::Rails::ServerSentEvent

Public Class Methods

default_formatter(info, yielded) click to toggle source
# File lib/popro/indicator/rails.rb, line 37
def self.default_formatter(info, yielded)
  info.to_h.merge(yielded: yielded)
end
new(response, **options) click to toggle source
# File lib/popro/indicator/rails.rb, line 15
def initialize(response, **options)
  response.status = 200
  response.headers.merge!(options.delete(:headers) || DEFAULT_HEADERS)
  formatter = options.delete(:formatter) || self.class.method(:default_formatter)

  @stream = ::ActionController::Live::SSE.new(response.stream, **options)
  @formatter = formatter
  @response = response
end

Public Instance Methods

call(*args) click to toggle source
# File lib/popro/indicator/rails.rb, line 25
def call(*args)
  @stream.write(@formatter.call(*args))
rescue ::ActionController::Live::ClientDisconnected
  # ignore disconnections
end
finish() click to toggle source
# File lib/popro/indicator/rails.rb, line 31
def finish
  @stream.close
rescue ::ActionController::Live::ClientDisconnected
  # ignore disconnections
end