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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use std::rc::Rc;
use yew::{
agent::{Bridge, Bridged},
prelude::*,
};
use crate::handle::{Handle, SharedState, WrapperHandle};
use crate::handler::{HandlerLink, Reduction, ReductionOnce, StateHandler};
use crate::service::*;
type PropHandle<SHARED> = <SHARED as SharedState>::Handle;
type PropHandler<SHARED> = <PropHandle<SHARED> as Handle>::Handler;
type Model<T> = <PropHandler<T> as StateHandler>::Model;
#[doc(hidden)]
pub enum SharedStateComponentMsg<SHARED>
where
SHARED: SharedState,
<SHARED as SharedState>::Handle: WrapperHandle,
PropHandler<SHARED>: 'static,
{
SetLocal(Rc<Model<SHARED>>),
SetLink(HandlerLink<PropHandler<SHARED>>),
Apply(Reduction<Model<SHARED>>),
ApplyOnce(ReductionOnce<Model<SHARED>>),
Ignore,
}
pub struct SharedStateComponent<C, SCOPE = PropHandler<<C as Component>::Properties>>
where
C: Component,
C::Properties: SharedState + Clone,
PropHandle<C::Properties>: WrapperHandle,
SCOPE: 'static,
{
props: C::Properties,
bridge: Box<dyn Bridge<StateService<PropHandler<C::Properties>, SCOPE>>>,
link_set: bool,
state_set: bool,
}
impl<C, SCOPE> Component for SharedStateComponent<C, SCOPE>
where
C: Component,
C::Properties: SharedState + Clone,
<C::Properties as SharedState>::Handle: Clone + WrapperHandle,
{
type Message = SharedStateComponentMsg<C::Properties>;
type Properties = C::Properties;
fn create(mut props: Self::Properties, link: ComponentLink<Self>) -> Self {
use SharedStateComponentMsg::*;
let callback = link.callback(|msg| match msg {
ServiceOutput::Service(ServiceResponse::State(state)) => SetLocal(state),
ServiceOutput::Service(ServiceResponse::Link(link)) => SetLink(link),
ServiceOutput::Handler(_) => Ignore,
});
let mut bridge = StateService::bridge(callback);
bridge.send(ServiceInput::Service(ServiceRequest::Subscribe));
props
.handle()
.set_callbacks(link.callback(Apply), link.callback(ApplyOnce));
Self {
props,
bridge,
state_set: Default::default(),
link_set: Default::default(),
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
use SharedStateComponentMsg::*;
match msg {
Apply(reduce) => {
self.bridge
.send(ServiceInput::Service(ServiceRequest::Apply(reduce)));
false
}
ApplyOnce(reduce) => {
self.bridge
.send(ServiceInput::Service(ServiceRequest::ApplyOnce(reduce)));
false
}
SetLocal(state) => {
self.props.handle().set_state(state);
self.state_set = true;
true
}
SetLink(link) => {
self.props.handle().set_link(link);
self.link_set = true;
true
}
Ignore => false,
}
}
fn change(&mut self, mut props: Self::Properties) -> ShouldRender {
*props.handle() = self.props.handle().clone();
self.props = props;
true
}
fn view(&self) -> Html {
if self.link_set && self.state_set {
let props = self.props.clone();
html! {
<C with props />
}
} else {
html! {}
}
}
}