Use sipper in download_progress
This commit is contained in:
parent
54ffbbf043
commit
05618ea9b3
2 changed files with 37 additions and 38 deletions
|
|
@ -1,16 +1,14 @@
|
|||
use iced::futures::{SinkExt, Stream, StreamExt};
|
||||
use iced::stream::try_channel;
|
||||
use iced::futures::StreamExt;
|
||||
use iced::task::{sipper, Straw};
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
pub fn download(
|
||||
url: impl AsRef<str>,
|
||||
) -> impl Stream<Item = Result<Progress, Error>> {
|
||||
try_channel(1, move |mut output| async move {
|
||||
pub fn download(url: impl AsRef<str>) -> impl Straw<(), Progress, Error> {
|
||||
sipper(move |mut progress| async move {
|
||||
let response = reqwest::get(url.as_ref()).await?;
|
||||
let total = response.content_length().ok_or(Error::NoContentLength)?;
|
||||
|
||||
let _ = output.send(Progress::Downloading { percent: 0.0 }).await;
|
||||
let _ = progress.send(Progress { percent: 0.0 }).await;
|
||||
|
||||
let mut byte_stream = response.bytes_stream();
|
||||
let mut downloaded = 0;
|
||||
|
|
@ -19,23 +17,20 @@ pub fn download(
|
|||
let bytes = next_bytes?;
|
||||
downloaded += bytes.len();
|
||||
|
||||
let _ = output
|
||||
.send(Progress::Downloading {
|
||||
let _ = progress
|
||||
.send(Progress {
|
||||
percent: 100.0 * downloaded as f32 / total as f32,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
let _ = output.send(Progress::Finished).await;
|
||||
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Progress {
|
||||
Downloading { percent: f32 },
|
||||
Finished,
|
||||
pub struct Progress {
|
||||
pub percent: f32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ struct Example {
|
|||
pub enum Message {
|
||||
Add,
|
||||
Download(usize),
|
||||
DownloadProgressed(usize, Result<download::Progress, download::Error>),
|
||||
DownloadUpdated(usize, Update),
|
||||
}
|
||||
|
||||
impl Example {
|
||||
|
|
@ -52,15 +52,13 @@ impl Example {
|
|||
|
||||
let task = download.start();
|
||||
|
||||
task.map(move |progress| {
|
||||
Message::DownloadProgressed(index, progress)
|
||||
})
|
||||
task.map(move |update| Message::DownloadUpdated(index, update))
|
||||
}
|
||||
Message::DownloadProgressed(id, progress) => {
|
||||
Message::DownloadUpdated(id, update) => {
|
||||
if let Some(download) =
|
||||
self.downloads.iter_mut().find(|download| download.id == id)
|
||||
{
|
||||
download.progress(progress);
|
||||
download.update(update);
|
||||
}
|
||||
|
||||
Task::none()
|
||||
|
|
@ -95,6 +93,12 @@ struct Download {
|
|||
state: State,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Update {
|
||||
Downloading(download::Progress),
|
||||
Finished(Result<(), download::Error>),
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Idle,
|
||||
|
|
@ -111,18 +115,20 @@ impl Download {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn start(
|
||||
&mut self,
|
||||
) -> Task<Result<download::Progress, download::Error>> {
|
||||
pub fn start(&mut self) -> Task<Update> {
|
||||
match self.state {
|
||||
State::Idle { .. }
|
||||
| State::Finished { .. }
|
||||
| State::Errored { .. } => {
|
||||
let (task, handle) = Task::stream(download(
|
||||
"https://huggingface.co/\
|
||||
let (task, handle) = Task::sip(
|
||||
download(
|
||||
"https://huggingface.co/\
|
||||
mattshumer/Reflection-Llama-3.1-70B/\
|
||||
resolve/main/model-00001-of-00162.safetensors",
|
||||
))
|
||||
),
|
||||
Update::Downloading,
|
||||
Update::Finished,
|
||||
)
|
||||
.abortable();
|
||||
|
||||
self.state = State::Downloading {
|
||||
|
|
@ -136,20 +142,18 @@ impl Download {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn progress(
|
||||
&mut self,
|
||||
new_progress: Result<download::Progress, download::Error>,
|
||||
) {
|
||||
pub fn update(&mut self, update: Update) {
|
||||
if let State::Downloading { progress, .. } = &mut self.state {
|
||||
match new_progress {
|
||||
Ok(download::Progress::Downloading { percent }) => {
|
||||
*progress = percent;
|
||||
match update {
|
||||
Update::Downloading(new_progress) => {
|
||||
*progress = new_progress.percent;
|
||||
}
|
||||
Ok(download::Progress::Finished) => {
|
||||
self.state = State::Finished;
|
||||
}
|
||||
Err(_error) => {
|
||||
self.state = State::Errored;
|
||||
Update::Finished(result) => {
|
||||
self.state = if result.is_ok() {
|
||||
State::Finished
|
||||
} else {
|
||||
State::Errored
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue