pub use maomi_tree as tree;
use tree::*;
use crate::{
error::Error,
node::{DynNodeList, OwnerWeak, SlotChange, SlotKindTrait},
};
pub mod context;
use context::BackendContext;
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum BackendStage {
Normal,
#[cfg(feature = "prerendering")]
Prerendering,
#[cfg(feature = "prerendering-apply")]
PrerenderingApply,
}
pub trait Backend: 'static {
type GeneralElement: BackendGeneralElement<BaseBackend = Self>;
type VirtualElement: BackendVirtualElement<BaseBackend = Self>;
type TextNode: BackendTextNode<BaseBackend = Self>;
fn async_task(fut: impl 'static + std::future::Future<Output = ()>)
where
Self: Sized;
fn backend_stage(&self) -> BackendStage;
fn root(&self) -> ForestNode<Self::GeneralElement>;
fn root_mut(&mut self) -> ForestNodeMut<Self::GeneralElement>;
}
pub trait BackendGeneralElement: 'static {
type BaseBackend: Backend<GeneralElement = Self>;
fn as_virtual_element_mut<'b>(
this: &'b mut ForestNodeMut<Self>,
) -> Option<
ForestValueMut<
'b,
<<Self as BackendGeneralElement>::BaseBackend as Backend>::VirtualElement,
>,
>
where
Self: Sized;
fn as_text_node_mut<'b>(
this: &'b mut ForestNodeMut<Self>,
) -> Option<
ForestValueMut<'b, <<Self as BackendGeneralElement>::BaseBackend as Backend>::TextNode>,
>
where
Self: Sized;
fn create_virtual_element<'b>(
this: &'b mut ForestNodeMut<Self>,
) -> Result<ForestNodeRc<<Self::BaseBackend as Backend>::GeneralElement>, Error>
where
Self: Sized;
fn create_text_node(
this: &mut ForestNodeMut<Self>,
content: &str,
) -> Result<ForestNodeRc<<Self::BaseBackend as Backend>::GeneralElement>, Error>
where
Self: Sized;
fn append<'b>(
this: &'b mut ForestNodeMut<Self>,
child: &'b ForestNodeRc<
<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement,
>,
) where
Self: Sized;
fn insert<'b>(
this: &'b mut ForestNodeMut<Self>,
target: &'b ForestNodeRc<
<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement,
>,
) where
Self: Sized;
fn temp_detach(
this: ForestNodeMut<Self>,
) -> ForestNodeRc<<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement>
where
Self: Sized;
fn detach(
this: ForestNodeMut<Self>,
) -> ForestNodeRc<<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement>
where
Self: Sized;
fn replace_with(
mut this: ForestNodeMut<Self>,
replacer: ForestNodeRc<
<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement,
>,
) -> ForestNodeRc<<<Self as BackendGeneralElement>::BaseBackend as Backend>::GeneralElement>
where
Self: Sized,
{
Self::insert(&mut this, &replacer);
Self::detach(this)
}
}
pub trait BackendVirtualElement {
type BaseBackend: Backend;
}
pub trait BackendTextNode {
type BaseBackend: Backend;
fn set_text(&mut self, content: &str);
}
pub trait BackendComponent<B: Backend> {
type SlotData;
type UpdateTarget;
type UpdateContext;
fn init<'b>(
backend_context: &'b BackendContext<B>,
owner: &'b mut ForestNodeMut<B::GeneralElement>,
owner_weak: &Box<dyn OwnerWeak>,
) -> Result<(Self, ForestNodeRc<B::GeneralElement>), Error>
where
Self: Sized;
fn create<'b>(
&'b mut self,
backend_context: &'b BackendContext<B>,
owner: &'b mut ForestNodeMut<B::GeneralElement>,
update_fn: Box<dyn 'b + FnOnce(&mut Self::UpdateTarget, &mut Self::UpdateContext)>,
slot_fn: &mut dyn FnMut(
&mut tree::ForestNodeMut<B::GeneralElement>,
&ForestToken,
&Self::SlotData,
) -> Result<(), Error>,
) -> Result<(), Error>;
fn apply_updates<'b>(
&'b mut self,
backend_context: &'b BackendContext<B>,
owner: &'b mut ForestNodeMut<B::GeneralElement>,
update_fn: Box<dyn 'b + FnOnce(&mut Self::UpdateTarget, &mut Self::UpdateContext)>,
slot_fn: &mut dyn FnMut(
SlotChange<&mut tree::ForestNodeMut<B::GeneralElement>, &ForestToken, &Self::SlotData>,
) -> Result<(), Error>,
) -> Result<(), Error>;
}
pub trait AsElementTag {
type Target: 'static;
type SlotChildren: SlotKindTrait<ForestTokenAddr, DynNodeList>;
}