Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give access to Subscription's crossbeam channel #162

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,34 @@ impl Subscription {
}))
}

/// Get a crossbeam Receiver for subscription messages.
/// Useful for `crossbeam_channel::select` macro
///
/// # Example
/// ```
/// # fn main() -> std::io::Result<()> {
/// # let nc = nats::connect("demo.nats.io")?;
/// # let sub1 = nc.subscribe("foo")?;
/// # let sub2 = nc.subscribe("bar")?;
/// # nc.publish("foo", "hello")?;
/// let sub1_ch = sub1.receiver();
/// let sub2_ch = sub2.receiver();
/// crossbeam_channel::select! {
/// recv(sub1_ch) -> msg => {
/// println!("Got message from sub1: {:?}", msg);
/// Ok(())
/// }
/// recv(sub2_ch) -> msg => {
/// println!("Got message from sub2: {:?}", msg);
/// Ok(())
/// }
/// }
/// # }
/// ```
pub fn receiver(&self) -> &channel::Receiver<Message> {
&self.0.messages
}

/// Get the next message, or None if the subscription
/// has been unsubscribed or the connection closed.
///
Expand Down