Skip to content

Commit

Permalink
Add README example
Browse files Browse the repository at this point in the history
  • Loading branch information
maddiesch committed Feb 26, 2024
1 parent 025df89 commit f615f88
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,21 @@
[![License](https://img.shields.io/github/license/maddiesch/go-bus)](./LICENSE)

A multi-producer multi-consumer generic event bus.

```go
eventBus := bus.New[string]()

subscription, cancel := eventBus.Sink()
defer cancel()

go produceEvents(eventBus)

for event := range subscription {
switch event {
case "stop":
return
default:
fmt.Println(event)
}
}
```
25 changes: 25 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,28 @@ func ExampleBus() {
fmt.Println(message)
// Output: Hello, World!
}

func ExampleBus_Sink() {
eventBus := bus.New[string]()

subscription, cancel := eventBus.Sink()
defer cancel()

go produceEvents(eventBus)

for event := range subscription {
switch event {
case "stop":
return
default:
fmt.Println(event)
}
}

// Output: Hello, World!
}

func produceEvents(eventBus *bus.Bus[string]) {
eventBus.Publish("Hello, World!")
eventBus.Publish("stop")
}

0 comments on commit f615f88

Please sign in to comment.