1
0
mirror of https://github.com/NUTtech/bell.git synced 2024-11-19 20:31:41 +02:00

issue-32: Add an example of using a bell with context

This commit is contained in:
lowit 2022-04-29 13:08:41 +03:00 committed by lowitea
parent 571bef02be
commit b9a5586aa5

View File

@ -1,9 +1,11 @@
package bell_test
import (
"context"
"fmt"
"github.com/nuttech/bell/v2"
"sort"
"time"
)
type CustomStruct struct {
@ -12,7 +14,7 @@ type CustomStruct struct {
}
func Example() {
// Example of use via global state
// Use via global state
event := "event_name"
event2 := "event_name_2"
@ -56,7 +58,7 @@ func Example() {
}
func ExampleEvents() {
// Example of use struct (without global state)
// Use events object (without global state)
eventName := "event_name"
@ -75,3 +77,44 @@ func ExampleEvents() {
// Output:
// Hello bell!
}
func Example_usingContext() {
// Use bell with context
// create a custom struct for pass a context
type Custom struct {
ctx context.Context
value interface{}
}
// add listener
bell.Listen("event", func(message bell.Message) {
for iterationsCount := 1; true; iterationsCount++ {
select {
case <-message.(*Custom).ctx.Done():
return
default:
fmt.Printf("Iteration #%d\n", iterationsCount)
time.Sleep(10 * time.Second)
}
}
})
// create a global context for all calls
globalCtx, cancelGlobalCtx := context.WithCancel(context.Background())
// create a children context for a call with timeout
ringCtx, ringCancel := context.WithTimeout(globalCtx, time.Minute)
defer ringCancel()
_ = bell.Ring("event", &Custom{ringCtx, "value"})
// wait a second for the handler to perform one iteration
time.Sleep(time.Second)
// interrupt all handlers
cancelGlobalCtx()
// Output:
// Iteration #1
}