{"modulePath":"github.com/floatdrop/chan","version":"v0.1.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package batch\n\nPackage batch groups values received from a channel into slices.\n\nA batch is sent when it reaches its size, when its timeout expires (see [WithTimeout](#WithTimeout)), when a split function says the next value starts a new batch (see [NewFunc](#NewFunc)), or when the input channel is closed. Batches are never empty and never share memory: the receiver owns every slice it gets.\n\n## Functions\n\n```go\nfunc New[T any](ctx context.Context, in \u003c-chan T, size int, opts ...Option) \u003c-chan []T\n```\nNew reads values from in and sends them in batches of up to size values to the returned channel, in the order they were received.\n\nWhen in is closed, New sends the last, possibly shorter, batch and closes the channel. When ctx is done, New stops reading and closes the channel; the batch it was filling is discarded. To shut down without losing values, close in.\n\nNew panics if size is less than 1.\n\n```go\nfunc NewFunc[T any](ctx context.Context, in \u003c-chan T, size int, split func(prev, next T) bool, opts ...Option) \u003c-chan []T\n```\nNewFunc is like [New](#New), but it also ends a batch where split reports true: split is called with the last value of the current batch and the next value, and if it returns true the current batch is sent and next starts a new one.\n\nTo group consecutive values that share a key, split on a change of key:\n\n\tfunc(prev, next Event) bool { return prev.UserID != next.UserID }\n\n## Types\n\n```go\ntype Option func(*config)\n```\nOption configures [New](#New) and [NewFunc](#NewFunc).\n\n```go\nfunc WithBuffer(n int) Option\n```\nWithBuffer sets the capacity of the output channel. The default is 0: the channel is unbuffered. WithBuffer panics if n is negative.\n\n```go\nfunc WithTimeout(d time.Duration) Option\n```\nWithTimeout bounds how long a batch can stay unfinished: once d has passed since its first value arrived, the batch is sent even if it is not full. Without this option, a batch waits until it is full or the input is closed. To batch by time alone, pass math.MaxInt as the size.\n\nWithTimeout panics if d is not positive.\n\n","path":"github.com/floatdrop/chan/batch","name":"batch","synopsis":"Package batch groups values received from a channel into slices.","isRedistributable":true}
