diff options
| author | faiface <faiface2202@gmail.com> | 2019-05-02 01:01:02 +0200 |
|---|---|---|
| committer | faiface <faiface2202@gmail.com> | 2019-05-02 01:01:02 +0200 |
| commit | 7bbe7dc9618949f2fe023efcb896a70150966e44 (patch) | |
| tree | 26c04eafd111309558272bc453106c63307b61ee /event.go | |
| parent | d07cc4a0af7f602fe3d519faca01b3abfce2e9b2 (diff) | |
| download | gui-7bbe7dc9618949f2fe023efcb896a70150966e44.zip | |
revamp the whole thing for a fresh new start
Diffstat (limited to 'event.go')
| -rw-r--r-- | event.go | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/event.go b/event.go new file mode 100644 index 0000000..3440926 --- /dev/null +++ b/event.go @@ -0,0 +1,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 +} |