The Flow library provides a thread-safe implementation for managing asynchronous data flows.
It includes several implementations, such as Stream, Repeater, Never, and Empty, each with different behaviors.
Include the library in your Go project:
go get github.com/bobcatalyst/flowA Stream represents a continuous flow of values that can be closed, pushed to, and listened to.
var s flow.Stream[int]
s.Push(1, 2, 3)
c := s.Listen(context.Background())
s.Push(4, 5, 6)
for val := range c {
fmt.Println(val)
}4
5
6
A Repeater repeats past values.
var r flow.Repeater[int]
r.Push(1, 2, 3)
c := s.Listen(context.Background())
r.Push(4, 5, 6)
for val := range c {
fmt.Println(val)
}1
2
3
4
5
6
A Never flow never emits any values and only closes when the context is canceled.
var n flow.Never[int]
c := n.Listen(context.Background())
// Will block until the context is canceled.An Empty flow immediately closes without emitting any values.
var e flow.Empty[int]
c := e.Listen(context.Background())
// Channel `c` is already closed.