Skip to content

Commit

Permalink
Support wasm target for download_progress
Browse files Browse the repository at this point in the history
  • Loading branch information
skygrango authored and BuildTools committed May 3, 2024
1 parent fe240a9 commit 5181677
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
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"]
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

0 comments on commit 5181677

Please sign in to comment.