Skip to main content

stream

Function stream 

Source
pub fn stream() -> Result<impl Stream<Item = Mode>, Error>
Expand description

Subscribes to changes in the system theme mode using an async stream.

Returns a Stream that yields a new Mode each time the OS theme changes.

§Example

use dark_light::{ Error, Mode };
use futures_util::StreamExt;

fn main() -> Result<(), Error> {
    futures_executor::block_on(async {
        let mut stream = dark_light::stream()?;
        while let Some(mode) = stream.next().await {
            match mode {
                Mode::Dark => {},
                Mode::Light => {},
                Mode::Unspecified => {},
            }
        }
        Ok(())
    })
}

Subscribes to theme changes as an async Stream.

This is an adapter over crate::subscribe. On most platforms it drives the underlying blocking crate::Watcher on a background thread and forwards each Mode into the returned stream. On wasm32 (which has no threads here), the browser’s MediaQueryList change event is forwarded directly instead.

§Example

use dark_light::{ Error, Mode };
use futures_util::StreamExt;

fn main() -> Result<(), Error> {
    futures_executor::block_on(async {
        let mut stream = dark_light::stream()?;
        while let Some(mode) = stream.next().await {
            match mode {
                Mode::Dark => {},
                Mode::Light => {},
                Mode::Unspecified => {},
            }
        }
        Ok(())
    })
}