WIP Refine

This commit is contained in:
gizak 2015-08-30 23:03:47 -04:00
parent 3036ef125c
commit 3ea00a7476

103
events.go
View File

@ -10,46 +10,48 @@ package termui
import ( import (
"strings" "strings"
"sync"
"time"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
) )
//import "github.com/nsf/termbox-go"
var evtChs = make([]chan Event, 0)
// EventCh returns an output-only event channel.
// This function can be called many times (multiplexer).
func EventCh() <-chan Event {
out := make(chan Event)
evtChs = append(evtChs, out)
return out
}
// turn on event listener
func evtListen() {
go func() {
for {
e := termbox.PollEvent()
// dispatch
for _, c := range evtChs {
go func(ch chan Event) {
ch <- uiEvt(e)
}(c)
}
}
}()
}
type Event struct { type Event struct {
Type string Type string
Uri string Path string
From string From string
To string To string
Data interface{} Data interface{}
Time int Time int64
} }
var sysevt struct {
chs []chan Event
}
func newSysEvtFromTb(e termbox.Event) Event {
ne := Event{From: "/sys", Time: time.Now().Unix()}
return ne
}
func hookSysEvt() {
sysevt.chs = make([]chan Event, 0)
for {
e := termbox.PollEvent()
for _, c := range sysevt.chs {
// shorten?
go func(ch chan Event, ev Event) { ch <- ev }(c, newSysEvtFromTb(e))
}
}
}
func NewSysEvtCh() chan Event {
ec := make(chan Event)
sysevt.chs = append(sysevt.chs, ec)
return ec
}
/*
type evtCtl struct { type evtCtl struct {
in chan Event in chan Event
out chan Event out chan Event
@ -58,14 +60,6 @@ type evtCtl struct {
close chan int close chan int
} }
//
type EvtStream struct {
srcMap map[string]Event
stream chan Event
cache map[string][]func(Event)
Handlers map[string]func(Event)
}
func newEvtCtl() evtCtl { func newEvtCtl() evtCtl {
ec := evtCtl{} ec := evtCtl{}
ec.in = make(chan Event) ec.in = make(chan Event)
@ -76,13 +70,31 @@ func newEvtCtl() evtCtl {
return ec return ec
} }
func NewEvtStream() EvtStream { */
return EvtStream{ //
srcMap: make(map[string]Event), type EvtStream struct {
srcMap map[string]chan Event
stream chan Event
cache map[string][]func(Event)
wg sync.WaitGroup
Handlers map[string]func(Event)
}
func NewEvtStream() *EvtStream {
return &EvtStream{
srcMap: make(map[string]chan Event),
stream: make(chan Event), stream: make(chan Event),
} }
} }
func (es *EvtStream) Init() {
go func() {
es.wg.Wait()
close(es.stream)
}()
}
// a: /sys/bell // a: /sys/bell
// b: /sys // b: /sys
// score: 1 // score: 1
@ -113,6 +125,17 @@ func MatchScore(a, b string) int {
return score return score
} }
func (es *EvtStream) Merge(ec chan Event) {
es.wg.Add(1)
go func(a chan Event) {
for n := range ec {
es.stream <- n
}
wg.Done()
}(ec)
}
/* /*
func (es *EvtStream) hookup() { func (es *EvtStream) hookup() {