From 6b77f54b8c870471f8bf322cf1a99492bb81e377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 3 Nov 2021 23:52:44 +0100 Subject: [PATCH] inactiveCursors to own func. --- internal/websocket/manager.go | 86 ++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 115a9aae..6da1896c 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -136,47 +136,7 @@ func (manager *WebSocketManagerCtx) Start() { manager.fileChooserDialogEvents() if manager.sessions.InactiveCursors() { - manager.logger.Info().Msg("starting inactive cursors handler") - - manager.wg.Add(1) - go func() { - defer manager.wg.Done() - - ticker := time.NewTicker(inactiveCursorsPeriod) - defer ticker.Stop() - - lastEmpty := false - - for { - select { - case <-manager.shutdown: - manager.logger.Info().Msg("stopping inactive cursors handler") - return - case <-ticker.C: - cursorsMap := manager.sessions.PopCursors() - - length := len(cursorsMap) - if length == 0 && lastEmpty { - continue - } - lastEmpty = length == 0 - - cursors := []message.SessionCursor{} - for session, cursor := range cursorsMap { - cursors = append( - cursors, - message.SessionCursor{ - ID: session.ID(), - X: uint16(cursor.X), - Y: uint16(cursor.Y), - }, - ) - } - - manager.sessions.InactiveCursorsBroadcast(event.SESSION_CURSORS, cursors, nil) - } - } - }() + manager.inactiveCursors() } manager.logger.Info().Msg("websocket starting") @@ -338,3 +298,47 @@ func (manager *WebSocketManagerCtx) handle(connection *websocket.Conn, peer type } } } + +func (manager *WebSocketManagerCtx) inactiveCursors() { + manager.logger.Info().Msg("starting inactive cursors handler") + + manager.wg.Add(1) + go func() { + defer manager.wg.Done() + + ticker := time.NewTicker(inactiveCursorsPeriod) + defer ticker.Stop() + + lastEmpty := false + + for { + select { + case <-manager.shutdown: + manager.logger.Info().Msg("stopping inactive cursors handler") + return + case <-ticker.C: + cursorsMap := manager.sessions.PopCursors() + + length := len(cursorsMap) + if length == 0 && lastEmpty { + continue + } + lastEmpty = length == 0 + + cursors := []message.SessionCursor{} + for session, cursor := range cursorsMap { + cursors = append( + cursors, + message.SessionCursor{ + ID: session.ID(), + X: uint16(cursor.X), + Y: uint16(cursor.Y), + }, + ) + } + + manager.sessions.InactiveCursorsBroadcast(event.SESSION_CURSORS, cursors, nil) + } + } + }() +}