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
// @TODO remove

use crate::app::MessageMapper;
use std::fmt;

type _HookFn = Box<dyn FnMut(&web_sys::Node)>; // todo

pub(crate) fn fmt_hook_fn<T>(h: &Option<T>) -> &'static str {
    match h {
        Some(_) => "Some(.. a dynamic handler ..)",
        None => "None",
    }
}

pub struct LifecycleHooks<Ms> {
    pub did_mount: Option<DidMount<Ms>>,
    pub did_update: Option<DidUpdate<Ms>>,
    pub will_unmount: Option<WillUnmount<Ms>>,
}

impl<Ms> LifecycleHooks<Ms> {
    pub const fn new() -> Self {
        Self {
            did_mount: None,
            did_update: None,
            will_unmount: None,
        }
    }
}

impl<Ms> fmt::Debug for LifecycleHooks<Ms> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "LifecycleHooks {{ did_mount:{:?}, did_update:{:?}, will_unmount:{} }}",
            fmt_hook_fn(&self.did_mount),
            fmt_hook_fn(&self.did_update),
            fmt_hook_fn(&self.will_unmount)
        )
    }
}

impl<Ms: 'static, OtherMs: 'static> MessageMapper<Ms, OtherMs> for LifecycleHooks<Ms> {
    type SelfWithOtherMs = LifecycleHooks<OtherMs>;
    fn map_msg(self, f: impl FnOnce(Ms) -> OtherMs + 'static + Clone) -> Self::SelfWithOtherMs {
        LifecycleHooks {
            did_mount: self.did_mount.map(|d| DidMount {
                actions: d.actions,
                message: d.message.map(f.clone()),
            }),
            did_update: self.did_update.map(|d| DidUpdate {
                actions: d.actions,
                message: d.message.map(f.clone()),
            }),
            will_unmount: self.will_unmount.map(|d| WillUnmount {
                actions: d.actions,
                message: d.message.map(f),
            }),
        }
    }
}

pub struct DidMount<Ms> {
    pub actions: Box<dyn FnMut(&web_sys::Node)>,
    pub message: Option<Ms>,
}

pub struct DidUpdate<Ms> {
    pub actions: Box<dyn FnMut(&web_sys::Node)>,
    pub message: Option<Ms>,
}

pub struct WillUnmount<Ms> {
    pub actions: Box<dyn FnMut(&web_sys::Node)>,
    pub message: Option<Ms>,
}

/// A constructor for `DidMount`, to be used in the API
pub fn did_mount<Ms>(mut actions: impl FnMut(&web_sys::Node) + 'static) -> DidMount<Ms> {
    let closure = move |el: &web_sys::Node| actions(el);
    DidMount {
        actions: Box::new(closure),
        message: None,
    }
}

/// A constructor for `DidUpdate`, to be used in the API
pub fn did_update<Ms>(mut actions: impl FnMut(&web_sys::Node) + 'static) -> DidUpdate<Ms> {
    let closure = move |el: &web_sys::Node| actions(el);
    DidUpdate {
        actions: Box::new(closure),
        message: None,
    }
}

/// A constructor for `WillUnmount`, to be used in the API
pub fn will_unmount<Ms>(mut actions: impl FnMut(&web_sys::Node) + 'static) -> WillUnmount<Ms> {
    let closure = move |el: &web_sys::Node| actions(el);
    WillUnmount {
        actions: Box::new(closure),
        message: None,
    }
}