-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
45 lines (34 loc) · 958 Bytes
/
example_test.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
package goeq_test
import (
"fmt"
"github.com/ftl/goeq"
)
type FrequencyListener interface {
FrequencyChanged(frequency float64) // FrequencyListeners will be notified when the frequency changed.
}
type Transceiver struct {
events *goeq.Queue
}
func (t *Transceiver) emitFrequencyChanged(frequency float64) {
t.events.Publish(goeq.Event(func(listener FrequencyListener) {
listener.FrequencyChanged(frequency)
}))
}
type FrequencyLabel struct{}
func (l *FrequencyLabel) SetText(text string) {
fmt.Println("Frequency:", text)
}
func (l *FrequencyLabel) FrequencyChanged(frequency float64) {
l.SetText(fmt.Sprintf("%0.2f", frequency))
}
func Example() {
events := goeq.New()
trx := &Transceiver{ // trx will produce FrequencyChanged events
events: events,
}
label := &FrequencyLabel{} // label will consume FrequencyChanged events
events.Subscribe(label)
trx.emitFrequencyChanged(14030000.0)
// Output:
// Frequency: 14030000.00
}