-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2419 from skygrango/wasm/support-download_progress
Support wasm target for `download_progress`
- Loading branch information
Showing
4 changed files
with
60 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,62 @@ | ||
use iced::futures; | ||
use iced::futures::{SinkExt, Stream, StreamExt}; | ||
use iced::stream::try_channel; | ||
use iced::Subscription; | ||
|
||
use std::hash::Hash; | ||
use std::sync::Arc; | ||
|
||
// Just a little utility function | ||
pub fn file<I: 'static + Hash + Copy + Send + Sync, T: ToString>( | ||
id: I, | ||
url: T, | ||
) -> iced::Subscription<(I, Progress)> { | ||
) -> iced::Subscription<(I, Result<Progress, Error>)> { | ||
Subscription::run_with_id( | ||
id, | ||
futures::stream::unfold(State::Ready(url.to_string()), move |state| { | ||
use iced::futures::FutureExt; | ||
|
||
download(id, state).map(Some) | ||
}), | ||
download(url.to_string()).map(move |progress| (id, progress)), | ||
) | ||
} | ||
|
||
async fn download<I: Copy>(id: I, state: State) -> ((I, Progress), State) { | ||
match state { | ||
State::Ready(url) => { | ||
let response = reqwest::get(&url).await; | ||
fn download(url: String) -> impl Stream<Item = Result<Progress, Error>> { | ||
try_channel(1, move |mut output| async move { | ||
let response = reqwest::get(&url).await?; | ||
let total = response.content_length().ok_or(Error::NoContentLength)?; | ||
|
||
match response { | ||
Ok(response) => { | ||
if let Some(total) = response.content_length() { | ||
( | ||
(id, Progress::Started), | ||
State::Downloading { | ||
response, | ||
total, | ||
downloaded: 0, | ||
}, | ||
) | ||
} else { | ||
((id, Progress::Errored), State::Finished) | ||
} | ||
} | ||
Err(_) => ((id, Progress::Errored), State::Finished), | ||
} | ||
} | ||
State::Downloading { | ||
mut response, | ||
total, | ||
downloaded, | ||
} => match response.chunk().await { | ||
Ok(Some(chunk)) => { | ||
let downloaded = downloaded + chunk.len() as u64; | ||
let _ = output.send(Progress::Downloading { percent: 0.0 }).await; | ||
|
||
let mut byte_stream = response.bytes_stream(); | ||
let mut downloaded = 0; | ||
|
||
let percentage = (downloaded as f32 / total as f32) * 100.0; | ||
while let Some(next_bytes) = byte_stream.next().await { | ||
let bytes = next_bytes?; | ||
downloaded += bytes.len(); | ||
|
||
( | ||
(id, Progress::Advanced(percentage)), | ||
State::Downloading { | ||
response, | ||
total, | ||
downloaded, | ||
}, | ||
) | ||
} | ||
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 | ||
// in case of errors. | ||
iced::futures::future::pending().await | ||
let _ = output | ||
.send(Progress::Downloading { | ||
percent: 100.0 * downloaded as f32 / total as f32, | ||
}) | ||
.await; | ||
} | ||
} | ||
|
||
let _ = output.send(Progress::Finished).await; | ||
|
||
Ok(()) | ||
}) | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum Progress { | ||
Started, | ||
Advanced(f32), | ||
Downloading { percent: f32 }, | ||
Finished, | ||
Errored, | ||
} | ||
|
||
pub enum State { | ||
Ready(String), | ||
Downloading { | ||
response: reqwest::Response, | ||
total: u64, | ||
downloaded: u64, | ||
}, | ||
Finished, | ||
#[derive(Debug, Clone)] | ||
pub enum Error { | ||
RequestFailed(Arc<reqwest::Error>), | ||
NoContentLength, | ||
} | ||
|
||
impl From<reqwest::Error> for Error { | ||
fn from(error: reqwest::Error) -> Self { | ||
Error::RequestFailed(Arc::new(error)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters