Documentation
¶
Overview ¶
Package pubsub provides a simple publish/subscribe mechanism.
Example ¶
// Create a new topic.
t := New[int]()
// Subscribe to changes.
changes := t.Subscribe(nil)
go func() {
for change := range changes {
fmt.Println("change:", change)
}
}()
// Publish a value.
t.Publish(1)
// Publish a value and wait for it to be received.
t.Publish(2)
time.Sleep(time.Millisecond * 100)
Output: change: 1 change: 2
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Topic ¶
type Topic[T any] struct { // contains filtered or unexported fields }
func (*Topic[T]) Subscribe ¶
func (s *Topic[T]) Subscribe(c chan T) chan T
Subscribe a channel to the topic.
The channel will be closed when the topic is closed.
If "c" is nil a new channel of size 16 will be created.
func (*Topic[T]) Unsubscribe ¶
func (s *Topic[T]) Unsubscribe(c chan T)
Unsubscribe a channel from the topic, closing the channel.
Click to show internal directories.
Click to hide internal directories.