-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
50 lines (42 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"context"
"log"
"sync/atomic"
"time"
"golang.org/x/sync/errgroup"
"github.com/leonsteinhaeuser/observer/v2"
)
// Event with some payload
type Event struct {
ID int
Payload int32
}
// Handler models some long-running component that needs to react to external events
type Handler struct {
N int32
}
// onEvent processes Event
func (h *Handler) OnEvent(ctx context.Context, message Event) error {
log.Printf("received message(%d) with payload %d\n", message.ID, message.Payload)
// do something with event's payload
atomic.AddInt32(&h.N, message.Payload)
return nil
}
var obsrv = new(observer.Observer[Event])
func main() {
h := &Handler{}
g, ctx := errgroup.WithContext(context.TODO())
g.Go(observer.Handle[Event](ctx, obsrv, h.OnEvent))
for id, payload := range []int32{1, 2, 3, 5, -1, 10} {
obsrv.NotifyAll(Event{id, payload})
}
time.Sleep(1 * time.Second) // Allow events to settle
if err := obsrv.Close(); err != nil {
log.Printf("closing observer: %+v", err)
}
log.Printf("final result: %d", h.N)
if err := g.Wait(); err != nil {
log.Fatal(err)
}
}