example_test.go 851 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package event
  2. import "fmt"
  3. func ExampleTypeMux() {
  4. type someEvent struct{ I int }
  5. type otherEvent struct{ S string }
  6. type yetAnotherEvent struct{ X, Y int }
  7. var mux TypeMux
  8. // Start a subscriber.
  9. done := make(chan struct{})
  10. sub := mux.Subscribe(someEvent{}, otherEvent{})
  11. go func() {
  12. for event := range sub.Chan() {
  13. fmt.Printf("Received: %#v\n", event)
  14. }
  15. fmt.Println("done")
  16. close(done)
  17. }()
  18. // Post some events.
  19. mux.Post(someEvent{5})
  20. mux.Post(yetAnotherEvent{X: 3, Y: 4})
  21. mux.Post(someEvent{6})
  22. mux.Post(otherEvent{"whoa"})
  23. // Stop closes all subscription channels.
  24. // The subscriber goroutine will print "done"
  25. // and exit.
  26. mux.Stop()
  27. // Wait for subscriber to return.
  28. <-done
  29. // Output:
  30. // Received: event.someEvent{I:5}
  31. // Received: event.someEvent{I:6}
  32. // Received: event.otherEvent{S:"whoa"}
  33. // done
  34. }