summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/download_progress/src/download.rs14
-rw-r--r--examples/websocket/src/echo.rs102
-rw-r--r--examples/websocket/src/main.rs2
3 files changed, 58 insertions, 60 deletions
diff --git a/examples/download_progress/src/download.rs b/examples/download_progress/src/download.rs
index d6cc1e24..bdf57290 100644
--- a/examples/download_progress/src/download.rs
+++ b/examples/download_progress/src/download.rs
@@ -1,4 +1,5 @@
-use iced::subscription;
+use iced::futures;
+use iced::Subscription;
use std::hash::Hash;
@@ -7,9 +8,14 @@ pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>(
id: I,
url: T,
) -> iced::Subscription<(I, Progress)> {
- subscription::unfold(id, State::Ready(url.to_string()), move |state| {
- download(id, state)
- })
+ Subscription::run_with_id(
+ id,
+ futures::stream::unfold(State::Ready(url.to_string()), move |state| {
+ use iced::futures::FutureExt;
+
+ download(id, state).map(Some)
+ }),
+ )
}
async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) {
diff --git a/examples/websocket/src/echo.rs b/examples/websocket/src/echo.rs
index cd32cb66..14652936 100644
--- a/examples/websocket/src/echo.rs
+++ b/examples/websocket/src/echo.rs
@@ -1,87 +1,79 @@
pub mod server;
use iced::futures;
-use iced::subscription::{self, Subscription};
+use iced::stream;
use iced::widget::text;
use futures::channel::mpsc;
use futures::sink::SinkExt;
-use futures::stream::StreamExt;
+use futures::stream::{Stream, StreamExt};
use async_tungstenite::tungstenite;
use std::fmt;
-pub fn connect() -> Subscription<Event> {
- struct Connect;
+pub fn connect() -> impl Stream<Item = Event> {
+ stream::channel(100, |mut output| async move {
+ let mut state = State::Disconnected;
- subscription::channel(
- std::any::TypeId::of::<Connect>(),
- 100,
- |mut output| async move {
- let mut state = State::Disconnected;
+ loop {
+ match &mut state {
+ State::Disconnected => {
+ const ECHO_SERVER: &str = "ws://127.0.0.1:3030";
- loop {
- match &mut state {
- State::Disconnected => {
- const ECHO_SERVER: &str = "ws://127.0.0.1:3030";
-
- match async_tungstenite::tokio::connect_async(
- ECHO_SERVER,
- )
+ match async_tungstenite::tokio::connect_async(ECHO_SERVER)
.await
- {
- Ok((websocket, _)) => {
- let (sender, receiver) = mpsc::channel(100);
-
- let _ = output
- .send(Event::Connected(Connection(sender)))
- .await;
+ {
+ Ok((websocket, _)) => {
+ let (sender, receiver) = mpsc::channel(100);
- state = State::Connected(websocket, receiver);
- }
- Err(_) => {
- tokio::time::sleep(
- tokio::time::Duration::from_secs(1),
- )
+ let _ = output
+ .send(Event::Connected(Connection(sender)))
.await;
- let _ = output.send(Event::Disconnected).await;
- }
+ state = State::Connected(websocket, receiver);
+ }
+ Err(_) => {
+ tokio::time::sleep(
+ tokio::time::Duration::from_secs(1),
+ )
+ .await;
+
+ let _ = output.send(Event::Disconnected).await;
}
}
- State::Connected(websocket, input) => {
- let mut fused_websocket = websocket.by_ref().fuse();
-
- futures::select! {
- 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;
-
- state = State::Disconnected;
- }
- Ok(_) => continue,
+ }
+ State::Connected(websocket, input) => {
+ let mut fused_websocket = websocket.by_ref().fuse();
+
+ futures::select! {
+ received = fused_websocket.select_next_some() => {
+ match received {
+ Ok(tungstenite::Message::Text(message)) => {
+ let _ = output.send(Event::MessageReceived(Message::User(message))).await;
}
- }
-
- message = input.select_next_some() => {
- let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
-
- if result.is_err() {
+ Err(_) => {
let _ = output.send(Event::Disconnected).await;
state = State::Disconnected;
}
+ Ok(_) => continue,
+ }
+ }
+
+ message = input.select_next_some() => {
+ let result = websocket.send(tungstenite::Message::Text(message.to_string())).await;
+
+ if result.is_err() {
+ let _ = output.send(Event::Disconnected).await;
+
+ state = State::Disconnected;
}
}
}
}
}
- },
- )
+ }
+ })
}
#[derive(Debug)]
diff --git a/examples/websocket/src/main.rs b/examples/websocket/src/main.rs
index 8422ce16..95a14fd9 100644
--- a/examples/websocket/src/main.rs
+++ b/examples/websocket/src/main.rs
@@ -83,7 +83,7 @@ impl WebSocket {
}
fn subscription(&self) -> Subscription<Message> {
- echo::connect().map(Message::Echo)
+ Subscription::run(echo::connect).map(Message::Echo)
}
fn view(&self) -> Element<Message> {