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
|
package gui
import "fmt"
type Event string
func Eventf(format string, a ...interface{}) Event {
return Event(fmt.Sprintf(format, a...))
}
func (e Event) Matches(format string, a ...interface{}) bool {
_, err := fmt.Sscanf(string(e), format, a...)
return err == nil
}
func MakeEventsChan() (<-chan Event, chan<- Event) {
out, in := make(chan Event), make(chan Event)
go func() {
var queue []Event
for {
x, ok := <-in
if !ok {
close(out)
return
}
queue = append(queue, x)
for len(queue) > 0 {
select {
case out <- queue[0]:
queue = queue[1:]
case x, ok := <-in:
if !ok {
for _, x := range queue {
out <- x
}
close(out)
return
}
queue = append(queue, x)
}
}
}
}()
return out, in
}
|