pub trait Subscription {
// Required methods
fn get_info(&self) -> RwLockReadGuard<'_, Option<NowPlayingInfo>>;
fn get_token_counter(&self) -> Arc<AtomicU64>;
fn get_listeners(
&self,
) -> Arc<Mutex<HashMap<ListenerToken, Box<dyn Fn(RwLockReadGuard<'_, Option<NowPlayingInfo>>) + Send + Sync>>>>;
// Provided methods
fn subscribe<F: Fn(RwLockReadGuard<'_, Option<NowPlayingInfo>>) + Send + Sync + 'static>(
&self,
listener: F,
) -> ListenerToken { ... }
fn unsubscribe(&self, token: ListenerToken) { ... }
}
Required Methods§
fn get_info(&self) -> RwLockReadGuard<'_, Option<NowPlayingInfo>>
fn get_token_counter(&self) -> Arc<AtomicU64>
fn get_listeners( &self, ) -> Arc<Mutex<HashMap<ListenerToken, Box<dyn Fn(RwLockReadGuard<'_, Option<NowPlayingInfo>>) + Send + Sync>>>>
Provided Methods§
Sourcefn subscribe<F: Fn(RwLockReadGuard<'_, Option<NowPlayingInfo>>) + Send + Sync + 'static>(
&self,
listener: F,
) -> ListenerToken
fn subscribe<F: Fn(RwLockReadGuard<'_, Option<NowPlayingInfo>>) + Send + Sync + 'static>( &self, listener: F, ) -> ListenerToken
Subscribes a listener to receive updates when the “Now Playing” information changes.
§Arguments
listener
: A function or closure that accepts aRwLockReadGuard<'_, Option<NowPlayingInfo>>
. The function will be invoked with the current “Now Playing” info whenever the data is updated.
§Returns
ListenerToken
: A token representing the listener, which can later be used to unsubscribe.
§Example
use media_remote::prelude::*;
let now_playing: NowPlaying = NowPlaying::new();
now_playing.subscribe(|guard| {
let info = guard.as_ref();
if let Some(info) = info {
println!("Currently playing: {:?}", info.title);
}
});
Sourcefn unsubscribe(&self, token: ListenerToken)
fn unsubscribe(&self, token: ListenerToken)
Unsubscribes a previously registered listener using the provided ListenerToken
.
§Arguments
token
: TheListenerToken
returned when the listener was subscribed. It is used to identify and remove the listener.
§Example
use media_remote::prelude::*;
let now_playing: NowPlaying = NowPlaying::new();
let token = now_playing.subscribe(|guard| {
let info = guard.as_ref();
if let Some(info) = info {
println!("Currently playing: {:?}", info.title);
}
});
now_playing.unsubscribe(token);
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.