Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmitterClosed = errors.New("streaming emitter closed")
ErrEmitterClosed is returned when attempting to emit after the emitter is closed.
Functions ¶
func Emit ¶
Emit sends data through the context-bound emitter. Returns nil if no emitter is present in the context. Returns an error if emission fails (context canceled, emitter closed, etc.).
func EmitOrCollect ¶
EmitOrCollect either emits the item (if streaming) or appends it to the slice. Returns the updated slice and any error from emission. This helper reduces duplication in services that support both streaming and buffered output.
Usage:
items, err := streaming.EmitOrCollect(ctx, item, items)
if err != nil {
return partialResult, nil
}
func IsStreaming ¶
IsStreaming returns true if a streaming emitter is attached to the context.
Types ¶
type Emitter ¶
type Emitter interface {
// Emit sends a data item through the stream.
// Returns an error if the context is canceled or the emitter is closed.
Emit(ctx context.Context, data any) error
// Close signals that no more items will be sent.
// The optional error is passed to the consumer as the final error state.
Close(err error)
}
Emitter sends data items to a consumer.
func FromContext ¶
FromContext retrieves the emitter from the context, if present.
func NewChannelEmitter ¶
NewChannelEmitter creates a new channel-based emitter. The buffer parameter controls the channel buffer size; values <= 0 use the default. Returns the emitter and a receive-only channel for consuming items.
type Item ¶
type Item struct {
// Data is the actual payload being streamed.
Data any
// Err indicates an error that occurred during streaming (non-fatal).
Err error
// Done signals that streaming has completed.
Done bool
}
Item represents a single piece of data emitted through the streaming channel.