From 36e263329c51216526724febb632cc0aef32f511 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 5 Aug 2017 11:46:15 +0200 Subject: [PATCH] Update termbox handlers termbox.PollEvent sometimes trigger a panic whil typing in the input box (#58). I took note from https://github.com/nsf/godit and implemented and extra event handler that should potentially handle more events from termbox. --- handlers/event.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/handlers/event.go b/handlers/event.go index c7a0fe7..ef38d1c 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -41,7 +41,7 @@ var actionMap = map[string]func(*context.AppContext){ func RegisterEventHandlers(ctx *context.AppContext) { eventHandler(ctx) - incomingMessageHandler(ctx) + messageHandler(ctx) } func eventHandler(ctx *context.AppContext) { @@ -54,18 +54,38 @@ func eventHandler(ctx *context.AppContext) { go func() { for { ev := <-ctx.EventQueue - - switch ev.Type { - case termbox.EventKey: - actionKeyEvent(ctx, ev) - case termbox.EventResize: - actionResizeEvent(ctx, ev) - } + handleTermboxEvents(ctx, ev) + handleMoreTermboxEvents(ctx, ev) } }() } -func incomingMessageHandler(ctx *context.AppContext) { +func handleTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool { + switch ev.Type { + case termbox.EventKey: + actionKeyEvent(ctx, ev) + case termbox.EventResize: + actionResizeEvent(ctx, ev) + } + + return true +} + +func handleMoreTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool { + for { + select { + case ev := <- ctx.EventQueue: + ok := handleTermboxEvents(ctx, ev) + if !ok { + return false + } + default: + return true + } + } +} + +func messageHandler(ctx *context.AppContext) { go func() { for { select {