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
69
70
71
72
use std::rc::Rc;

use yew::{Component, ComponentLink, Html, Properties, ShouldRender};

use crate::handle::Handle;
use crate::{SharedState, SharedStateComponent};

pub type Render<H> = Rc<dyn Fn(&H) -> Html>;

#[derive(Properties, Clone)]
pub struct Props<H>
where
    H: Handle + Clone + Default,
{
    #[prop_or_default]
    handle: H,
    pub view: Render<H>,
}

impl<H> SharedState for Props<H>
where
    H: Handle + Clone + Default,
{
    type Handle = H;

    fn handle(&mut self) -> &mut Self::Handle {
        &mut self.handle
    }
}

pub enum Msg {}

pub struct Model<H>
where
    H: Handle + Clone + Default,
{
    props: Props<H>,
}

impl<H> Component for Model<H>
where
    H: Handle + Default + Clone + 'static,
{
    type Message = Msg;
    type Properties = Props<H>;

    fn create(props: Self::Properties, _link: ComponentLink<Self>) -> Self {
        Self { props }
    }

    fn update(&mut self, _msg: Self::Message) -> ShouldRender {
        true
    }

    fn view(&self) -> Html {
        (self.props.view)(&self.props.handle)
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        self.props = props;
        true
    }
}

pub type StateView<H, SCOPE = H> = SharedStateComponent<Model<H>, SCOPE>;

pub fn view_state<F, H>(f: F) -> Render<H>
where
    F: Fn(&H) -> Html + 'static,
{
    Rc::new(f)
}