File
Blob: firmware/crates/esp32-radio/src/station.rs
| 1 | //! Coalesced metadata between the radio owner and blocking HTTP signaling task. |
| 2 | use crate::error::{Error, Result}; |
| 3 | use radio_core::now_playing::NowPlaying; |
| 4 | use std::sync::{Arc, Mutex}; |
| 5 | |
| 6 | #[derive(Clone, Default)] |
| 7 | pub(crate) struct Station(Arc<Mutex<Option<Arc<NowPlaying>>>>); |
| 8 | |
| 9 | impl Station { |
| 10 | pub(crate) fn publish(&self, value: &NowPlaying) -> Result<()> { |
| 11 | let snapshot = Arc::new(value.clone()); |
| 12 | *self |
| 13 | .0 |
| 14 | .lock() |
| 15 | .map_err(|_| Error::new("station metadata unavailable"))? = Some(snapshot); |
| 16 | Ok(()) |
| 17 | } |
| 18 | pub(crate) fn snapshot(&self) -> Result<Option<Arc<NowPlaying>>> { |
| 19 | Ok(self |
| 20 | .0 |
| 21 | .lock() |
| 22 | .map_err(|_| Error::new("station metadata unavailable"))? |
| 23 | .clone()) |
| 24 | } |
| 25 | } |