Implement subscription::channel and simplify unfold

This commit is contained in:
Héctor Ramón Jiménez 2023-04-11 07:46:54 +02:00
parent ff24f9040c
commit ae7e6b3d48
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
3 changed files with 121 additions and 85 deletions

View file

@ -18,10 +18,7 @@ pub struct Download<I> {
url: String, url: String,
} }
async fn download<I: Copy>( async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) {
id: I,
state: State,
) -> (Option<(I, Progress)>, State) {
match state { match state {
State::Ready(url) => { State::Ready(url) => {
let response = reqwest::get(&url).await; let response = reqwest::get(&url).await;
@ -30,7 +27,7 @@ async fn download<I: Copy>(
Ok(response) => { Ok(response) => {
if let Some(total) = response.content_length() { if let Some(total) = response.content_length() {
( (
Some((id, Progress::Started)), (id, Progress::Started),
State::Downloading { State::Downloading {
response, response,
total, total,
@ -38,10 +35,10 @@ async fn download<I: Copy>(
}, },
) )
} else { } else {
(Some((id, Progress::Errored)), State::Finished) ((id, Progress::Errored), State::Finished)
} }
} }
Err(_) => (Some((id, Progress::Errored)), State::Finished), Err(_) => ((id, Progress::Errored), State::Finished),
} }
} }
State::Downloading { State::Downloading {
@ -55,7 +52,7 @@ async fn download<I: Copy>(
let percentage = (downloaded as f32 / total as f32) * 100.0; let percentage = (downloaded as f32 / total as f32) * 100.0;
( (
Some((id, Progress::Advanced(percentage))), (id, Progress::Advanced(percentage)),
State::Downloading { State::Downloading {
response, response,
total, total,
@ -63,8 +60,8 @@ async fn download<I: Copy>(
}, },
) )
} }
Ok(None) => (Some((id, Progress::Finished)), State::Finished), Ok(None) => ((id, Progress::Finished), State::Finished),
Err(_) => (Some((id, Progress::Errored)), State::Finished), Err(_) => ((id, Progress::Errored), State::Finished),
}, },
State::Finished => { State::Finished => {
// We do not let the stream die, as it would start a // We do not let the stream die, as it would start a

View file

@ -13,63 +13,67 @@ use std::fmt;
pub fn connect() -> Subscription<Event> { pub fn connect() -> Subscription<Event> {
struct Connect; struct Connect;
subscription::unfold( subscription::channel(
std::any::TypeId::of::<Connect>(), std::any::TypeId::of::<Connect>(),
State::Disconnected, 100,
|state| async move { |mut output| async move {
match state { let mut state = State::Disconnected;
State::Disconnected => {
const ECHO_SERVER: &str = "ws://localhost:3030";
match async_tungstenite::tokio::connect_async(ECHO_SERVER) loop {
match &mut state {
State::Disconnected => {
const ECHO_SERVER: &str = "ws://localhost:3030";
match async_tungstenite::tokio::connect_async(
ECHO_SERVER,
)
.await .await
{ {
Ok((websocket, _)) => { Ok((websocket, _)) => {
let (sender, receiver) = mpsc::channel(100); let (sender, receiver) = mpsc::channel(100);
( let _ = output
Some(Event::Connected(Connection(sender))), .send(Event::Connected(Connection(sender)))
State::Connected(websocket, receiver), .await;
)
}
Err(_) => {
tokio::time::sleep(
tokio::time::Duration::from_secs(1),
)
.await;
(Some(Event::Disconnected), State::Disconnected) state = State::Connected(websocket, receiver);
} }
} Err(_) => {
} tokio::time::sleep(
State::Connected(mut websocket, mut input) => { tokio::time::Duration::from_secs(1),
let mut fused_websocket = websocket.by_ref().fuse(); )
.await;
futures::select! { let _ = output.send(Event::Disconnected).await;
received = fused_websocket.select_next_some() => {
match received {
Ok(tungstenite::Message::Text(message)) => {
(
Some(Event::MessageReceived(Message::User(message))),
State::Connected(websocket, input)
)
}
Ok(_) => {
(None, State::Connected(websocket, input))
}
Err(_) => {
(Some(Event::Disconnected), State::Disconnected)
}
} }
} }
}
State::Connected(websocket, input) => {
let mut fused_websocket = websocket.by_ref().fuse();
message = input.select_next_some() => { futures::select! {
let result = websocket.send(tungstenite::Message::Text(message.to_string())).await; received = fused_websocket.select_next_some() => {
match received {
Ok(tungstenite::Message::Text(message)) => {
let _ = output.send(Event::MessageReceived(Message::User(message))).await;
}
Err(_) => {
let _ = output.send(Event::Disconnected).await;
if result.is_ok() { state = State::Disconnected;
(None, State::Connected(websocket, input)) }
} else { Ok(_) => continue,
(Some(Event::Disconnected), State::Disconnected) }
}
message = input.select_next_some() => {
let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
if !result.is_ok() {
let _ = output.send(Event::Disconnected).await;
state = State::Disconnected;
}
} }
} }
} }

View file

@ -3,6 +3,8 @@ use crate::event::{self, Event};
use crate::window; use crate::window;
use crate::Hasher; use crate::Hasher;
use iced_futures::futures::channel::mpsc;
use iced_futures::futures::never::Never;
use iced_futures::futures::{self, Future, Stream}; use iced_futures::futures::{self, Future, Stream};
use iced_futures::{BoxStream, MaybeSend}; use iced_futures::{BoxStream, MaybeSend};
@ -133,6 +135,27 @@ where
/// [`Stream`] that will call the provided closure to produce every `Message`. /// [`Stream`] that will call the provided closure to produce every `Message`.
/// ///
/// The `id` will be used to uniquely identify the [`Subscription`]. /// The `id` will be used to uniquely identify the [`Subscription`].
pub fn unfold<I, T, Fut, Message>(
id: I,
initial: T,
mut f: impl FnMut(T) -> Fut + MaybeSend + Sync + 'static,
) -> Subscription<Message>
where
I: Hash + 'static,
T: MaybeSend + 'static,
Fut: Future<Output = (Message, T)> + MaybeSend + 'static,
Message: 'static + MaybeSend,
{
use futures::future::FutureExt;
run_with_id(
id,
futures::stream::unfold(initial, move |state| f(state).map(Some)),
)
}
/// Creates a [`Subscription`] that publishes the events sent from a [`Future`]
/// to an [`mpsc::Sender`] with the given bounds.
/// ///
/// # Creating an asynchronous worker with bidirectional communication /// # Creating an asynchronous worker with bidirectional communication
/// You can leverage this helper to create a [`Subscription`] that spawns /// You can leverage this helper to create a [`Subscription`] that spawns
@ -145,6 +168,7 @@ where
/// ``` /// ```
/// use iced_native::subscription::{self, Subscription}; /// use iced_native::subscription::{self, Subscription};
/// use iced_native::futures::channel::mpsc; /// use iced_native::futures::channel::mpsc;
/// use iced_native::futures::sink::SinkExt;
/// ///
/// pub enum Event { /// pub enum Event {
/// Ready(mpsc::Sender<Input>), /// Ready(mpsc::Sender<Input>),
@ -165,27 +189,35 @@ where
/// fn some_worker() -> Subscription<Event> { /// fn some_worker() -> Subscription<Event> {
/// struct SomeWorker; /// struct SomeWorker;
/// ///
/// subscription::unfold(std::any::TypeId::of::<SomeWorker>(), State::Starting, |state| async move { /// subscription::channel(std::any::TypeId::of::<SomeWorker>(), 100, |mut output| async move {
/// match state { /// let mut state = State::Starting;
/// State::Starting => {
/// // Create channel
/// let (sender, receiver) = mpsc::channel(100);
/// ///
/// (Some(Event::Ready(sender)), State::Ready(receiver)) /// loop {
/// } /// match &mut state {
/// State::Ready(mut receiver) => { /// State::Starting => {
/// use iced_native::futures::StreamExt; /// // Create channel
/// let (sender, receiver) = mpsc::channel(100);
/// ///
/// // Read next input sent from `Application` /// // Send the sender back to the application
/// let input = receiver.select_next_some().await; /// output.send(Event::Ready(sender)).await;
/// ///
/// match input { /// // We are ready to receive messages
/// Input::DoSomeWork => { /// state = State::Ready(receiver);
/// // Do some async work... /// }
/// State::Ready(receiver) => {
/// use iced_native::futures::StreamExt;
/// ///
/// // Finally, we can optionally return a message to tell the /// // Read next input sent from `Application`
/// // `Application` the work is done /// let input = receiver.select_next_some().await;
/// (Some(Event::WorkFinished), State::Ready(receiver)) ///
/// 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;
/// }
/// } /// }
/// } /// }
/// } /// }
@ -198,25 +230,28 @@ where
/// connection open. /// connection open.
/// ///
/// [`websocket`]: https://github.com/iced-rs/iced/tree/0.8/examples/websocket /// [`websocket`]: https://github.com/iced-rs/iced/tree/0.8/examples/websocket
pub fn unfold<I, T, Fut, Message>( pub fn channel<I, Fut, Message>(
id: I, id: I,
initial: T, size: usize,
mut f: impl FnMut(T) -> Fut + MaybeSend + Sync + 'static, f: impl Fn(mpsc::Sender<Message>) -> Fut + MaybeSend + Sync + 'static,
) -> Subscription<Message> ) -> Subscription<Message>
where where
I: Hash + 'static, I: Hash + 'static,
T: MaybeSend + 'static, Fut: Future<Output = Never> + MaybeSend + 'static,
Fut: Future<Output = (Option<Message>, T)> + MaybeSend + 'static,
Message: 'static + MaybeSend, Message: 'static + MaybeSend,
{ {
use futures::future::{self, FutureExt}; use futures::stream::{self, StreamExt};
use futures::stream::StreamExt;
run_with_id( Subscription::from_recipe(Runner {
id, id,
futures::stream::unfold(initial, move |state| f(state).map(Some)) spawn: move |_| {
.filter_map(future::ready), let (sender, receiver) = mpsc::channel(size);
)
let runner = stream::once(f(sender)).map(|_| unreachable!());
stream::select(receiver, runner)
},
})
} }
struct Runner<I, F, S, Message> struct Runner<I, F, S, Message>