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

Support wasm target for download_progress #2419

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions examples/download_progress/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ edition = "2021"
publish = false

[dependencies]
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced.workspace = true
iced.features = ["tokio"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
iced.workspace = true
iced.features = ["tokio", "webgl"]

bytes = "1.6"
futures = "0.3"

[dependencies.reqwest]
version = "0.11"
version = "0.12"
default-features = false
features = ["rustls-tls"]
features = ["rustls-tls", "stream"]
12 changes: 12 additions & 0 deletions examples/download_progress/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
<meta charset="utf-8" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Download_Progress - Iced</title>
<base data-trunk-public-url />
</head>
<body style="height: 100%; margin: 0">
<link data-trunk rel="rust" href="Cargo.toml" data-wasm-opt="z" data-bin="download_progress" />
</body>
</html>
54 changes: 35 additions & 19 deletions examples/download_progress/src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use iced::subscription;

use std::hash::Hash;

#[cfg(target_arch = "wasm32")]
use futures::{Stream, StreamExt};
#[cfg(target_arch = "wasm32")]
use std::pin::Pin;
// Just a little utility function
pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>(
id: I,
Expand All @@ -16,14 +20,16 @@ async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) {
match state {
State::Ready(url) => {
let response = reqwest::get(&url).await;

match response {
Ok(response) => {
if let Some(total) = response.content_length() {
(
(id, Progress::Started),
State::Downloading {
response,
#[cfg(target_arch = "wasm32")]
stream: response.bytes_stream().boxed_local(),
#[cfg(not(target_arch = "wasm32"))]
stream: response,
total,
downloaded: 0,
},
Expand All @@ -36,27 +42,33 @@ async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) {
}
}
State::Downloading {
mut response,
mut stream,
total,
downloaded,
} => match response.chunk().await {
Ok(Some(chunk)) => {
let downloaded = downloaded + chunk.len() as u64;
} => {
#[cfg(target_arch = "wasm32")]
let chunk = stream.next().await.map_or(Ok(None), |v| v.map(Some));
#[cfg(not(target_arch = "wasm32"))]
let chunk = stream.chunk().await;
match chunk {
Ok(Some(chunk)) => {
let downloaded = downloaded + chunk.len() as u64;

let percentage = (downloaded as f32 / total as f32) * 100.0;
let percentage = (downloaded as f32 / total as f32) * 100.0;

(
(id, Progress::Advanced(percentage)),
State::Downloading {
response,
total,
downloaded,
},
)
(
(id, Progress::Advanced(percentage)),
State::Downloading {
stream,
total,
downloaded,
},
)
}
Ok(None) => ((id, Progress::Finished), State::Finished),
Err(_) => ((id, Progress::Errored), State::Finished),
}
Ok(None) => ((id, Progress::Finished), State::Finished),
Err(_) => ((id, Progress::Errored), State::Finished),
},
}
State::Finished => {
// We do not let the stream die, as it would start a
// new download repeatedly if the user is not careful
Expand All @@ -77,7 +89,11 @@ pub enum Progress {
pub enum State {
Ready(String),
Downloading {
response: reqwest::Response,
#[cfg(target_arch = "wasm32")]
stream:
Pin<Box<dyn Stream<Item = Result<bytes::Bytes, reqwest::Error>>>>,
#[cfg(not(target_arch = "wasm32"))]
stream: reqwest::Response,
total: u64,
downloaded: u64,
},
Expand Down