Skip to main content

functora_core/
progress.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub enum Stage {
7    #[default]
8    Attach,
9    Zip,
10    Encrypt,
11    Decrypt,
12    Unzip,
13    Download,
14    Preview,
15}
16
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct Job<S> {
19    pub stage: S,
20    pub done: u64,
21    pub total: u64,
22    pub name: Option<String>,
23}
24
25impl<S> Job<S> {
26    pub fn percent(&self) -> u8 {
27        (self.done * 100 / self.total.max(1)).min(100) as u8
28    }
29}
30
31pub async fn yield_to_paint() {
32    #[cfg(target_arch = "wasm32")]
33    {
34        gloo_timers::future::TimeoutFuture::new(1).await;
35    }
36    #[cfg(not(target_arch = "wasm32"))]
37    {
38        YieldOnce(false).await;
39    }
40}
41
42pub struct YieldOnce(bool);
43
44impl YieldOnce {
45    #[must_use]
46    pub const fn new(ready: bool) -> Self {
47        Self(ready)
48    }
49}
50
51impl Future for YieldOnce {
52    type Output = ();
53
54    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
55        if self.0 {
56            Poll::Ready(())
57        } else {
58            self.0 = true;
59            cx.waker().wake_by_ref();
60            Poll::Pending
61        }
62    }
63}