-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
38 lines (31 loc) · 797 Bytes
/
example_test.go
File metadata and controls
38 lines (31 loc) · 797 Bytes
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
package event
import (
"fmt"
)
func Example() {
// Custom event type
type MyEvent struct {
Message string
}
// Create a new event manager.
mgr := New()
// Subscribe to the event.
Subscribe(mgr, 0, func(e *MyEvent) {
// Handle the event.
fmt.Println("handler A received event:", e.Message)
})
// Subscribe to the event higher priority.
Subscribe(mgr, 1, func(e *MyEvent) {
// Handle the event.
fmt.Println("handler B received event:", e.Message)
e.Message = "hello gophers"
})
unsubscribe := Subscribe(mgr, 0, func(e *MyEvent) {
// Handle the event.
fmt.Println("handler C received event:", e.Message)
})
// Unsubscribe from the event if you want to or ignore and never call it.
unsubscribe()
// Fire the event.
mgr.Fire(&MyEvent{Message: "hello world"})
}