Skip to content

Commit

Permalink
Simplify subscription::channel example
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jul 2, 2024
1 parent 4687bf7 commit 2b19471
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions futures/src/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,44 +369,29 @@ where
/// // ...
/// }
///
/// enum State {
/// Starting,
/// Ready(mpsc::Receiver<Input>),
/// }
///
/// fn some_worker() -> Subscription<Event> {
/// struct SomeWorker;
///
/// subscription::channel(std::any::TypeId::of::<SomeWorker>(), 100, |mut output| async move {
/// let mut state = State::Starting;
///
/// loop {
/// match &mut state {
/// State::Starting => {
/// // Create channel
/// let (sender, receiver) = mpsc::channel(100);
/// // Create channel
/// let (sender, mut receiver) = mpsc::channel(100);
///
/// // Send the sender back to the application
/// output.send(Event::Ready(sender)).await;
/// // Send the sender back to the application
/// output.send(Event::Ready(sender)).await;
///
/// // We are ready to receive messages
/// state = State::Ready(receiver);
/// }
/// State::Ready(receiver) => {
/// use iced_futures::futures::StreamExt;
/// loop {
/// use iced_futures::futures::StreamExt;
///
/// // Read next input sent from `Application`
/// let input = receiver.select_next_some().await;
/// // Read next input sent from `Application`
/// let input = receiver.select_next_some().await;
///
/// match input {
/// Input::DoSomeWork => {
/// // Do some async work...
/// match input {
/// Input::DoSomeWork => {
/// // Do some async work...
///
/// // Finally, we can optionally produce a message to tell the
/// // `Application` the work is done
/// output.send(Event::WorkFinished).await;
/// }
/// }
/// // Finally, we can optionally produce a message to tell the
/// // `Application` the work is done
/// output.send(Event::WorkFinished).await;
/// }
/// }
/// }
Expand Down

0 comments on commit 2b19471

Please sign in to comment.