Skip to content

Commit

Permalink
Rename Application to Runtime and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 8, 2024
1 parent 3e9686d commit 9f611ab
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
39 changes: 19 additions & 20 deletions src/lib.rs
Expand Up @@ -36,49 +36,48 @@ pub trait Model<M> {
fn handle(&mut self, msg: M) -> ControlFlow;
}

pub struct Application<T, F, S, M, Tree> {
pub struct Runtime<T, VB, E, M, S> {
model: T,
composable: F,
state: Option<S>,
view_builder: VB,
element: Option<E>,
cx: Context<M>,
tree: Tree,
state: S,
}

impl<T, F, S, M, Tree> Application<T, F, S, M, Tree> {
pub fn new(send: Arc<dyn Fn(M)>, model: T, composable: F, tree: Tree) -> Self
impl<T, VB, E, M, S> Runtime<T, VB, E, M, S> {
pub fn new(send: Arc<dyn Fn(M)>, model: T, view_builder: VB, state: S) -> Self
where
M: Send + 'static,
{
let cx = Context::new(send);

Self {
model,
composable,
state: None,
view_builder,
element: None,
cx,

tree,
state,
}
}

pub fn build<C>(&mut self)
pub fn build<V>(&mut self)
where
T: Model<M>,
F: FnMut(&T) -> C,
C: View<Tree, M, Element = S>,
VB: FnMut(&T) -> V,
V: View<S, M, Element = E>,
{
let state = (self.composable)(&self.model).build(&mut self.cx, &mut self.tree);
self.state = Some(state);
let state = (self.view_builder)(&self.model).build(&mut self.cx, &mut self.state);
self.element = Some(state);
}

pub fn rebuild<C>(&mut self)
pub fn rebuild<V>(&mut self)
where
T: Model<M>,
F: FnMut(&T) -> C,
C: View<Tree, M, Element = S>,
VB: FnMut(&T) -> V,
V: View<S, M, Element = E>,
{
let state = self.state.as_mut().unwrap();
(self.composable)(&self.model).rebuild(&mut self.cx, &mut self.tree, state);
let state = self.element.as_mut().unwrap();
(self.view_builder)(&self.model).rebuild(&mut self.cx, &mut self.state, state);
}

pub fn handle(&mut self, msg: M) -> ControlFlow
Expand Down
4 changes: 2 additions & 2 deletions src/view/mod.rs
Expand Up @@ -16,9 +16,9 @@ pub use self::once::{once, Once};
pub trait View<T, M> {
type Element;

fn build(&mut self, cx: &mut Context<M>, tree: &mut T) -> Self::Element;
fn build(&mut self, cx: &mut Context<M>, state: &mut T) -> Self::Element;

fn rebuild(&mut self, cx: &mut Context<M>, tree: &mut T, element: &mut Self::Element);
fn rebuild(&mut self, cx: &mut Context<M>, state: &mut T, element: &mut Self::Element);

fn map<F, M1>(self, f: F) -> Map<Self, F, M>
where
Expand Down
6 changes: 3 additions & 3 deletions src/web/mod.rs
@@ -1,4 +1,4 @@
use crate::{Application, Context, ControlFlow, Model, View};
use crate::{Runtime, Context, ControlFlow, Model, View};
use std::{cell::RefCell, rc::Rc, sync::Arc};
use web_sys::{wasm_bindgen::JsCast, Document, Element, Text};

Expand All @@ -13,9 +13,9 @@ where

M: Send + 'static,
{
let cell = Rc::new(RefCell::new(None::<Application<_, _, _, _, _>>));
let cell = Rc::new(RefCell::new(None::<Runtime<_, _, _, _, _>>));
let cell_clone = cell.clone();
let mut app = Application::new(
let mut app = Runtime::new(
Arc::new(move |msg| {
let mut g = cell_clone.borrow_mut();
let app = g.as_mut().unwrap();
Expand Down

0 comments on commit 9f611ab

Please sign in to comment.