Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel[T any] struct { // C is a writable channel on which you can send messages which will // be delivered to all connected listeners. C chan<- T // contains filtered or unexported fields }
Channel represents a multicast channel container which provides a writable channel C and allows multiple listeners to be connected.
func From ¶
From creates a new multicast channel which exposes messages it receives on the provided channel to all connected listeners.
Example ¶
source := make(chan interface{}) c := multicast.From(source) wg := sync.WaitGroup{} wg.Add(2) go func() { l := c.Listen() wg.Done() defer wg.Done() for msg := range l.C { fmt.Printf("Listener 1: %s\n", msg) } }() go func() { l := c.Listen() wg.Done() defer wg.Done() for msg := range l.C { fmt.Printf("Listener 2: %s\n", msg) } }() wg.Wait() wg.Add(2) source <- "Hello World!" close(source) wg.Wait()
Output: Listener 1: Hello World! Listener 2: Hello World!
func New ¶
New creates a new multicast channel container which can have listeners connected to it and messages sent via its C channel property.
Example ¶
c := multicast.New[string]() wg := sync.WaitGroup{} wg.Add(2) go func() { l := c.Listen() wg.Done() defer wg.Done() for msg := range l.C { fmt.Printf("Listener 1: %s\n", msg) } }() go func() { l := c.Listen() wg.Done() defer wg.Done() for msg := range l.C { fmt.Printf("Listener 2: %s\n", msg) } }() wg.Wait() wg.Add(2) c.C <- "Hello World!" c.Close() wg.Wait()
Output: Listener 1: Hello World! Listener 2: Hello World!
func (*Channel[T]) Close ¶
func (c *Channel[T]) Close()
Close is a convenience function for closing the top level channel. You may also close the channel directly by using `close(c.C)`.
Example ¶
c := multicast.New[struct{}]() wg := sync.WaitGroup{} wg.Add(1) go func() { l := c.Listen() for range l.C { } fmt.Println("Listener closed") wg.Done() }() c.Close() wg.Wait()
Output: Listener closed
type Listener ¶
type Listener[T any] struct { C <-chan T // contains filtered or unexported fields }
Listener represents a listener which will receive messages from a channel.
Example ¶
m := multicast.New[string]() l := m.Listen() wg := sync.WaitGroup{} wg.Add(1) go func() { for msg := range l.C { fmt.Printf("Listener got: %#v\n", msg) } wg.Done() }() m.C <- "Hello!" m.Close() wg.Wait()
Output: Listener got: "Hello!"
func NewListener ¶
NewListener creates a new listener which will forward messages it receives on its f channel before exposing them on its C channel. You will very rarely need to use this method directly in your applications, prefer using From instead.
Example ¶
s := make(chan interface{}) wg := sync.WaitGroup{} wg.Add(1) go func() { l := multicast.NewListener(s) fmt.Printf("Listener got: %s\n", <-l.C) wg.Done() }() s <- "Hello World!" close(s) wg.Wait()
Output: Listener got: Hello World!
func (*Listener[T]) Chain ¶
Chain is a shortcut which updates an existing listener to forward to a new listener and then returns the new listener. You will generally not need to make use of this method in your applications.
Example ¶
s := make(chan interface{}) wg := sync.WaitGroup{} wg.Add(1) l1 := multicast.NewListener(s) go func() { fmt.Printf("Listener 1: %s\n", <-l1.C) wg.Done() }() wg.Add(1) l2 := l1.Chain() go func() { fmt.Printf("Listener 2: %s\n", <-l2.C) wg.Done() }() s <- "Hello World!" close(s) wg.Wait()
Output: Listener 1: Hello World! Listener 2: Hello World!