My personal site cherry.computer
htmx tailwind axum askama

refactor: remove mut requirement from get_scrobble function

RwLock uses interior mutability magic to ensure that you don't need a
&mut reference for write() to work.

cherry.computer 5802e018 30a3bedf

verified
+3 -3
+1 -1
server/src/index.rs
··· 8 8 scrobble: Option<ScrobblesTemplate>, 9 9 } 10 10 11 - pub async fn get_index(mut monitor: ScrobbleMonitor) -> RootTemplate { 11 + pub async fn get_index(monitor: ScrobbleMonitor) -> RootTemplate { 12 12 let scrobbles_template = monitor.try_get_scrobble(); 13 13 if scrobbles_template.is_none() { 14 14 // start fetching scrobble so we can send a fresh response to the client ASAP
+1 -1
server/src/main.rs
··· 69 69 } 70 70 71 71 async fn get_scrobble( 72 - State(mut monitor): State<ScrobbleMonitor>, 72 + State(monitor): State<ScrobbleMonitor>, 73 73 Query(ScrobbleQuery { immediate }): Query<ScrobbleQuery>, 74 74 ) -> Sse<impl Stream<Item = Result<sse::Event, Infallible>>> { 75 75 let stream = stream! {
+1 -1
server/src/scrobble_monitor.rs
··· 44 44 .map(|scrobble| scrobble.data.clone()) 45 45 } 46 46 47 - pub async fn get_scrobble(&mut self) -> anyhow::Result<ScrobblesTemplate> { 47 + pub async fn get_scrobble(&self) -> anyhow::Result<ScrobblesTemplate> { 48 48 if let Some(scrobble) = &*self.last_scrobble.read().await { 49 49 if scrobble.is_fresh() { 50 50 tracing::debug!("returning recently fetched scrobble data");