Skip to content

Commit

Permalink
remove sync::subscribe
Browse files Browse the repository at this point in the history
This is just wrapping async code and I don't think there's much
of a use case for it. Anyone who wants this can start their own
async executor and use the async fn subscribe.
  • Loading branch information
Be-ing authored and Eduardo Flores committed Jan 7, 2025
1 parent 86b5b39 commit 912fe8a
Show file tree
Hide file tree
Showing 6 changed files with 0 additions and 130 deletions.
8 changes: 0 additions & 8 deletions examples/sync.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
detect();
subscribe();
Ok(())
}

fn detect() {
println!("Current mode: {:?}", dark_light::sync::detect());
}

fn subscribe() {
let stream = dark_light::sync::subscribe();
while let Ok(mode) = stream.recv() {
println!("System theme changed: {:?}", mode);
}
}
20 changes: 0 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,4 @@ pub mod sync {
/// }
/// ```
pub use super::platforms::platform::detect::sync::detect;

/// Notifies the user if the system theme has been changed.
///
/// This function returns a stream of `Mode` values. The stream will emit a new value whenever the system theme changes.
///
/// # Example
///
/// ``` no_run
/// use dark_light::Mode;
///
/// let stream = dark_light::sync::subscribe();
/// while let Ok(mode) = stream.recv() {
/// match mode {
/// Mode::Dark => {},
/// Mode::Light => {},
/// Mode::Default => {},
/// }
/// }
/// ```
pub use super::platforms::platform::subscribe::sync::subscribe;
}
31 changes: 0 additions & 31 deletions src/platforms/freedesktop/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,6 @@ use futures_lite::{Stream, StreamExt};

use crate::Mode;

#[cfg(any(feature = "sync", doc))]
pub(crate) mod sync {
use super::super::Mode;
use super::color_scheme_stream;
use futures_lite::StreamExt;

pub fn subscribe() -> std::sync::mpsc::Receiver<Mode> {
let (tx, rx) = std::sync::mpsc::channel();

std::thread::spawn(move || {
futures_lite::future::block_on(async {
let stream = match color_scheme_stream().await {
Ok(stream) => stream.boxed(),
Err(err) => {
log::error!("Failed to subscribe to color scheme changes: {}", err);
futures_lite::stream::empty().boxed()
}
};

stream
.for_each(|mode| {
let _ = tx.send(mode);
})
.await;
});
});

rx
}
}

pub async fn subscribe() -> impl Stream<Item = Mode> + Send {
match color_scheme_stream().await {
Ok(stream) => stream.boxed(),
Expand Down
23 changes: 0 additions & 23 deletions src/platforms/macos/subscribe.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
use crate::Mode;

#[cfg(any(feature = "sync", doc))]
pub(crate) mod sync {
pub fn subscribe() -> std::sync::mpsc::Receiver<crate::Mode> {
let (tx, rx) = std::sync::mpsc::channel();
let mut last_mode = crate::sync::detect();

tx.send(last_mode).unwrap();

std::thread::spawn(move || loop {
let current_mode = crate::sync::detect();

if current_mode != last_mode {
if tx.send(current_mode).is_err() {
break;
}
last_mode = current_mode;
}
});

rx
}
}

pub async fn subscribe() -> impl futures_lite::Stream<Item = Mode> {
Box::pin(futures_lite::stream::unfold(
crate::detect().await,
Expand Down
23 changes: 0 additions & 23 deletions src/platforms/websys/subscribe.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
#[cfg(any(feature = "sync", doc))]
pub(crate) mod sync {
pub fn subscribe() -> std::sync::mpsc::Receiver<crate::Mode> {
let (tx, rx) = std::sync::mpsc::channel();
let mut last_mode = crate::sync::detect();

tx.send(last_mode).unwrap();

std::thread::spawn(move || loop {
let current_mode = crate::sync::detect();

if current_mode != last_mode {
if tx.send(current_mode).is_err() {
break;
}
last_mode = current_mode;
}
});

rx
}
}

pub async fn subscribe() -> impl futures_lite::Stream<Item = crate::Mode> {
Box::pin(futures_lite::stream::unfold(
crate::detect().await,
Expand Down
25 changes: 0 additions & 25 deletions src/platforms/windows/subscribe.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
use crate::Mode;

#[cfg(any(feature = "sync", doc))]
pub(crate) mod sync {
use crate::Mode;

pub fn subscribe() -> std::sync::mpsc::Receiver<Mode> {
let (tx, rx) = std::sync::mpsc::channel();
let mut last_mode = crate::sync::detect();

tx.send(last_mode).unwrap();

std::thread::spawn(move || loop {
let current_mode = crate::sync::detect();

if current_mode != last_mode {
if tx.send(current_mode).is_err() {
break;
}
last_mode = current_mode;
}
});

rx
}
}

pub async fn subscribe() -> impl futures_lite::Stream<Item = Mode> {
Box::pin(futures_lite::stream::unfold(
crate::detect().await,
Expand Down

0 comments on commit 912fe8a

Please sign in to comment.