1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

use async_component_core::{AsyncComponent, ComponentPollFlags};

pub struct SuspenseComponent<Fallback, Component>(SuspenseVariant<Fallback, Component>);

impl<F: AsyncComponent, C: AsyncComponent> SuspenseComponent<F, C> {
    pub fn new(fallback: F, fut: impl Future<Output = C> + 'static) -> Self {
        Self(SuspenseVariant::Loading {
            fallback,
            fut: Box::pin(fut),
        })
    }

    pub const fn is_ready(&self) -> bool {
        matches!(self.0, SuspenseVariant::Ready { .. })
    }

    pub const fn is_loading(&self) -> bool {
        matches!(self.0, SuspenseVariant::Loading { .. })
    }

    pub const fn get(&self) -> Result<&C, &F> {
        match self.0 {
            SuspenseVariant::Loading { ref fallback, .. } => Err(fallback),
            SuspenseVariant::Ready(ref component) => Ok(component),
        }
    }

    pub fn get_mut(&mut self) -> Result<&mut C, &mut F> {
        match self.0 {
            SuspenseVariant::Loading {
                ref mut fallback, ..
            } => Err(fallback),
            SuspenseVariant::Ready(ref mut component) => Ok(component),
        }
    }
}

impl<F: AsyncComponent, C: AsyncComponent> AsyncComponent for SuspenseComponent<F, C> {
    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<ComponentPollFlags> {
        if let SuspenseVariant::Loading { ref mut fut, .. } = self.0 {
            if let Poll::Ready(component) = Pin::new(fut).poll(cx) {
                self.0 = SuspenseVariant::Ready(component);
            }
        }

        match self.0 {
            SuspenseVariant::Loading {
                ref mut fallback, ..
            } => Pin::new(fallback).poll_next(cx),

            SuspenseVariant::Ready(ref mut component) => Pin::new(component).poll_next(cx),
        }
    }
}

enum SuspenseVariant<Fallback, Component> {
    Loading {
        fallback: Fallback,
        fut: Pin<Box<dyn Future<Output = Component>>>,
    },
    Ready(Component),
}