Skip to content

Commit

Permalink
Create tracing macros and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 8, 2024
1 parent 3958299 commit d273ad4
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 108 deletions.
94 changes: 42 additions & 52 deletions src/lib.rs
Expand Up @@ -10,7 +10,10 @@

#![cfg_attr(docsrs, feature(doc_cfg))]

use std::sync::Arc;
use std::rc::Rc;

mod rt;
pub use self::rt::Runtime;

pub mod view;
pub use self::view::View;
Expand All @@ -20,12 +23,14 @@ pub use self::view::View;
pub mod web;

pub struct Context<M> {
send: Arc<dyn Fn(M)>,
send: Rc<dyn Fn(M)>,
}

impl<M> Context<M> {
pub fn new(send: Arc<dyn Fn(M)>) -> Self {
Self { send }
pub fn new(send: impl Fn(M) + 'static) -> Self {
Self {
send: Rc::new(send),
}
}

pub fn send(&self, msg: M) {
Expand Down Expand Up @@ -55,56 +60,41 @@ pub trait Model<M> {
fn handle(&mut self, msg: M) -> ControlFlow;
}

/// Runtime for a model and view builder.
pub struct Runtime<T, VB, E, M, S> {
model: T,
view_builder: VB,
element: Option<E>,
cx: Context<M>,
state: S,
#[cfg(feature = "tracing")]
#[cfg_attr(docsrs, doc_cfg(feature = "tracing"))]
#[macro_export]
macro_rules! build_span {
($name:tt) => {
$crate::span!("build", $name)
};
}

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,
view_builder,
element: None,
cx,
state,
}
}

pub fn build<V>(&mut self)
where
T: Model<M>,
VB: FnMut(&T) -> V,
V: View<S, M, Element = E>,
{
let state = (self.view_builder)(&self.model).build(&mut self.cx, &mut self.state);
self.element = Some(state);
}
#[cfg(feature = "tracing")]
#[cfg_attr(docsrs, doc_cfg(feature = "tracing"))]
#[macro_export]
macro_rules! rebuild_span {
($name:tt) => {
$crate::span!("rebuild", $name)
};
}

pub fn rebuild<V>(&mut self)
where
T: Model<M>,
VB: FnMut(&T) -> V,
V: View<S, M, Element = E>,
{
let state = self.element.as_mut().unwrap();
(self.view_builder)(&self.model).rebuild(&mut self.cx, &mut self.state, state);
}
#[cfg(feature = "tracing")]
#[cfg_attr(docsrs, doc_cfg(feature = "tracing"))]
#[macro_export]
macro_rules! remove_span {
($name:tt) => {
$crate::span!("remove", $name)
};
}

pub fn handle(&mut self, msg: M) -> ControlFlow
where
T: Model<M>,
M: 'static,
{
self.model.handle(msg)
}
#[cfg(feature = "tracing")]
#[cfg_attr(docsrs, doc_cfg(feature = "tracing"))]
#[macro_export]
macro_rules! span {
($method:tt, $name:tt) => {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!(concat!("View::", $method), view = $name);
#[cfg(feature = "tracing")]
let _g = span.enter();
};
}
61 changes: 61 additions & 0 deletions src/rt.rs
@@ -0,0 +1,61 @@
use crate::{Context, ControlFlow, Model, View};

/// Runtime for a model and view builder.
pub struct Runtime<T, VB, E, M, S> {
model: T,
view_builder: VB,
element: Option<E>,
cx: Context<M>,
state: S,
}

impl<T, VB, E, M, S> Runtime<T, VB, E, M, S> {
/// Create a new runtime.
///
/// The send function will receive messages from the runtime's [`Context`].
pub fn new(send: impl Fn(M) + 'static, model: T, view_builder: VB, state: S) -> Self
where
M: Send + 'static,
{
let cx = Context::new(send);

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

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

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

/// Send a message to the model.
pub fn handle(&mut self, msg: M) -> ControlFlow
where
T: Model<M>,
M: 'static,
{
self.model.handle(msg)
}
}
12 changes: 3 additions & 9 deletions src/view/lazy.rs
Expand Up @@ -28,19 +28,15 @@ where

fn build(&mut self, cx: &mut crate::Context<M>, tree: &mut T) -> Self::Element {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Build", view = "Lazy");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::build_span!("Lazy");

let element = self.view.build(cx, tree);
(self.hash, element)
}

fn rebuild(&mut self, cx: &mut crate::Context<M>, tree: &mut T, element: &mut Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Rebuild", view = "Lazy");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::rebuild_span!("Lazy");

if self.hash != element.0 {
#[cfg(feature = "tracing")]
Expand All @@ -53,9 +49,7 @@ where

fn remove(&mut self, cx: &mut crate::Context<M>, state: &mut T, element: Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Remove", view = "Lazy");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::remove_span!("Lazy");

self.view.remove(cx, state, element.1);
}
Expand Down
6 changes: 3 additions & 3 deletions src/view/map.rs
Expand Up @@ -19,23 +19,23 @@ where
fn build(&mut self, cx: &mut Context<M1>, tree: &mut T) -> Self::Element {
let f = self.f.clone();
let send = cx.send.clone();
let mut cx = Context::new(Arc::new(move |msg| send(f(msg))));
let mut cx = Context::new(move |msg| send(f(msg)));

self.view.build(&mut cx, tree)
}

fn rebuild(&mut self, cx: &mut Context<M1>, tree: &mut T, element: &mut Self::Element) {
let f = self.f.clone();
let send = cx.send.clone();
let mut cx = Context::new(Arc::new(move |msg| send(f(msg))));
let mut cx = Context::new(move |msg| send(f(msg)));

self.view.rebuild(&mut cx, tree, element)
}

fn remove(&mut self, cx: &mut Context<M1>, state: &mut T, element: Self::Element) {
let f = self.f.clone();
let send = cx.send.clone();
let mut cx = Context::new(Arc::new(move |msg| send(f(msg))));
let mut cx = Context::new(move |msg| send(f(msg)));

self.view.remove(&mut cx, state, element)
}
Expand Down
12 changes: 6 additions & 6 deletions src/view/mod.rs
Expand Up @@ -73,27 +73,27 @@ macro_rules! impl_viewbuilder_for_tuple {

fn build(&mut self, cx: &mut Context<M>, tree: &mut T) -> Self::Element {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Build", view = stringify!(($($t),*)));
let name = stringify!(($($t),*));
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::build_span!(name);

($(self.$idx.build(cx, tree)),*)
}

fn rebuild(&mut self, cx: &mut Context<M>, tree: &mut T, element: &mut Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Rebuild", view = stringify!(($($t),*)));
let name = stringify!(($($t),*));
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::rebuild_span!(name);

$(self.$idx.rebuild(cx, tree, &mut element.$idx));*
}

fn remove(&mut self, cx: &mut Context<M>, tree: &mut T, element: Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Rebuild", view = stringify!(($($t),*)));
let name = stringify!(($($t),*));
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::remove_span!(name);

$(self.$idx.remove(cx, tree, element.$idx));*
}
Expand Down
12 changes: 3 additions & 9 deletions src/view/once.rs
Expand Up @@ -19,24 +19,18 @@ where

fn build(&mut self, cx: &mut Context<M>, tree: &mut T) -> Self::Element {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Build", view = "Once");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::build_span!("Once");

self.view.build(cx, tree)
}

fn rebuild(&mut self, _cx: &mut Context<M>, _tree: &mut T, _element: &mut Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Rebuild", view = "Once");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::rebuild_span!("Once");
}

fn remove(&mut self, _cx: &mut Context<M>, _state: &mut T, _element: Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::trace_span!("View::Remove", view = "Once");
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::remove_span!("Once");
}
}
21 changes: 6 additions & 15 deletions src/web/html.rs
Expand Up @@ -62,13 +62,7 @@ where

fn build(&mut self, cx: &mut Context<M>, tree: &mut Web) -> Self::Element {
#[cfg(feature = "tracing")]
let span = tracing::span!(
tracing::Level::TRACE,
"HTML element",
tag = self.tag.as_ref()
);
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::build_span!("Element");

let element = tree.document.create_element(self.tag.as_ref()).unwrap();
tree.parent.append_child(&element).unwrap();
Expand All @@ -84,18 +78,15 @@ where

fn rebuild(&mut self, cx: &mut Context<M>, _tree: &mut Web, element: &mut Self::Element) {
#[cfg(feature = "tracing")]
let span = tracing::span!(
tracing::Level::TRACE,
"HTML element",
tag = self.tag.as_ref()
);
#[cfg(feature = "tracing")]
let _g = span.enter();
crate::rebuild_span!("Element");

self.attrs.rebuild(cx, &mut element.0, &mut element.1)
}

fn remove(&mut self, _cx: &mut Context<M>, _state: &mut Web, _element: Self::Element) {}
fn remove(&mut self, _cx: &mut Context<M>, _state: &mut Web, _element: Self::Element) {
#[cfg(feature = "tracing")]
crate::remove_span!("Element");
}
}

pub trait EventHandler<Marker, M>: Clone + 'static {
Expand Down

0 comments on commit d273ad4

Please sign in to comment.