From 7e57b42c911e9a774cbf010766b4f23252d4a317 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 29 Oct 2016 22:48:27 +0200 Subject: [PATCH] Add timeout when changing channels Fixes #25 --- handlers/event.go | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/handlers/event.go b/handlers/event.go index df4d096..e3350ca 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -2,6 +2,7 @@ package handlers import ( "strconv" + "time" "github.com/gizak/termui" "github.com/nlopes/slack" @@ -11,6 +12,8 @@ import ( "github.com/erroneousboat/slack-term/views" ) +var timer *time.Timer + // actionMap binds specific action names to the function counterparts, // these action names can then be used to bind them to specific keys // in the Config. @@ -193,19 +196,36 @@ func actionGetMessages(ctx *context.AppContext) { termui.Render(ctx.View.Chat) } -func actionGetChannels(ctx *context.AppContext) { - ctx.View.Channels.GetChannels(ctx.Service) - termui.Render(ctx.View.Channels) -} - func actionMoveCursorUpChannels(ctx *context.AppContext) { - ctx.View.Channels.MoveCursorUp() - actionChangeChannel(ctx) + go func() { + if timer != nil { + timer.Stop() + } + + ctx.View.Channels.MoveCursorUp() + termui.Render(ctx.View.Channels) + + timer = time.NewTimer(time.Second / 4) + <-timer.C + + actionChangeChannel(ctx) + }() } func actionMoveCursorDownChannels(ctx *context.AppContext) { - ctx.View.Channels.MoveCursorDown() - actionChangeChannel(ctx) + go func() { + if timer != nil { + timer.Stop() + } + + ctx.View.Channels.MoveCursorDown() + termui.Render(ctx.View.Channels) + + timer = time.NewTimer(time.Second / 4) + <-timer.C + + actionChangeChannel(ctx) + }() } func actionMoveCursorTopChannels(ctx *context.AppContext) {