From 09625ecca542531bdd16188a8833ab82dde79821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Fri, 26 Feb 2021 15:04:50 +0100 Subject: [PATCH] session refactor state. --- internal/session/session.go | 23 +++++++---------------- internal/types/session.go | 1 - 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/internal/session/session.go b/internal/session/session.go index 11c3c967..14e18175 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -13,10 +13,9 @@ type SessionCtx struct { logger zerolog.Logger manager *SessionManagerCtx profile types.MemberProfile + state types.MemberState websocketPeer types.WebSocketPeer - websocketConnected bool webrtcPeer types.WebRTCPeer - webrtcConnected bool } func (session *SessionCtx) ID() string { @@ -76,13 +75,13 @@ func (session *SessionCtx) profileChanged() { }, nil) } - if !session.CanWatch() && session.IsWatching() { + if !session.CanWatch() && session.state.IsWatching { if err := session.webrtcPeer.Destroy(); err != nil { session.logger.Warn().Err(err).Msgf("webrtc destroy has failed") } } - if (!session.CanConnect() || !session.CanLogin()) && session.IsConnected() { + if (!session.CanConnect() || !session.CanLogin()) && session.state.IsConnected { if err := session.Disconnect("profile changed"); err != nil { session.logger.Warn().Err(err).Msgf("websocket destroy has failed") } @@ -98,19 +97,11 @@ func (session *SessionCtx) IsHost() bool { } func (session *SessionCtx) IsConnected() bool { - return session.websocketConnected -} - -func (session *SessionCtx) IsWatching() bool { - return session.webrtcConnected + return session.state.IsConnected } func (session *SessionCtx) GetState() types.MemberState { - // TODO: Save state in member struct. - return types.MemberState{ - IsConnected: session.IsConnected(), - IsWatching: session.IsWatching(), - } + return session.state } // --- @@ -128,7 +119,7 @@ func (session *SessionCtx) SetWebSocketPeer(websocketPeer types.WebSocketPeer) { } func (session *SessionCtx) SetWebSocketConnected(connected bool) { - session.websocketConnected = connected + session.state.IsConnected = connected if connected { session.manager.emmiter.Emit("connected", session) @@ -196,7 +187,7 @@ func (session *SessionCtx) SetWebRTCConnected(webrtcPeer types.WebRTCPeer, conne return } - session.webrtcConnected = connected + session.state.IsWatching = connected session.manager.emmiter.Emit("state_changed", session) if !connected { diff --git a/internal/types/session.go b/internal/types/session.go index c2a3a772..c909de8b 100644 --- a/internal/types/session.go +++ b/internal/types/session.go @@ -45,7 +45,6 @@ type Session interface { // state IsHost() bool IsConnected() bool - IsWatching() bool GetState() MemberState // websocket