WIP Wrap up Event
This commit is contained in:
		
							parent
							
								
									5816873b74
								
							
						
					
					
						commit
						283c3a36f2
					
				
							
								
								
									
										83
									
								
								events.go
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								events.go
									
									
									
									
									
								
							@ -8,10 +8,10 @@
 | 
			
		||||
 | 
			
		||||
package termui
 | 
			
		||||
 | 
			
		||||
import "github.com/nsf/termbox-go"
 | 
			
		||||
//import "github.com/nsf/termbox-go"
 | 
			
		||||
 | 
			
		||||
/***********************************termbox-go**************************************/
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
type (
 | 
			
		||||
	EventType uint8
 | 
			
		||||
	Modifier  uint8
 | 
			
		||||
@ -127,9 +127,9 @@ const (
 | 
			
		||||
	EventRaw
 | 
			
		||||
	EventNone
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
/**************************************end**************************************/
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
// convert termbox.Event to termui.Event
 | 
			
		||||
func uiEvt(e termbox.Event) Event {
 | 
			
		||||
	event := Event{}
 | 
			
		||||
@ -171,49 +171,52 @@ func evtListen() {
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
// EventHandlers is a handler sequence
 | 
			
		||||
var EventHandlers []func(Event)
 | 
			
		||||
 | 
			
		||||
var signalQuit = make(chan bool)
 | 
			
		||||
 | 
			
		||||
// Quit sends quit signal to terminate termui
 | 
			
		||||
func Quit() {
 | 
			
		||||
	signalQuit <- true
 | 
			
		||||
*/
 | 
			
		||||
type Event struct {
 | 
			
		||||
	Type  string
 | 
			
		||||
	Uri   string
 | 
			
		||||
	Data  interface{}
 | 
			
		||||
	Time  int
 | 
			
		||||
	Refer string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Wait listening to signalQuit, block operation.
 | 
			
		||||
func Wait() {
 | 
			
		||||
	<-signalQuit
 | 
			
		||||
type evtCtl struct {
 | 
			
		||||
	in      chan Event
 | 
			
		||||
	out     chan Event
 | 
			
		||||
	suspend chan int
 | 
			
		||||
	recover chan int
 | 
			
		||||
	close   chan int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RegEvtHandler register function into TSEventHandler sequence.
 | 
			
		||||
func RegEvtHandler(fn func(Event)) {
 | 
			
		||||
	EventHandlers = append(EventHandlers, fn)
 | 
			
		||||
//
 | 
			
		||||
type EvtStream struct {
 | 
			
		||||
	srcMap map[string]evtCtl
 | 
			
		||||
	stream chan Event
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EventLoop handles all events and
 | 
			
		||||
// redirects every event to callbacks in EventHandlers
 | 
			
		||||
func EventLoop() {
 | 
			
		||||
	evt := make(chan termbox.Event)
 | 
			
		||||
func newEvtCtl() evtCtl {
 | 
			
		||||
	ec := evtCtl{}
 | 
			
		||||
	ec.in = make(chan Event)
 | 
			
		||||
	ec.suspend = make(chan int)
 | 
			
		||||
	ec.recover = make(chan int)
 | 
			
		||||
	ec.close = make(chan int)
 | 
			
		||||
	ec.out = make(chan Event)
 | 
			
		||||
	return ec
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
	go func() {
 | 
			
		||||
		for {
 | 
			
		||||
			evt <- termbox.PollEvent()
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
		select {
 | 
			
		||||
		case c := <-signalQuit:
 | 
			
		||||
			defer func() { signalQuit <- c }()
 | 
			
		||||
			return
 | 
			
		||||
		case e := <-evt:
 | 
			
		||||
			for _, fn := range EventHandlers {
 | 
			
		||||
				fn(uiEvt(e))
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
func NewEvtStream() EvtStream {
 | 
			
		||||
	return EvtStream{
 | 
			
		||||
		srcMap: make(map[string]evtCtl),
 | 
			
		||||
		stream: make(chan Event),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
func (es *EvtStream) hookup() {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (es EvtStream) Subscribe(uri string) chan Event {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user