streaming

package
v1.0.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmitterClosed = errors.New("streaming emitter closed")

ErrEmitterClosed is returned when attempting to emit after the emitter is closed.

Functions

func Emit

func Emit(ctx context.Context, data any) error

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

func EmitOrCollect[T any](ctx context.Context, item T, slice []T) ([]T, error)

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

func IsStreaming(ctx context.Context) bool

IsStreaming returns true if a streaming emitter is attached to the context.

func WithEmitter

func WithEmitter(ctx context.Context, emitter Emitter) context.Context

WithEmitter attaches an emitter to the context. If the emitter is nil, the context is returned unchanged.

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

func FromContext(ctx context.Context) (Emitter, bool)

FromContext retrieves the emitter from the context, if present.

func NewChannelEmitter

func NewChannelEmitter(buffer int) (Emitter, <-chan Item)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL