use crate::{Cmd, ViewCtx};
pub trait Component: Sized {
type Props;
type State;
type Msg: Clone + Send + 'static;
fn init(props: &Self::Props) -> Self::State;
fn update(state: &mut Self::State, msg: Self::Msg) -> Cmd<Self::Msg>;
fn view(props: &Self::Props, state: &Self::State, ctx: &mut ViewCtx<Self::Msg>);
}
impl<'a, ParentMsg> ViewCtx<'a, ParentMsg> {
pub fn mount<C>(
&mut self,
props: &C::Props,
state: &C::State,
map_msg: impl Fn(C::Msg) -> ParentMsg,
) where
C: Component,
{
let mut child_msgs: Vec<C::Msg> = Vec::new();
{
let mut child_ctx = ViewCtx::new(self.ui, &mut child_msgs);
C::view(props, state, &mut child_ctx);
}
for child_msg in child_msgs {
self.emit(map_msg(child_msg));
}
}
pub fn mount_mut<C>(
&mut self,
props: &C::Props,
state: &mut C::State,
map_msg: impl Fn(C::Msg) -> ParentMsg,
) where
C: Component,
{
let mut child_msgs: Vec<C::Msg> = Vec::new();
{
let mut child_ctx = ViewCtx::new(self.ui, &mut child_msgs);
C::view(props, state, &mut child_ctx);
}
for child_msg in child_msgs {
let _cmd = C::update(state, child_msg.clone());
self.emit(map_msg(child_msg));
}
}
}