github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/eventstream/eventstream_example_subscribe_test.go (about)

     1  package eventstream_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/asynkron/protoactor-go/eventstream"
     7  )
     8  
     9  // Subscribe subscribes to events
    10  func ExampleEventStream_Subscribe() {
    11  	es := eventstream.NewEventStream()
    12  	handler := func(event interface{}) {
    13  		fmt.Println(event)
    14  	}
    15  
    16  	// only allow strings
    17  	predicate := func(event interface{}) bool {
    18  		_, ok := event.(string)
    19  		return ok
    20  	}
    21  
    22  	sub := es.SubscribeWithPredicate(handler, predicate)
    23  
    24  	es.Publish("Hello World")
    25  	es.Publish(1)
    26  
    27  	es.Unsubscribe(sub)
    28  
    29  	// Output: Hello World
    30  }