Fixed example and added an example program

This commit is contained in:
2024-09-09 12:11:47 +01:00
parent 12024e3156
commit 087bf1952d
2 changed files with 62 additions and 8 deletions

View File

@@ -11,6 +11,8 @@ go get -u gitlab.com/naterciom/gubgub
## Example
Ignoring errors for code brevity!
```Go
package main
@@ -31,20 +33,16 @@ func consumer(msg MyMessage) {
}
func main() {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()
topic := gubgub.NewAsyncTopic[MyMessage]()
defer topic.Close() // Returns after all messages are delivered
topic := gubgub.NewAsyncTopic[MyMessage](ctx)
topic.Subscribe(gubgub.Forever(consumer))
_ := topic.Subscribe(gubgub.Forever(consumer))
// The AsyncTopic doesn't wait for the subscriber to be registered so, for the purposes of this
// example, we sleep on it.
time.Sleep(time.Millisecond)
topic.Publish(MyMessage{Name: "John Smith"})
<-ctx.Done()
_ := topic.Publish(MyMessage{Name: "John Smith"}) // Returns immediately
}
```
## Topics