diff --git a/enterprise-compiler/src/visitor.rs b/enterprise-compiler/src/visitor.rs index baae472..08cea3b 100644 --- a/enterprise-compiler/src/visitor.rs +++ b/enterprise-compiler/src/visitor.rs @@ -69,6 +69,43 @@ pub enum DepNode { ModelValue(Symbol), } +impl DepNode { + fn gen_update_code( + &self, + model_bimap: &BiHashMap, + updates: &mut TokenStream, + update_func: &mut TokenStream, + ) { + match self { + DepNode::ModelValue(sym) => { + let sym_name = format_ident!("{}", model_bimap.get_by_left(&sym).unwrap()); + let inner_lock = format_ident!("inner_lock_{}", Symbol::gensym().as_str()); + updates.extend(quote! { + let #inner_lock = self.#sym_name.clone(); + }); + update_func.extend(quote! { + { + let mut locked = #inner_lock.lock(); + *locked = new_value.clone(); + } + }); + } + DepNode::RsxSpan(id) => { + let id_str = id.as_str(); + update_func.extend(quote! { + { + use enterprise::stdweb::web::{INonElementParentNode, INode}; + if let Some(target) = enterprise::stdweb::web::document().get_element_by_id(#id_str) { + target.set_text_content(&new_value.clone()); + } + } + }); + } + _ => (), + } + } +} + type DependencyGraph = DiGraphMap; #[derive(Default, Debug)] @@ -164,39 +201,11 @@ impl Visitor { let mut update_func = TokenStream::new(); while let Some(nx) = dfs.next(&self.deps) { if nx != starting { - match nx { - DepNode::ModelValue(sym) => { - let sym_name = format_ident!( - "{}", - self.model_bimap.get_by_left(&sym).unwrap() - ); - let inner_lock = format_ident!( - "inner_lock_{}", - Symbol::gensym().as_str() - ); - updates.extend(quote! { - let #inner_lock = self.#sym_name.clone(); - }); - update_func.extend(quote! { - { - let mut locked = #inner_lock.lock(); - *locked = new_value.clone(); - } - }); - } - DepNode::RsxSpan(id) => { - let id_str = id.as_str(); - update_func.extend(quote! { - { - use enterprise::stdweb::web::{INonElementParentNode, INode}; - if let Some(target) = enterprise::stdweb::web::document().get_element_by_id(#id_str) { - target.set_text_content(&new_value.clone()); - } - } - }); - } - _ => (), - } + nx.gen_update_code( + &self.model_bimap, + &mut updates, + &mut update_func, + ); } } updates.extend(quote! { diff --git a/examples/helloworld/src/main.rs b/examples/helloworld/src/main.rs index 892f5a8..5f2c265 100644 --- a/examples/helloworld/src/main.rs +++ b/examples/helloworld/src/main.rs @@ -1,21 +1,12 @@ #[macro_use] extern crate enterprise; -// #[macro_use] extern crate stdweb; use std::sync::Arc; use enterprise::{Backend, Component, Web}; -// use stdweb::{ -// unstable::TryFrom, -// web::{ -// document, html_element::InputElement, Element, IElement, IEventTarget, INode, -// INonElementParentNode, -// }, -// }; - example!(); fn main() { diff --git a/static/index.html b/examples/helloworld/static/index.html similarity index 79% rename from static/index.html rename to examples/helloworld/static/index.html index 99ec130..10efc64 100644 --- a/static/index.html +++ b/examples/helloworld/static/index.html @@ -6,6 +6,6 @@
- + \ No newline at end of file