duid_core/core/apps/
user_app.rs

1use crate::core::{
2    html::nodes::Node,
3    duid_events::{Cmd, Sub}
4};
5
6#[derive(Clone)]
7pub struct UserApp<MDL, MSG> 
8where
9    MSG: std::fmt::Debug + Clone + 'static
10{
11    model: MDL,
12    view: fn(&MDL) -> Node<MSG>,
13    update: fn(&mut MDL, MSG) -> Cmd<MSG>,
14    subscription: fn(&MDL) -> Sub<MSG>
15}
16
17
18
19impl<MDL, MSG> UserApp<MDL, MSG> 
20where 
21    MSG: std::fmt::Debug + Clone + 'static,
22    MDL: Clone
23{
24
25    pub fn new(
26        model: MDL,
27        view: fn(&MDL) -> Node<MSG>,
28        update: fn(&mut MDL, MSG) -> Cmd<MSG>,
29        subscription: fn(&MDL) -> Sub<MSG>
30    ) -> UserApp<MDL, MSG>  {
31        UserApp {
32            model,
33            view,
34            update,
35            subscription
36        }
37    }
38
39    pub fn render(&self) -> Node<MSG> {
40        (self.view)(&self.model)
41    }
42
43    pub fn update(&mut self, msg: MSG) -> Cmd<MSG> {
44        (self.update)(&mut self.model, msg)
45    }
46
47    pub fn subscription(&self) -> Sub<MSG> {
48        (self.subscription)(&self.model)
49    }
50}
51
52