Skip to content

Commit

Permalink
Use sipper in download_progress
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 10, 2025
1 parent 54ffbbf commit 05618ea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 38 deletions.
23 changes: 9 additions & 14 deletions examples/download_progress/src/download.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)]
Expand Down
52 changes: 28 additions & 24 deletions examples/download_progress/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ struct Example {
pub enum Message {
Add,
Download(usize),
DownloadProgressed(usize, Result<download::Progress, download::Error>),
DownloadUpdated(usize, Update),
}

impl Example {
Expand All @@ -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()
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -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;
}
Ok(download::Progress::Finished) => {
self.state = State::Finished;
match update {
Update::Downloading(new_progress) => {
*progress = new_progress.percent;
}
Err(_error) => {
self.state = State::Errored;
Update::Finished(result) => {
self.state = if result.is_ok() {
State::Finished
} else {
State::Errored
};
}
}
}
Expand Down

0 comments on commit 05618ea

Please sign in to comment.