Class: Braintrust::Server::SSEStreamBody

Inherits:
Object
  • Object
show all
Defined in:
lib/braintrust/server/sse.rb

Overview

Rack 3 streaming response body that writes SSE events via ‘call(stream)`.

Required for servers using the protocol-rack adapter (e.g. Falcon), which dispatches ‘each`-based bodies through a buffered Enumerable path. Bodies that respond only to `call` are dispatched through the Streaming path for true async writes.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ SSEStreamBody

Returns a new instance of SSEStreamBody.



29
30
31
# File 'lib/braintrust/server/sse.rb', line 29

def initialize(&block)
  @block = block
end

Instance Method Details

#call(stream) ⇒ Object



33
34
35
36
37
38
# File 'lib/braintrust/server/sse.rb', line 33

def call(stream)
  writer = SSEWriter.new { |chunk| stream.write(chunk) }
  @block.call(writer)
ensure
  stream.close
end