1use crate::app::Application;
4use crate::app::ScheduledRender;
5use crate::app::Dispatcher;
6use crate::app::Commands;
7use crate::app::Update;
8
9use wasm_bindgen::prelude::*;
10use std::rc::Rc;
11use std::cell::RefCell;
12
13pub type Msg = ();
15pub type Cmd = ();
17pub type Key = ();
19
20pub struct App {
22 messages: Rc<RefCell<Vec<Msg>>>,
23 render: Option<ScheduledRender<Cmd>>,
24}
25
26impl App {
27 pub fn dispatcher() -> Dispatcher<Msg, Cmd> {
29 Dispatcher::from(Rc::new(RefCell::new(Box::new(
30 App {
31 messages: Rc::new(RefCell::new(vec![])),
32 render: None,
33 }
34 ) as Box<dyn Application<Msg, Cmd>>)))
35 }
36
37 pub fn dispatcher_with_vec(messages: Rc<RefCell<Vec<Msg>>>) -> Dispatcher<Msg, Cmd> {
39 Dispatcher::from(Rc::new(RefCell::new(Box::new(
40 App {
41 messages: messages,
42 render: None,
43 }
44 ) as Box<dyn Application<Msg, Cmd>>)))
45 }
46}
47
48impl Application<Msg, Cmd> for App {
49 fn update(&mut self, msg: Msg) -> Commands<Cmd> {
50 self.messages.borrow_mut().push(msg);
51 Commands::default()
52 }
53 fn render(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<Cmd> { vec![] }
54 fn process(&self, _cmd: Cmd, _app: &Dispatcher<Msg, Cmd>) { }
55 fn get_scheduled_render(&mut self) -> &mut Option<ScheduledRender<Cmd>> {
56 &mut self.render
57 }
58 fn set_scheduled_render(&mut self, handle: ScheduledRender<Cmd>) {
59 self.render = Some(handle);
60 }
61 fn push_listener(&mut self, _listener: (String, Closure<dyn FnMut(web_sys::Event)>)) { }
62 fn node(&self) -> Option<web_sys::Node> { None }
63 fn nodes(&self) -> Vec<web_sys::Node> { vec![] }
64 fn create(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<web_sys::Node> { vec![] }
65 fn detach(&mut self, _app: &Dispatcher<Msg, Cmd>) { }
66}
67
68pub trait Model<Message, Command> {
70 fn test_update(&mut self, msg: Message) -> Commands<Command>;
77}
78
79impl<Message, Command, M: Update<Message, Command>> Model<Message, Command> for M {
80 fn test_update(&mut self, msg: Message) -> Commands<Command> {
81 let mut cmds = Commands::default();
82 Update::update(self, msg, &mut cmds);
83 cmds
84 }
85}