github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/event/example_test.go (about)

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