Use subscription::run for download_progress example
This commit is contained in:
parent
35e4f30759
commit
75348c5b8c
1 changed files with 70 additions and 85 deletions
|
|
@ -1,45 +1,36 @@
|
||||||
|
use futures::Stream;
|
||||||
use iced_futures::futures;
|
use iced_futures::futures;
|
||||||
use std::hash::{Hash, Hasher};
|
use iced_native::subscription;
|
||||||
|
|
||||||
|
use std::hash::Hash;
|
||||||
|
|
||||||
// Just a little utility function
|
// Just a little utility function
|
||||||
pub fn file<I: 'static + Hash + Copy + Send, T: ToString>(
|
pub fn file<I: 'static + Hash + Copy + Send, T: ToString>(
|
||||||
id: I,
|
id: I,
|
||||||
url: T,
|
url: T,
|
||||||
) -> iced::Subscription<(I, Progress)> {
|
) -> iced::Subscription<(I, Progress)> {
|
||||||
iced::Subscription::from_recipe(Download {
|
subscription::run(
|
||||||
|
Download {
|
||||||
id,
|
id,
|
||||||
url: url.to_string(),
|
url: url.to_string(),
|
||||||
})
|
},
|
||||||
|
download,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Hash, Clone)]
|
||||||
pub struct Download<I> {
|
pub struct Download<I> {
|
||||||
id: I,
|
id: I,
|
||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure iced can use our download stream
|
fn download<I: Copy>(
|
||||||
impl<H, I, T> iced_native::subscription::Recipe<H, I> for Download<T>
|
download: Download<I>,
|
||||||
where
|
) -> impl Stream<Item = (I, Progress)> {
|
||||||
T: 'static + Hash + Copy + Send,
|
let id = download.id;
|
||||||
H: Hasher,
|
|
||||||
{
|
|
||||||
type Output = (T, Progress);
|
|
||||||
|
|
||||||
fn hash(&self, state: &mut H) {
|
futures::stream::unfold(
|
||||||
struct Marker;
|
State::Ready(download.url),
|
||||||
std::any::TypeId::of::<Marker>().hash(state);
|
|
||||||
|
|
||||||
self.id.hash(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stream(
|
|
||||||
self: Box<Self>,
|
|
||||||
_input: futures::stream::BoxStream<'static, I>,
|
|
||||||
) -> futures::stream::BoxStream<'static, Self::Output> {
|
|
||||||
let id = self.id;
|
|
||||||
|
|
||||||
Box::pin(futures::stream::unfold(
|
|
||||||
State::Ready(self.url),
|
|
||||||
move |state| async move {
|
move |state| async move {
|
||||||
match state {
|
match state {
|
||||||
State::Ready(url) => {
|
State::Ready(url) => {
|
||||||
|
|
@ -57,10 +48,7 @@ where
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
Some((
|
Some(((id, Progress::Errored), State::Finished))
|
||||||
(id, Progress::Errored),
|
|
||||||
State::Finished,
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
@ -91,9 +79,7 @@ where
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
Some(((id, Progress::Finished), State::Finished))
|
Some(((id, Progress::Finished), State::Finished))
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => Some(((id, Progress::Errored), State::Finished)),
|
||||||
Some(((id, Progress::Errored), State::Finished))
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
State::Finished => {
|
State::Finished => {
|
||||||
// We do not let the stream die, as it would start a
|
// We do not let the stream die, as it would start a
|
||||||
|
|
@ -105,8 +91,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
))
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue