This commit is contained in:
Michael Zhang 2020-02-11 00:34:13 -06:00
parent cdb7a9b8e3
commit 8b0fdef461
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
3 changed files with 43 additions and 43 deletions

View file

@ -69,6 +69,43 @@ pub enum DepNode {
ModelValue(Symbol), ModelValue(Symbol),
} }
impl DepNode {
fn gen_update_code(
&self,
model_bimap: &BiHashMap<Id, String>,
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<DepNode, ()>; type DependencyGraph = DiGraphMap<DepNode, ()>;
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -164,39 +201,11 @@ impl Visitor {
let mut update_func = TokenStream::new(); let mut update_func = TokenStream::new();
while let Some(nx) = dfs.next(&self.deps) { while let Some(nx) = dfs.next(&self.deps) {
if nx != starting { if nx != starting {
match nx { nx.gen_update_code(
DepNode::ModelValue(sym) => { &self.model_bimap,
let sym_name = format_ident!( &mut updates,
"{}", &mut update_func,
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());
}
}
});
}
_ => (),
}
} }
} }
updates.extend(quote! { updates.extend(quote! {

View file

@ -1,21 +1,12 @@
#[macro_use] #[macro_use]
extern crate enterprise; extern crate enterprise;
// #[macro_use]
extern crate stdweb; extern crate stdweb;
use std::sync::Arc; use std::sync::Arc;
use enterprise::{Backend, Component, Web}; use enterprise::{Backend, Component, Web};
// use stdweb::{
// unstable::TryFrom,
// web::{
// document, html_element::InputElement, Element, IElement, IEventTarget, INode,
// INonElementParentNode,
// },
// };
example!(); example!();
fn main() { fn main() {

View file

@ -6,6 +6,6 @@
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>
<script src="enterprise.js"></script> <script src="helloworld.js"></script>
</body> </body>
</html> </html>