use std::marker::PhantomData;
use crate::core::{MessageContext, MessageResult, Mut, View, ViewMarker};
use crate::{DomNode, DomView, ViewCtx};
pub struct AfterBuild<State, Action, E, F> {
element: E,
callback: F,
phantom: PhantomData<fn() -> (State, Action)>,
}
pub struct AfterRebuild<State, Action, E, F> {
element: E,
callback: F,
phantom: PhantomData<fn() -> (State, Action)>,
}
pub struct BeforeTeardown<State, Action, E, F> {
element: E,
callback: F,
phantom: PhantomData<fn() -> (State, Action)>,
}
pub fn after_build<State, Action, E, F>(element: E, callback: F) -> AfterBuild<State, Action, E, F>
where
State: 'static,
Action: 'static,
E: DomView<State, Action> + 'static,
F: Fn(&E::DomNode) + 'static,
{
AfterBuild {
element,
callback,
phantom: PhantomData,
}
}
pub fn after_rebuild<State, Action, E, F>(
element: E,
callback: F,
) -> AfterRebuild<State, Action, E, F>
where
State: 'static,
Action: 'static,
E: DomView<State, Action> + 'static,
F: Fn(&E::DomNode) + 'static,
{
AfterRebuild {
element,
callback,
phantom: PhantomData,
}
}
pub fn before_teardown<State, Action, E, F>(
element: E,
callback: F,
) -> BeforeTeardown<State, Action, E, F>
where
State: 'static,
Action: 'static,
E: DomView<State, Action> + 'static,
F: Fn(&E::DomNode) + 'static,
{
BeforeTeardown {
element,
callback,
phantom: PhantomData,
}
}
impl<State, Action, E, F> ViewMarker for AfterBuild<State, Action, E, F> {}
impl<State, Action, E, F> ViewMarker for AfterRebuild<State, Action, E, F> {}
impl<State, Action, E, F> ViewMarker for BeforeTeardown<State, Action, E, F> {}
impl<State, Action, V, F> View<State, Action, ViewCtx> for AfterBuild<State, Action, V, F>
where
State: 'static,
Action: 'static,
F: Fn(&V::DomNode) + 'static,
V: DomView<State, Action> + 'static,
{
type Element = V::Element;
type ViewState = V::ViewState;
fn build(&self, ctx: &mut ViewCtx, app_state: &mut State) -> (Self::Element, Self::ViewState) {
let (mut el, view_state) = self.element.build(ctx, app_state);
el.node.apply_props(&mut el.props, &mut el.flags);
(self.callback)(&el.node);
(el, view_state)
}
fn rebuild(
&self,
prev: &Self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: Mut<'_, Self::Element>,
app_state: &mut State,
) {
self.element
.rebuild(&prev.element, view_state, ctx, element, app_state);
}
fn teardown(
&self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
el: Mut<'_, Self::Element>,
) {
self.element.teardown(view_state, ctx, el);
}
fn message(
&self,
view_state: &mut Self::ViewState,
message: &mut MessageContext,
element: Mut<'_, Self::Element>,
app_state: &mut State,
) -> MessageResult<Action> {
self.element
.message(view_state, message, element, app_state)
}
}
impl<State, Action, V, F> View<State, Action, ViewCtx> for AfterRebuild<State, Action, V, F>
where
State: 'static,
Action: 'static,
F: Fn(&V::DomNode) + 'static,
V: DomView<State, Action> + 'static,
{
type Element = V::Element;
type ViewState = V::ViewState;
fn build(&self, ctx: &mut ViewCtx, app_state: &mut State) -> (Self::Element, Self::ViewState) {
self.element.build(ctx, app_state)
}
fn rebuild(
&self,
prev: &Self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
mut element: Mut<'_, Self::Element>,
app_state: &mut State,
) {
self.element.rebuild(
&prev.element,
view_state,
ctx,
element.reborrow_mut(),
app_state,
);
element.node.apply_props(element.props, element.flags);
(self.callback)(element.node);
}
fn teardown(
&self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
el: Mut<'_, Self::Element>,
) {
self.element.teardown(view_state, ctx, el);
}
fn message(
&self,
view_state: &mut Self::ViewState,
message: &mut MessageContext,
element: Mut<'_, Self::Element>,
app_state: &mut State,
) -> MessageResult<Action> {
self.element
.message(view_state, message, element, app_state)
}
}
impl<State, Action, V, F> View<State, Action, ViewCtx> for BeforeTeardown<State, Action, V, F>
where
State: 'static,
Action: 'static,
F: Fn(&V::DomNode) + 'static,
V: DomView<State, Action> + 'static,
{
type Element = V::Element;
type ViewState = V::ViewState;
fn build(&self, ctx: &mut ViewCtx, app_state: &mut State) -> (Self::Element, Self::ViewState) {
self.element.build(ctx, app_state)
}
fn rebuild(
&self,
prev: &Self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
element: Mut<'_, Self::Element>,
app_state: &mut State,
) {
self.element
.rebuild(&prev.element, view_state, ctx, element, app_state);
}
fn teardown(
&self,
view_state: &mut Self::ViewState,
ctx: &mut ViewCtx,
el: Mut<'_, Self::Element>,
) {
(self.callback)(el.node);
self.element.teardown(view_state, ctx, el);
}
fn message(
&self,
view_state: &mut Self::ViewState,
message: &mut MessageContext,
element: Mut<'_, Self::Element>,
app_state: &mut State,
) -> MessageResult<Action> {
self.element
.message(view_state, message, element, app_state)
}
}