use crate::app::Application;
use crate::app::ScheduledRender;
use crate::app::Dispatcher;
use crate::app::Commands;
use crate::app::Update;
use wasm_bindgen::prelude::*;
use std::rc::Rc;
use std::cell::RefCell;
pub type Msg = ();
pub type Cmd = ();
pub type Key = ();
pub struct App {
messages: Rc<RefCell<Vec<Msg>>>,
render: Option<ScheduledRender<Cmd>>,
}
impl App {
pub fn dispatcher() -> Dispatcher<Msg, Cmd> {
Dispatcher::from(Rc::new(RefCell::new(Box::new(
App {
messages: Rc::new(RefCell::new(vec![])),
render: None,
}
) as Box<dyn Application<Msg, Cmd>>)))
}
pub fn dispatcher_with_vec(messages: Rc<RefCell<Vec<Msg>>>) -> Dispatcher<Msg, Cmd> {
Dispatcher::from(Rc::new(RefCell::new(Box::new(
App {
messages: messages,
render: None,
}
) as Box<dyn Application<Msg, Cmd>>)))
}
}
impl Application<Msg, Cmd> for App {
fn update(&mut self, msg: Msg) -> Commands<Cmd> {
self.messages.borrow_mut().push(msg);
Commands::default()
}
fn render(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<Cmd> { vec![] }
fn process(&self, _cmd: Cmd, _app: &Dispatcher<Msg, Cmd>) { }
fn get_scheduled_render(&mut self) -> &mut Option<ScheduledRender<Cmd>> {
&mut self.render
}
fn set_scheduled_render(&mut self, handle: ScheduledRender<Cmd>) {
self.render = Some(handle);
}
fn push_listener(&mut self, _listener: (String, Closure<dyn FnMut(web_sys::Event)>)) { }
fn node(&self) -> Option<web_sys::Node> { None }
fn nodes(&self) -> Vec<web_sys::Node> { vec![] }
fn create(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<web_sys::Node> { vec![] }
fn detach(&mut self, _app: &Dispatcher<Msg, Cmd>) { }
}
pub trait Model<Message, Command> {
fn test_update(&mut self, msg: Message) -> Commands<Command>;
}
impl<Message, Command, M: Update<Message, Command>> Model<Message, Command> for M {
fn test_update(&mut self, msg: Message) -> Commands<Command> {
let mut cmds = Commands::default();
Update::update(self, msg, &mut cmds);
cmds
}
}