mod base;
use std::{any::Any, fmt::Debug, rc::Rc};
use base::{AsRespoEffectBase, RespoEffectDynEq};
use cirru_parser::Cirru;
use web_sys::Node;
pub trait RespoEffect
where
Self: Debug + Any + RespoEffectDynEq + AsRespoEffectBase + 'static,
{
#[allow(unused_variables)]
fn run(&self, effect_type: RespoEffectType, el: &Node) -> Result<(), String> {
match effect_type {
RespoEffectType::Mounted => self.mounted(el),
RespoEffectType::BeforeUpdate => self.before_update(el),
RespoEffectType::Updated => self.updated(el),
RespoEffectType::BeforeUnmount => self.before_unmount(el),
}
}
#[allow(unused_variables)]
fn mounted(&self, el: &Node) -> Result<(), String> {
Ok(())
}
#[allow(unused_variables)]
fn before_update(&self, el: &Node) -> Result<(), String> {
Ok(())
}
#[allow(unused_variables)]
fn updated(&self, el: &Node) -> Result<(), String> {
Ok(())
}
#[allow(unused_variables)]
fn before_unmount(&self, el: &Node) -> Result<(), String> {
Ok(())
}
}
#[derive(Debug, Clone)]
pub struct RespoEffectBox(pub Rc<dyn RespoEffect>);
impl PartialEq for RespoEffectBox {
fn eq(&self, other: &Self) -> bool {
let r = self.0.as_ref();
r.do_eq(other.0.as_ref().as_base()) == Some(true)
}
}
impl Eq for RespoEffectBox {}
impl RespoEffectBox {
pub fn new<T>(v: T) -> Self
where
T: RespoEffect + 'static,
{
Self(Rc::new(v))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RespoEffectType {
Mounted,
BeforeUpdate,
Updated,
BeforeUnmount,
}
impl From<RespoEffectType> for Cirru {
fn from(effect_type: RespoEffectType) -> Self {
match effect_type {
RespoEffectType::Mounted => "::mounted".into(),
RespoEffectType::BeforeUpdate => "::before-update".into(),
RespoEffectType::Updated => "::updated".into(),
RespoEffectType::BeforeUnmount => "::before-unmount".into(),
}
}
}