Simplify subscription::channel example

This commit is contained in:
Héctor Ramón Jiménez 2024-07-02 19:01:04 +02:00
parent 4687bf7f14
commit 2b19471d1c
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -369,44 +369,29 @@ where
/// // ... /// // ...
/// } /// }
/// ///
/// enum State {
/// Starting,
/// Ready(mpsc::Receiver<Input>),
/// }
///
/// fn some_worker() -> Subscription<Event> { /// fn some_worker() -> Subscription<Event> {
/// struct SomeWorker; /// struct SomeWorker;
/// ///
/// subscription::channel(std::any::TypeId::of::<SomeWorker>(), 100, |mut output| async move { /// subscription::channel(std::any::TypeId::of::<SomeWorker>(), 100, |mut output| async move {
/// let mut state = State::Starting; /// // Create channel
/// let (sender, mut receiver) = mpsc::channel(100);
///
/// // Send the sender back to the application
/// output.send(Event::Ready(sender)).await;
/// ///
/// loop { /// loop {
/// match &mut state { /// use iced_futures::futures::StreamExt;
/// State::Starting => {
/// // Create channel
/// let (sender, receiver) = mpsc::channel(100);
/// ///
/// // Send the sender back to the application /// // Read next input sent from `Application`
/// output.send(Event::Ready(sender)).await; /// let input = receiver.select_next_some().await;
/// ///
/// // We are ready to receive messages /// match input {
/// state = State::Ready(receiver); /// Input::DoSomeWork => {
/// } /// // Do some async work...
/// State::Ready(receiver) => {
/// use iced_futures::futures::StreamExt;
/// ///
/// // Read next input sent from `Application` /// // Finally, we can optionally produce a message to tell the
/// let input = receiver.select_next_some().await; /// // `Application` the work is done
/// /// output.send(Event::WorkFinished).await;
/// 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;
/// }
/// }
/// } /// }
/// } /// }
/// } /// }