WIP Refine
This commit is contained in:
parent
3036ef125c
commit
3ea00a7476
103
events.go
103
events.go
@ -10,46 +10,48 @@ package termui
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"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 string
|
||||
Uri string
|
||||
Path string
|
||||
From string
|
||||
To string
|
||||
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 {
|
||||
in chan Event
|
||||
out chan Event
|
||||
@ -58,14 +60,6 @@ type evtCtl struct {
|
||||
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 {
|
||||
ec := evtCtl{}
|
||||
ec.in = make(chan Event)
|
||||
@ -76,13 +70,31 @@ func newEvtCtl() evtCtl {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
func (es *EvtStream) Init() {
|
||||
|
||||
go func() {
|
||||
es.wg.Wait()
|
||||
close(es.stream)
|
||||
}()
|
||||
}
|
||||
|
||||
// a: /sys/bell
|
||||
// b: /sys
|
||||
// score: 1
|
||||
@ -113,6 +125,17 @@ func MatchScore(a, b string) int {
|
||||
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() {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user