-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
51 lines (39 loc) · 1.04 KB
/
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
46
47
48
49
50
51
package gsignal_test
import (
"fmt"
"github.com/quasilyte/gsignal"
)
type button struct {
Name string
EventClicked gsignal.Event[*button]
}
func (b *button) Click() { b.EventClicked.Emit(b) }
type listener struct {
disposed bool
}
func (l *listener) IsDisposed() bool { return l.disposed }
func (l *listener) onClick(b *button) {
fmt.Println("listener on click")
}
func Example() {
b := &button{Name: "example"}
b.Click() // nothing happens, 0 connections
i := 1
b.EventClicked.Connect(nil, func(b *button) {
fmt.Printf("%s clicked (%d)\n", b.Name, i)
i++
})
b.Click() // prints "example clicked (1)" once
b.Click() // prints "example clicked (2)" once; again
l := &listener{}
b.EventClicked.Connect(l, l.onClick)
b.Click() // prints "example clicked (3)", then "listener on click"
l.disposed = true // this will cause a disconnect
b.Click() // prints "example clicked (4)" once
// Output:
// example clicked (1)
// example clicked (2)
// example clicked (3)
// listener on click
// example clicked (4)
}