-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifiers): Fixes #44 only for async_std
- Loading branch information
Showing
9 changed files
with
411 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,39 @@ | ||
/// Notifies any task to wake up | ||
pub struct Notify { | ||
#[cfg(feature = "async_std")] | ||
use async_std::channel::{Receiver, Sender}; | ||
|
||
} | ||
#[cfg(feature = "async_tokio")] | ||
use tokio::sync::mpsc::{Receiver, Sender}; | ||
|
||
use crate::connection::queue::recv; | ||
|
||
/// Notify is a struct that wraps a buffer channel | ||
/// these channels are used to send messages to the main thread. | ||
#[derive(Clone)] | ||
pub struct Notify(pub Option<Sender<()>>, pub Receiver<()>); | ||
|
||
impl Notify { | ||
/// Creates a new Notify struct. | ||
pub fn new() -> Self { | ||
let (send, recv) = async_std::channel::bounded(1); | ||
Self(Some(send), recv) | ||
} | ||
|
||
/// Sends a message to all listeners. | ||
pub async fn notify(&self) -> bool { | ||
if let Some(sender) = &self.0 { | ||
sender.close(); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Waits for a message from the main thread. | ||
pub async fn wait(&self) -> bool { | ||
if let Err(_) = self.1.recv().await { | ||
false | ||
} else { | ||
true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters