dkforest

A forum and chat platform (onion)
git clone https://git.dasho.dev/n0tr1v/dkforest.git
Log | Files | Refs | LICENSE

commit 3210ffc4ec6d5228b760e8a34a3d951617e8c891
parent 07f0fe48296381b8c0b1510ec6070ecb99bfcd0c
Author: n0tr1v <n0tr1v@protonmail.com>
Date:   Sun, 24 Dec 2023 00:45:09 -0500

cleanup

Diffstat:
Mpkg/web/handlers/poker.go | 74++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 32 insertions(+), 42 deletions(-)

diff --git a/pkg/web/handlers/poker.go b/pkg/web/handlers/poker.go @@ -362,26 +362,12 @@ func PokerStreamHandler(c echo.Context) error { send(poker.BuildBaseHtml(g, authUser, chatRoomSlug)) c.Response().Flush() -Loop: - for { - select { - case <-quit: - break Loop - default: - } - - _, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) - if err != nil { - if errors.Is(err, pubsub.ErrCancelled) { - break Loop - } - continue - } - + loop(quit, sub, func(topic string, payload any) error { send(poker.BuildPayloadHtml(g, authUser, payload)) c.Response().Flush() - continue - } + return nil + }) + return nil } @@ -412,6 +398,22 @@ func PokerLogsHandler(c echo.Context) error { } c.Response().Flush() + loop(quit, sub, func(topic string, payload any) error { + switch evt := payload.(type) { + case poker.LogEvent: + send(fmt.Sprintf(`<div>%s</div>`, evt.Message)) + c.Response().Flush() + } + return nil + }) + + return nil +} + +var BreakLoopErr = errors.New("break Loop") +var ContinueLoopErr = errors.New("continue Loop") + +func loop[T any](quit <-chan struct{}, sub *pubsub.Sub[T], clb func(topic string, payload T) error) { Loop: for { select { @@ -420,7 +422,7 @@ Loop: default: } - _, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) + topic, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) if err != nil { if errors.Is(err, pubsub.ErrCancelled) { break Loop @@ -428,13 +430,14 @@ Loop: continue } - switch evt := payload.(type) { - case poker.LogEvent: - send(fmt.Sprintf(`<div>%s</div>`, evt.Message)) - c.Response().Flush() + if err := clb(topic, payload); err != nil { + if errors.Is(err, BreakLoopErr) { + break Loop + } else if errors.Is(err, ContinueLoopErr) { + continue Loop + } } } - return nil } func PokerBetHandler(c echo.Context) error { @@ -547,29 +550,16 @@ func PokerBetHandler(c echo.Context) error { c.Response().Flush() } -Loop: - for { - select { - case <-quit: - break Loop - default: - } - - _, payload, err := sub.ReceiveTimeout2(1*time.Second, quit) - if err != nil { - if errors.Is(err, pubsub.ErrCancelled) { - break Loop - } - continue - } - + loop(quit, sub, func(topic string, payload any) error { switch payload.(type) { case poker.RefreshButtonsEvent: send(fmt.Sprintf(`<meta http-equiv="refresh" content="0" />`)) c.Response().Flush() - return nil + return BreakLoopErr } - } + return nil + }) + return nil }