use crate::signals::{BindingId, Runtime, Signal};
use serde_json::{Map, Value};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::rc::Rc;
pub mod expr;
use expr::Expr;
pub mod recording;
pub use recording::{Call, Recording};
#[cfg(feature = "dom")]
pub mod dom;
#[derive(Debug, Clone, PartialEq)]
pub enum Attr {
Lit(String),
Expr(String),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Node {
El { el: String, attrs: BTreeMap<String, Attr>, on: BTreeMap<String, String>, children: Vec<Node> },
Text(Attr),
If { cond: String, then: Vec<Node>, otherwise: Vec<Node> },
Each { expr: String, as_: String, key: String, body: Vec<Node> },
Component { name: String, props: BTreeMap<String, Attr> },
Slot { name: String, props: BTreeMap<String, Attr> },
Html(String),
}
#[derive(Debug, Clone, PartialEq)]
pub struct LiveDecl {
pub snapshot: String,
pub params: BTreeMap<String, Value>,
pub changes: Option<String>,
pub changes_params: BTreeMap<String, Value>,
pub key: String,
pub on: BTreeMap<String, String>,
pub debounce_ms: Option<u64>,
pub when: Option<Value>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ComponentTree {
pub name: String,
pub abi: String,
pub props: Vec<String>,
pub state: Map<String, Value>,
pub live: BTreeMap<String, LiveDecl>,
pub uses: Vec<String>,
pub bus: BTreeMap<String, String>,
pub handlers: String,
pub tree: Vec<Node>,
pub css: Option<String>,
pub keys: Vec<crate::keys::KeyDecl>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum TreeError {
Malformed(String),
Expr { expr: String, message: String },
}
impl std::fmt::Display for TreeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TreeError::Malformed(m) => write!(f, "a malformed tree: {m}"),
TreeError::Expr { expr, message } => write!(f, "the expression `{expr}`: {message}"),
}
}
}
fn bad(at: &str, what: &str) -> TreeError {
TreeError::Malformed(format!("{at}: {what}"))
}
fn attr(v: &Value, at: &str) -> Result<Attr, TreeError> {
match v {
Value::String(s) => Ok(Attr::Lit(s.clone())),
Value::Object(o) => match o.get("expr") {
Some(Value::String(e)) if o.len() == 1 => Ok(Attr::Expr(e.clone())),
_ => Err(bad(at, "a value is a string or { \"expr\": … }")),
},
Value::Number(_) | Value::Bool(_) => Ok(Attr::Lit(v.to_string())),
_ => Err(bad(at, "a value is a string or { \"expr\": … }")),
}
}
fn expr_of(v: Option<&Value>, at: &str) -> Result<String, TreeError> {
match v {
Some(Value::String(s)) => Ok(s.clone()),
Some(Value::Object(o)) => match o.get("expr") {
Some(Value::String(e)) => Ok(e.clone()),
_ => Err(bad(at, "an { \"expr\": … }")),
},
_ => Err(bad(at, "an { \"expr\": … }")),
}
}
fn attrs(v: Option<&Value>, at: &str) -> Result<BTreeMap<String, Attr>, TreeError> {
match v {
None => Ok(BTreeMap::new()),
Some(Value::Object(o)) => o.iter().map(|(k, v)| Ok((k.clone(), attr(v, &format!("{at}.{k}"))?))).collect(),
Some(_) => Err(bad(at, "a map")),
}
}
fn strings(v: Option<&Value>, at: &str) -> Result<BTreeMap<String, String>, TreeError> {
match v {
None => Ok(BTreeMap::new()),
Some(Value::Object(o)) => o
.iter()
.map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())).ok_or_else(|| bad(&format!("{at}.{k}"), "a string")))
.collect(),
Some(_) => Err(bad(at, "a map of strings")),
}
}
fn nodes(v: Option<&Value>, at: &str) -> Result<Vec<Node>, TreeError> {
match v {
None => Ok(Vec::new()),
Some(Value::Array(a)) => a.iter().enumerate().map(|(i, n)| node(n, &format!("{at}[{i}]"))).collect(),
Some(_) => Err(bad(at, "a list of nodes")),
}
}
fn node(v: &Value, at: &str) -> Result<Node, TreeError> {
let o = v.as_object().ok_or_else(|| bad(at, "a node is a map"))?;
if let Some(el) = o.get("el") {
let el = el.as_str().ok_or_else(|| bad(at, "el is a tag"))?;
return Ok(Node::El {
el: el.to_string(),
attrs: attrs(o.get("attrs"), &format!("{at}.attrs"))?,
on: strings(o.get("on"), &format!("{at}.on"))?,
children: nodes(o.get("children"), &format!("{at}.children"))?,
});
}
if let Some(t) = o.get("text") {
return Ok(Node::Text(attr(t, &format!("{at}.text"))?));
}
if o.contains_key("if") {
return Ok(Node::If {
cond: expr_of(o.get("if"), &format!("{at}.if"))?,
then: nodes(o.get("then"), &format!("{at}.then"))?,
otherwise: nodes(o.get("else"), &format!("{at}.else"))?,
});
}
if let Some(each) = o.get("each") {
let e = each.as_object().ok_or_else(|| bad(at, "each is a map"))?;
let s = |k: &str| e.get(k).and_then(Value::as_str).map(str::to_string).ok_or_else(|| bad(&format!("{at}.each"), &format!("each has a {k}")));
return Ok(Node::Each { expr: s("expr")?, as_: s("as")?, key: s("key")?, body: nodes(o.get("body"), &format!("{at}.body"))? });
}
if let Some(c) = o.get("component") {
let name = c.as_str().ok_or_else(|| bad(at, "component is a name"))?;
return Ok(Node::Component { name: name.to_string(), props: attrs(o.get("props"), &format!("{at}.props"))? });
}
if o.contains_key("html") {
return Ok(Node::Html(expr_of(o.get("html"), &format!("{at}.html"))?));
}
if let Some(sl) = o.get("slot") {
let name = sl.as_str().ok_or_else(|| bad(at, "slot is a name"))?;
return Ok(Node::Slot { name: name.to_string(), props: attrs(o.get("props"), &format!("{at}.props"))? });
}
Err(bad(at, "a node is el, text, if, each, component or slot"))
}
fn keys(v: Option<&Value>) -> Result<Vec<crate::keys::KeyDecl>, TreeError> {
let Some(v) = v else { return Ok(Vec::new()) };
let o = v.as_object().ok_or_else(|| bad("keys", "a map of chord → { to, label, in_inputs, only }"))?;
let mut out = Vec::new();
for (chord, e) in o {
let at = format!("keys.{chord}");
let e = e.as_object().ok_or_else(|| bad(&at, "a map"))?;
let to = e.get("to").and_then(Value::as_str).ok_or_else(|| bad(&at, "to is a string"))?;
let label = match e.get("label") {
None => None,
Some(Value::String(l)) => Some(l.clone()),
Some(_) => return Err(bad(&at, "label is a string")),
};
let flag = |k: &str| match e.get(k) {
None => Ok(false),
Some(Value::Bool(b)) => Ok(*b),
Some(_) => Err(bad(&at, &format!("{k} is true or false"))),
};
out.push(crate::keys::KeyDecl::new(chord, to, label, flag("in_inputs")?, flag("only")?).map_err(|m| bad(&at, &m))?.controls(flag("in_controls")?));
}
Ok(out)
}
fn params(v: Option<&Value>, at: &str) -> Result<BTreeMap<String, Value>, TreeError> {
match v {
None | Some(Value::Null) => Ok(BTreeMap::new()),
Some(Value::Object(o)) => Ok(o.iter().map(|(k, v)| (k.clone(), v.clone())).collect()),
Some(_) => Err(bad(at, "params are a map")),
}
}
impl ComponentTree {
pub fn parse(json: &str) -> Result<Self, TreeError> {
let tree = Self::read(json)?;
tree.check()?;
Ok(tree)
}
pub fn read(json: &str) -> Result<Self, TreeError> {
let v: Value = serde_json::from_str(json).map_err(|e| TreeError::Malformed(e.to_string()))?;
let o = v.as_object().ok_or_else(|| bad("the tree", "a map"))?;
let s = |k: &str| o.get(k).and_then(Value::as_str).map(str::to_string).ok_or_else(|| bad("the tree", &format!("{k} is a string")));
let list = |k: &str| -> Result<Vec<String>, TreeError> {
match o.get(k) {
None => Ok(Vec::new()),
Some(Value::Array(a)) => a.iter().map(|x| x.as_str().map(str::to_string).ok_or_else(|| bad(k, "a list of strings"))).collect(),
Some(_) => Err(bad(k, "a list of strings")),
}
};
let mut live = BTreeMap::new();
if let Some(l) = o.get("live") {
for (name, d) in l.as_object().ok_or_else(|| bad("live", "a map"))? {
let at = format!("live.{name}");
let d = d.as_object().ok_or_else(|| bad(&at, "a map"))?;
let ds = |k: &str| d.get(k).and_then(Value::as_str).map(str::to_string).ok_or_else(|| bad(&at, &format!("{k} is a string")));
live.insert(
name.clone(),
LiveDecl {
snapshot: ds("snapshot")?,
params: params(d.get("params"), &at)?,
changes: d.get("changes").and_then(Value::as_str).map(str::to_string),
changes_params: params(d.get("changes_params"), &at)?,
key: ds("key")?,
on: strings(d.get("on"), &format!("{at}.on"))?,
debounce_ms: d.get("debounce_ms").and_then(Value::as_u64),
when: d.get("when").cloned(),
},
);
}
}
let state = match o.get("state") {
None => Map::new(),
Some(Value::Object(m)) => m.clone(),
Some(_) => return Err(bad("state", "a map")),
};
let tree = Self {
name: s("name")?,
abi: s("abi")?,
props: list("props")?,
state,
live,
uses: list("uses")?,
bus: strings(o.get("bus"), "bus")?,
handlers: o.get("handlers").and_then(Value::as_str).unwrap_or_default().to_string(),
tree: nodes(Some(o.get("tree").ok_or_else(|| bad("the tree", "tree is a list"))?), "tree")?,
css: o.get("css").and_then(Value::as_str).map(str::to_string),
keys: keys(o.get("keys"))?,
};
Ok(tree)
}
pub fn check(&self) -> Result<(), TreeError> {
fn walk(nodes: &[Node], out: &mut Vec<String>) {
for n in nodes {
match n {
Node::El { attrs, children, .. } => {
out.extend(attrs.values().filter_map(|a| if let Attr::Expr(e) = a { Some(e.clone()) } else { None }));
walk(children, out);
}
Node::Text(Attr::Expr(e)) => out.push(e.clone()),
Node::Text(_) => {}
Node::If { cond, then, otherwise } => {
out.push(cond.clone());
walk(then, out);
walk(otherwise, out);
}
Node::Each { expr, key, body, .. } => {
out.push(expr.clone());
out.push(key.clone());
walk(body, out);
}
Node::Component { props, .. } | Node::Slot { props, .. } => {
out.extend(props.values().filter_map(|a| if let Attr::Expr(e) = a { Some(e.clone()) } else { None }));
}
Node::Html(e) => out.push(e.clone()),
}
}
}
let mut all = Vec::new();
walk(&self.tree, &mut all);
for e in all {
Expr::compile(&e).map_err(|message| TreeError::Expr { expr: e.clone(), message })?;
}
Ok(())
}
}
pub trait Renderer: Clone + 'static {
type Node: Clone + PartialEq + std::fmt::Debug + 'static;
fn create_element(&self, tag: &str) -> Self::Node;
fn create_text(&self, text: &str) -> Self::Node;
fn set_attr(&self, node: &Self::Node, name: &str, value: &str);
fn remove_attr(&self, node: &Self::Node, name: &str);
fn set_text(&self, node: &Self::Node, text: &str);
fn insert(&self, parent: &Self::Node, child: &Self::Node, before: Option<&Self::Node>);
fn remove(&self, parent: &Self::Node, child: &Self::Node);
fn listen(&self, node: &Self::Node, event: &str, handler: Rc<dyn Fn(Value)>) -> Unlisten;
fn marks(&self, root: &Self::Node) -> Vec<Self::Node>;
fn attrs(&self, node: &Self::Node) -> Vec<(String, String)>;
fn contains(&self, ancestor: &Self::Node, node: &Self::Node) -> bool;
fn observe(&self, root: &Self::Node, f: Rc<dyn Fn(Vec<Self::Node>)>) -> Unlisten;
fn sheet(&self, key: &str, css: &str, on: bool) {
let _ = (key, css, on);
}
fn listen_keys(&self, f: Rc<dyn Fn(&crate::keys::Press) -> bool>) -> Unlisten {
let _ = f;
Box::new(|| {})
}
fn keys_overlay(&self, groups: Option<&crate::keys::KeyGroups>) {
let _ = groups;
}
}
pub type Unlisten = Box<dyn FnOnce()>;
pub trait Children<R: Renderer> {
fn mount_component(&self, at: &R::Node, name: &str, props: Map<String, Value>);
fn mount_slot(&self, at: &R::Node, layout: &str, slot: &str, props: Map<String, Value>);
fn unmount(&self, at: &R::Node);
fn binds(&self, _name: &str) -> bool {
false
}
fn mount_bound(&self, at: &R::Node, _layout: &str, name: &str, props: Map<String, Value>) {
self.mount_component(at, name, props)
}
}
#[derive(Clone, Debug)]
pub struct State {
rt: Runtime,
keys: Rc<RefCell<BTreeMap<String, Signal<Value>>>>,
}
impl State {
pub fn new(rt: &Runtime, initial: &Map<String, Value>) -> Self {
let keys = initial.iter().map(|(k, v)| (k.clone(), rt.signal(v.clone()))).collect();
Self { rt: rt.clone(), keys: Rc::new(RefCell::new(keys)) }
}
pub fn snapshot(&self) -> Map<String, Value> {
self.keys.borrow().iter().map(|(k, s)| (k.clone(), s.peek())).collect()
}
pub fn shape(&self) -> Vec<String> {
self.keys.borrow().keys().cloned().collect()
}
pub fn replace(&self, next: &Map<String, Value>) {
self.rt.batch(|| {
for (k, v) in next {
let existing = self.keys.borrow().get(k).cloned();
match existing {
Some(sig) => sig.set(v.clone()),
None => {
let sig = self.rt.signal(v.clone());
self.keys.borrow_mut().insert(k.clone(), sig);
}
}
}
});
}
fn read(&self, keys: Option<&std::collections::BTreeSet<String>>) -> Map<String, Value> {
let all = self.keys.borrow();
match keys {
None => all.iter().map(|(k, s)| (k.clone(), s.get())).collect(),
Some(keys) => keys.iter().filter_map(|k| all.get(k).map(|s| (k.clone(), s.get()))).collect(),
}
}
}
pub fn eval_when(when: Option<&Value>, props: &Map<String, Value>, state: &State) -> bool {
let Some(w) = when else { return true };
let mut one = BTreeMap::new();
one.insert("when".to_string(), w.clone());
match eval_params(&one, props, state).get("when") {
None | Some(Value::Null) | Some(Value::Bool(false)) => false,
Some(Value::String(s)) => !s.is_empty(),
Some(Value::Number(n)) => n.as_f64() != Some(0.0),
Some(_) => true,
}
}
pub fn eval_params(params: &BTreeMap<String, Value>, props: &Map<String, Value>, state: &State) -> Value {
let mut out = Map::new();
for (k, v) in params {
let text = v.as_object().and_then(|o| o.get("expr")).and_then(Value::as_str);
if let Some(path) = text.and_then(expr::path_of) {
let fields = &path[1..];
let found = if path[0] == "state" {
let keys: Option<std::collections::BTreeSet<String>> = fields.first().map(|k| [k.clone()].into());
Some(expr::walk(&Value::Object(state.read(keys.as_ref())), fields).cloned().unwrap_or(Value::Null))
} else {
props.get(&path[0]).map(|p| expr::walk(p, fields).cloned().unwrap_or(Value::Null))
};
if let Some(found) = found {
out.insert(k.clone(), found);
continue;
}
}
let value = match text {
None => v.clone(),
Some(text) => match Expr::compile(text) {
Err(m) => {
warn(&format!("the param {k}: `{text}`: {m}"));
Value::Null
}
Ok(e) => {
let mut scope = rhai::Scope::new();
for (pk, pv) in props {
if e.reads.names.contains(pk) {
scope.push_constant_dynamic(pk.clone(), expr::to_dynamic(pv));
}
}
if e.reads.names.contains("state") {
scope.push_dynamic("state", expr::to_dynamic(&Value::Object(state.read(e.reads.state_keys.as_ref()))));
}
e.eval(&mut scope).map(|d| expr::to_value(&d)).unwrap_or_else(|m| {
warn(&format!("the param {k}: `{text}`: {m}"));
Value::Null
})
}
},
};
out.insert(k.clone(), value);
}
Value::Object(out)
}
pub struct Scope {
pub props: Map<String, Value>,
pub state: State,
pub live: BTreeMap<String, Signal<Value>>,
pub on_event: Rc<dyn Fn(&str, Value)>,
}
struct Ctx<R: Renderer> {
rt: Runtime,
renderer: R,
layout: String,
scope: Scope,
children: Rc<dyn Children<R>>,
}
type Memo = Rc<RefCell<Memoed>>;
#[derive(Default)]
enum Memoed {
#[default]
Empty,
Seen(std::rc::Weak<dyn std::any::Any>),
Kept(std::rc::Weak<dyn std::any::Any>, rhai::Dynamic),
}
#[derive(Clone)]
struct Var {
name: String,
sig: Signal<Value>,
memo: Memo,
}
type Env = Rc<Vec<Var>>;
fn memoized(sig: &Signal<Value>, memo: &Memo) -> rhai::Dynamic {
let held = sig.shared();
let again = match &*memo.borrow() {
Memoed::Kept(was, d) if std::rc::Weak::ptr_eq(was, &Rc::downgrade(&held)) => return d.clone(),
Memoed::Seen(was) => std::rc::Weak::ptr_eq(was, &Rc::downgrade(&held)),
_ => false,
};
let d = expr::to_dynamic(held.downcast_ref::<Value>().expect("a value"));
let was = Rc::downgrade(&held);
*memo.borrow_mut() = if again { Memoed::Kept(was, d.clone()) } else { Memoed::Seen(was) };
d
}
struct Block<R: Renderer> {
top: Vec<R::Node>,
bindings: Vec<BindingId>,
unlisten: Vec<Unlisten>,
hosts: Vec<R::Node>,
dyns: Vec<Rc<RefCell<Dyn<R>>>>,
}
impl<R: Renderer> Default for Block<R> {
fn default() -> Self {
Self { top: Vec::new(), bindings: Vec::new(), unlisten: Vec::new(), hosts: Vec::new(), dyns: Vec::new() }
}
}
struct Dyn<R: Renderer> {
parent: R::Node,
top: bool,
rows: Vec<(String, Block<R>, Option<Signal<Value>>)>,
}
impl<R: Renderer> Ctx<R> {
fn expr(&self, text: &str) -> Option<Expr> {
match Expr::compile(text) {
Ok(e) => Some(e),
Err(m) => {
warn(&format!("{}: the expression `{text}`: {m}", self.layout));
None
}
}
}
fn eval(&self, env: &Env, text: &str) -> rhai::Dynamic {
self.eval_with(env, None, text)
}
fn eval_with(&self, env: &Env, extra: Option<(&str, &Value)>, text: &str) -> rhai::Dynamic {
let Some(e) = self.expr(text) else { return rhai::Dynamic::UNIT };
if let Some(v) = e.path.as_ref().and_then(|p| self.path_value(env, extra, &e, p)) {
return expr::to_dynamic(&v);
}
let mut scope = rhai::Scope::new();
for (k, v) in &self.scope.props {
if e.reads.names.contains(k) {
scope.push_constant_dynamic(k.clone(), expr::to_dynamic(v));
}
}
if e.reads.names.contains("state") {
let state = self.scope.state.read(e.reads.state_keys.as_ref());
scope.push_dynamic("state", expr::to_dynamic(&Value::Object(state)));
}
for (k, sig) in &self.scope.live {
if e.reads.names.contains(k) {
scope.push_dynamic(k.clone(), sig.with(expr::to_dynamic));
}
}
for v in env.iter() {
if e.reads.names.contains(&v.name) {
scope.push_dynamic(v.name.clone(), memoized(&v.sig, &v.memo));
}
}
if let Some((k, v)) = extra {
scope.push_constant_dynamic(k.to_string(), expr::to_dynamic(v));
}
match e.eval(&mut scope) {
Ok(d) => d,
Err(m) => {
warn(&format!("{}: `{text}`: {m}", self.layout));
rhai::Dynamic::UNIT
}
}
}
fn path_value(&self, env: &Env, extra: Option<(&str, &Value)>, e: &Expr, path: &[String]) -> Option<Value> {
let (name, fields) = (&path[0], &path[1..]);
let null = || Value::Null;
if let Some((k, v)) = extra {
if k == name {
return Some(expr::walk(v, fields).cloned().unwrap_or_else(null));
}
}
if let Some(v) = env.iter().rev().find(|v| &v.name == name) {
return Some(v.sig.with(|row| expr::walk(row, fields).cloned().unwrap_or_else(null)));
}
if let Some(sig) = self.scope.live.get(name) {
return Some(sig.with(|l| expr::walk(l, fields).cloned().unwrap_or_else(null)));
}
if name == "state" {
let state = Value::Object(self.scope.state.read(e.reads.state_keys.as_ref()));
return Some(expr::walk(&state, fields).cloned().unwrap_or_else(null));
}
self.scope.props.get(name).map(|v| expr::walk(v, fields).cloned().unwrap_or_else(null))
}
fn eval_value(&self, env: &Env, extra: Option<(&str, &Value)>, text: &str) -> Value {
if let Some(e) = self.expr(text) {
if let Some(v) = e.path.as_ref().and_then(|p| self.path_value(env, extra, &e, p)) {
return v;
}
}
expr::to_value(&self.eval_with(env, extra, text))
}
fn props(&self, env: &Env, props: &BTreeMap<String, Attr>) -> Map<String, Value> {
self.rt.untracked(|| {
props
.iter()
.map(|(k, a)| {
let v = match a {
Attr::Lit(s) => Value::String(s.clone()),
Attr::Expr(e) => expr::to_value(&self.eval(env, e)),
};
(k.clone(), v)
})
.collect()
})
}
}
fn warn(msg: &str) {
#[cfg(feature = "dom")]
web_sys::console::warn_1(&msg.into());
#[cfg(not(feature = "dom"))]
let _ = msg;
}
fn watch<T: PartialEq + Clone + 'static>(rt: &Runtime, mut compute: impl FnMut() -> T + 'static, mut changed: impl FnMut(T) + 'static) -> (BindingId, T) {
let last: Rc<RefCell<Option<T>>> = Rc::default();
let (l, r) = (last.clone(), rt.clone());
let id = rt.bind(move || {
let v = compute();
let mut held = l.borrow_mut();
match held.as_ref() {
None => *held = Some(v),
Some(old) if *old == v => {}
Some(_) => {
*held = Some(v.clone());
drop(held);
r.untracked(|| changed(v));
}
}
});
let first = last.borrow().clone().expect("a binding runs when made");
(id, first)
}
fn build<R: Renderer>(ctx: &Rc<Ctx<R>>, env: &Env, nodes: &[Node], parent: &R::Node, before: Option<&R::Node>, block: &mut Block<R>, top: bool) {
for n in nodes {
build_node(ctx, env, n, parent, before, block, top);
}
}
fn place<R: Renderer>(ctx: &Ctx<R>, parent: &R::Node, node: &R::Node, before: Option<&R::Node>, block: &mut Block<R>, top: bool) {
ctx.renderer.insert(parent, node, before);
if top {
block.top.push(node.clone());
}
}
fn build_node<R: Renderer>(ctx: &Rc<Ctx<R>>, env: &Env, node: &Node, parent: &R::Node, before: Option<&R::Node>, block: &mut Block<R>, top: bool) {
let r = &ctx.renderer;
match node {
Node::El { el, attrs, on, children } => {
let e = r.create_element(el);
for (name, a) in attrs {
match a {
Attr::Lit(v) => r.set_attr(&e, name, v),
Attr::Expr(x) => {
let (c, env2, x2) = (ctx.clone(), env.clone(), x.clone());
let (c2, e2, name2) = (ctx.clone(), e.clone(), name.clone());
let (id, first) = watch(&ctx.rt, move || expr::as_text(&c.eval(&env2, &x2)), move |v: String| c2.renderer.set_attr(&e2, &name2, &v));
r.set_attr(&e, name, &first);
block.bindings.push(id);
}
}
}
for (event, handler) in on {
let (on_event, handler) = (ctx.scope.on_event.clone(), handler.clone());
block.unlisten.push(r.listen(&e, event, Rc::new(move |payload: Value| on_event(&handler, payload))));
}
build(ctx, env, children, &e, None, block, false);
place(ctx, parent, &e, before, block, top);
}
Node::Text(Attr::Lit(t)) => {
let t = r.create_text(t);
place(ctx, parent, &t, before, block, top);
}
Node::Text(Attr::Expr(x)) => {
let cell: Rc<RefCell<Option<R::Node>>> = Rc::default();
let (c, env2, x2) = (ctx.clone(), env.clone(), x.clone());
let (c2, cell2) = (ctx.clone(), cell.clone());
let (id, first) = watch(&ctx.rt, move || expr::as_text(&c.eval(&env2, &x2)), move |v: String| {
if let Some(n) = cell2.borrow().as_ref() {
c2.renderer.set_text(n, &v);
}
});
let t = r.create_text(&first);
*cell.borrow_mut() = Some(t.clone());
block.bindings.push(id);
place(ctx, parent, &t, before, block, top);
}
Node::If { cond, then, otherwise } => {
let anchor = r.create_text("");
place(ctx, parent, &anchor, before, block, top);
let d = Rc::new(RefCell::new(Dyn { parent: parent.clone(), top, rows: Vec::new() }));
let branches = Rc::new((then.clone(), otherwise.clone()));
let (c, env2, cond2) = (ctx.clone(), env.clone(), cond.clone());
let (c2, env3, d2, a2, b2) = (ctx.clone(), env.clone(), d.clone(), anchor.clone(), branches.clone());
let (id, first) = watch(&ctx.rt, move || expr::truthy(&c.eval(&env2, &cond2)), move |v: bool| {
fill_if(&c2, &env3, &d2, &a2, &b2, v);
});
block.bindings.push(id);
ctx.rt.untracked(|| fill_if(ctx, env, &d, &anchor, &branches, first));
block.dyns.push(d);
}
Node::Each { expr: list, as_, key, body } => {
let anchor = r.create_text("");
place(ctx, parent, &anchor, before, block, top);
let d = Rc::new(RefCell::new(Dyn { parent: parent.clone(), top, rows: Vec::new() }));
let spec = Rc::new((as_.clone(), body.clone()));
let (c, env2, list2, as2, key2) = (ctx.clone(), env.clone(), list.clone(), as_.clone(), key.clone());
let (c2, env3, d2, a2, s2) = (ctx.clone(), env.clone(), d.clone(), anchor.clone(), spec.clone());
let (id, first) = watch(
&ctx.rt,
move || keyed(&c, &env2, &list2, &as2, &key2),
move |rows: Vec<(String, Value)>| reconcile(&c2, &env3, &d2, &a2, &s2, rows),
);
block.bindings.push(id);
ctx.rt.untracked(|| reconcile(ctx, env, &d, &anchor, &spec, first));
block.dyns.push(d);
}
Node::Component { name, props } => {
let props = ctx.props(env, props);
let e = if ctx.children.binds(name) {
let e = r.create_element("div");
r.set_attr(&e, "data-layout", &ctx.layout);
r.set_attr(&e, "data-slot", name);
r.set_attr(&e, "data-component", name);
ctx.children.mount_bound(&e, &ctx.layout, name, props);
e
} else {
let e = r.create_element(name);
ctx.children.mount_component(&e, name, props);
e
};
block.hosts.push(e.clone());
place(ctx, parent, &e, before, block, top);
}
Node::Html(x) => {
let anchor = r.create_text("");
place(ctx, parent, &anchor, before, block, top);
let d = Rc::new(RefCell::new(Dyn { parent: parent.clone(), top, rows: Vec::new() }));
let (c, env2, x2) = (ctx.clone(), env.clone(), x.clone());
let (c2, d2, a2) = (ctx.clone(), d.clone(), anchor.clone());
let (id, first) = watch(&ctx.rt, move || expr::as_text(&c.eval(&env2, &x2)), move |html: String| fill_html(&c2, &d2, &a2, &html));
block.bindings.push(id);
ctx.rt.untracked(|| fill_html(ctx, &d, &anchor, &first));
block.dyns.push(d);
}
Node::Slot { name, props } => {
let e = r.create_element("div");
r.set_attr(&e, "data-layout", &ctx.layout);
r.set_attr(&e, "data-slot", name);
let props = ctx.props(env, props);
ctx.children.mount_slot(&e, &ctx.layout, name, props);
block.hosts.push(e.clone());
place(ctx, parent, &e, before, block, top);
}
}
}
fn fill_html<R: Renderer>(ctx: &Rc<Ctx<R>>, d: &Rc<RefCell<Dyn<R>>>, anchor: &R::Node, html: &str) {
let (old, parent) = {
let mut d = d.borrow_mut();
(std::mem::take(&mut d.rows), d.parent.clone())
};
for (_, b, _) in old {
dispose(ctx, b, Some(&parent));
}
fn draw<R: Renderer>(r: &R, nodes: &[crate::html::HNode], parent: &R::Node, before: Option<&R::Node>, top: &mut Vec<R::Node>) {
for n in nodes {
let node = match n {
crate::html::HNode::Text(t) => r.create_text(t),
crate::html::HNode::Element { tag, attrs, children } => {
let e = r.create_element(tag);
for (k, v) in attrs {
r.set_attr(&e, k, v);
}
draw(r, children, &e, None, &mut Vec::new());
e
}
};
r.insert(parent, &node, before);
top.push(node);
}
}
let mut b = Block::default();
draw(&ctx.renderer, &crate::html::sanitize(html), &parent, Some(anchor), &mut b.top);
d.borrow_mut().rows.push((String::new(), b, None));
}
fn fill_if<R: Renderer>(ctx: &Rc<Ctx<R>>, env: &Env, d: &Rc<RefCell<Dyn<R>>>, anchor: &R::Node, branches: &(Vec<Node>, Vec<Node>), on: bool) {
let (old, parent) = {
let mut d = d.borrow_mut();
(std::mem::take(&mut d.rows), d.parent.clone())
};
for (_, b, _) in old {
dispose(ctx, b, Some(&parent));
}
let nodes = if on { &branches.0 } else { &branches.1 };
if nodes.is_empty() {
return;
}
let mut b = Block::default();
build(ctx, env, nodes, &parent, Some(anchor), &mut b, true);
d.borrow_mut().rows.push((String::new(), b, None));
}
fn keyed<R: Renderer>(ctx: &Ctx<R>, env: &Env, list: &str, as_: &str, key: &str) -> Vec<(String, Value)> {
let items = match ctx.eval_value(env, None, list) {
Value::Array(a) => a,
_ => Vec::new(),
};
ctx.rt.untracked(|| {
let mut seen = std::collections::BTreeSet::new();
items
.into_iter()
.enumerate()
.map(|(i, item)| {
let mut k = match ctx.eval_value(env, Some((as_, &item)), key) {
Value::String(s) => s,
other => expr::as_text(&expr::to_dynamic(&other)),
};
if !seen.insert(k.clone()) {
k = format!("{k}#{i}");
}
(k, item)
})
.collect()
})
}
fn reconcile<R: Renderer>(ctx: &Rc<Ctx<R>>, env: &Env, d: &Rc<RefCell<Dyn<R>>>, anchor: &R::Node, spec: &(String, Vec<Node>), rows: Vec<(String, Value)>) {
let (mut old, parent) = {
let mut d = d.borrow_mut();
(std::mem::take(&mut d.rows), d.parent.clone())
};
let wanted: std::collections::BTreeSet<&String> = rows.iter().map(|(k, _)| k).collect();
let mut kept = Vec::new();
for (k, b, s) in old.drain(..) {
if wanted.contains(&k) {
kept.push((k, b, s));
} else {
dispose(ctx, b, Some(&parent));
}
}
let kept_order: Vec<&String> = kept.iter().map(|(k, _, _)| k).collect();
let new_order: Vec<&String> = rows.iter().map(|(k, _)| k).filter(|k| kept_order.contains(k)).collect();
let moved = kept_order != new_order;
let mut by_key: BTreeMap<String, (Block<R>, Option<Signal<Value>>)> = kept.into_iter().map(|(k, b, s)| (k, (b, s))).collect();
let mut next: Vec<(String, Block<R>, Option<Signal<Value>>)> = Vec::with_capacity(rows.len());
let mut before = anchor.clone();
for (k, item) in rows.into_iter().rev() {
match by_key.remove(&k) {
Some((b, s)) => {
if let Some(s) = &s {
s.set(item);
}
if moved {
for n in &b.top {
ctx.renderer.insert(&parent, n, Some(&before));
}
}
if let Some(first) = b.top.first() {
before = first.clone();
}
next.push((k, b, s));
}
None => {
let sig = ctx.rt.signal(item);
let mut inner = (**env).clone();
inner.push(Var { name: spec.0.clone(), sig: sig.clone(), memo: Memo::default() });
let mut b = Block::default();
build(ctx, &Rc::new(inner), &spec.1, &parent, Some(&before), &mut b, true);
if let Some(first) = b.top.first() {
before = first.clone();
}
next.push((k, b, Some(sig)));
}
}
}
next.reverse();
d.borrow_mut().rows = next;
}
fn dispose<R: Renderer>(ctx: &Ctx<R>, block: Block<R>, parent: Option<&R::Node>) {
for id in block.bindings {
ctx.rt.dispose(id);
}
for off in block.unlisten {
off();
}
for d in block.dyns {
let (rows, dparent, top) = {
let mut d = d.borrow_mut();
(std::mem::take(&mut d.rows), d.parent.clone(), d.top)
};
for (_, b, _) in rows {
dispose(ctx, b, if top && parent.is_some() { Some(&dparent) } else { None });
}
}
for h in &block.hosts {
ctx.children.unmount(h);
}
if let Some(p) = parent {
for n in &block.top {
ctx.renderer.remove(p, n);
}
}
}
pub struct Mounted<R: Renderer> {
pub parent: R::Node,
ctx: Rc<Ctx<R>>,
block: Block<R>,
}
impl<R: Renderer> Mounted<R> {
pub fn unmount(self) {
dispose(&self.ctx, self.block, Some(&self.parent));
}
pub fn top(&self) -> Vec<R::Node> {
self.block.top.clone()
}
}
pub fn mount<R: Renderer>(
rt: &Runtime,
renderer: &R,
tree: &ComponentTree,
scope: Scope,
parent: &R::Node,
children: Rc<dyn Children<R>>,
) -> Result<Mounted<R>, TreeError> {
tree.check()?;
let ctx = Rc::new(Ctx { rt: rt.clone(), renderer: renderer.clone(), layout: tree.name.clone(), scope, children });
let mut block = Block::default();
rt.untracked(|| build(&ctx, &Rc::new(Vec::new()), &tree.tree, parent, None, &mut block, true));
Ok(Mounted { parent: parent.clone(), ctx, block })
}
#[cfg(test)]
mod tests;