Trait Subscription

Source
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§

Provided Methods§

Source

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 a RwLockReadGuard<'_, 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);
    }    
});
Source

fn unsubscribe(&self, token: ListenerToken)

Unsubscribes a previously registered listener using the provided ListenerToken.

§Arguments
  • token: The ListenerToken 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.

Implementors§