@@ -8,8 +8,6 @@ use std::io::Write;
88
99use anyhow:: Context as _;
1010use anyhow:: Result ;
11- use librespot_core:: authentication:: Credentials ;
12- use librespot_core:: session:: Session ;
1311use reqwest:: StatusCode ;
1412use rspotify:: {
1513 http:: Query ,
@@ -81,30 +79,46 @@ impl Client {
8179 }
8280
8381 /// Create a new client session
84- pub async fn new_session ( & self , state : & SharedState , reauth : bool ) -> Result < ( ) > {
82+ pub async fn new_session ( & self , state : Option < & SharedState > , reauth : bool ) -> Result < ( ) > {
8583 let session = self . auth_config . new_session ( ) ;
8684 let creds = auth:: get_creds ( & self . auth_config , reauth)
8785 . await
8886 . context ( "get credentials" ) ?;
8987 * self . session . lock ( ) . await = Some ( session. clone ( ) ) ;
9088
89+ #[ allow( unused_mut) ]
90+ let mut connected = false ;
91+
9192 #[ cfg( feature = "streaming" ) ]
92- if state. is_streaming_enabled ( ) {
93- self . new_streaming_connection ( state. clone ( ) , session, creds)
93+ if let Some ( state) = state {
94+ if state. is_streaming_enabled ( ) {
95+ self . new_streaming_connection ( state. clone ( ) , session. clone ( ) , creds. clone ( ) )
96+ . await
97+ . context ( "new streaming connection" ) ?;
98+ connected = true ;
99+ }
100+ }
101+
102+ if !connected {
103+ // if session is not connected (triggered by `new_streaming_connection`), connect to the session
104+ session
105+ . connect ( creds, true )
94106 . await
95- . context ( "new streaming connection " ) ?;
107+ . context ( "connect to a session " ) ?;
96108 }
97109
98110 tracing:: info!( "Used a new session for Spotify client." ) ;
99111
100112 self . refresh_token ( ) . await . context ( "refresh auth token" ) ?;
101113
102- // reset the application's caches
103- state. data . write ( ) . caches = MemoryCaches :: new ( ) ;
114+ if let Some ( state) = state {
115+ // reset the application's caches
116+ state. data . write ( ) . caches = MemoryCaches :: new ( ) ;
104117
105- self . initialize_playback ( state)
106- . await
107- . context ( "initialize playback" ) ?;
118+ self . initialize_playback ( state)
119+ . await
120+ . context ( "initialize playback" ) ?;
121+ }
108122
109123 Ok ( ( ) )
110124 }
@@ -113,7 +127,7 @@ impl Client {
113127 pub async fn check_valid_session ( & self , state : & SharedState ) -> Result < ( ) > {
114128 if self . session ( ) . await . is_invalid ( ) {
115129 tracing:: info!( "Client's current session is invalid, creating a new session..." ) ;
116- self . new_session ( state, false )
130+ self . new_session ( Some ( state) , false )
117131 . await
118132 . context ( "create new client session" ) ?;
119133 }
@@ -125,8 +139,8 @@ impl Client {
125139 pub async fn new_streaming_connection (
126140 & self ,
127141 state : SharedState ,
128- session : Session ,
129- creds : Credentials ,
142+ session : librespot_core :: Session ,
143+ creds : librespot_core :: authentication :: Credentials ,
130144 ) -> Result < ( ) > {
131145 let new_conn =
132146 crate :: streaming:: new_connection ( self . clone ( ) , state, session, creds) . await ?;
@@ -290,7 +304,7 @@ impl Client {
290304 }
291305 #[ cfg( feature = "streaming" ) ]
292306 ClientRequest :: RestartIntegratedClient => {
293- self . new_session ( state, false ) . await ?;
307+ self . new_session ( Some ( state) , false ) . await ?;
294308 }
295309 ClientRequest :: GetCurrentUser => {
296310 let user = self . current_user ( ) . await ?;
0 commit comments