Created
June 2, 2019 23:58
-
-
Save rbalicki2/b125a403cb327738b35d6a6e42a0191f to your computer and use it in GitHub Desktop.
Results of smd! macro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the following two are equivalent: | |
// First | |
let my_app = smd!( | |
on_hash_change={on_hash_change_callback}; | |
post_render={post_render_callback}; | |
// comments can go anywhere | |
{ interpolated_item } | |
<div | |
ref={&mut my_ref} | |
on_click={handle_on_click} | |
> | |
Text can go inside of DOM nodes | |
<hr /> | |
</div> | |
But it can also go outside of DOM nodes. | |
); | |
// Second | |
let mut my_app = { | |
#[allow(dead_code)] | |
use smithy::types::Component; | |
let component: smithy::types::SmithyComponent = | |
smithy::types::SmithyComponent(Box::new(move |phase| match phase { | |
smithy::types::Phase::Rendering => { | |
smithy::types::PhaseResult::Rendering(smithy::types::Node::Vec(vec![ | |
{ interpolated_item }.render(), | |
smithy::types::Node::Dom(smithy::types::HtmlToken { | |
node_type: "div".into(), | |
attributes: std::collections::HashMap::new(), | |
children: { | |
let mut children = Vec::with_capacity(2usize); | |
children.push(smithy::types::Node::Text( | |
"Text can go inside dom nodes".into(), | |
)); | |
children.push(smithy::types::Node::Dom(smithy::types::HtmlToken { | |
node_type: "hr".into(), | |
attributes: std::collections::HashMap::new(), | |
children: vec![], | |
})); | |
children | |
}, | |
}), | |
smithy::types::Node::Text("But it can also go outside of DOM nodes".into()), | |
])) | |
}, | |
smithy::types::Phase::UiEventHandling(ui_event_handling) => match ui_event_handling { | |
(smithy::types::UiEvent::OnClick(val), [1usize]) => { | |
({ handle_on_click })(val); | |
smithy::types::PhaseResult::UiEventHandling(true) | |
}, | |
(evt, [0usize, rest..]) => smithy::types::PhaseResult::UiEventHandling( | |
{ interpolated_item }.handle_ui_event(evt, rest), | |
), | |
_ => smithy::types::PhaseResult::UiEventHandling(false), | |
}, | |
smithy::types::Phase::WindowEventHandling(window_event) => { | |
let mut event_handled = false; | |
event_handled = | |
({ interpolated_item }).handle_window_event(window_event) || event_handled; | |
match window_event { | |
smithy::types::WindowEvent::OnHashChange(val) => { | |
({ on_hash_change_callback })(val); | |
smithy::types::PhaseResult::WindowEventHandling(true) | |
}, | |
_ => smithy::types::PhaseResult::WindowEventHandling(event_handled), | |
} | |
}, | |
smithy::types::Phase::PostRendering => { | |
{ | |
({ interpolated_item }).handle_post_render(); | |
} | |
({ post_render_callback })(); | |
smithy::types::PhaseResult::PostRendering | |
}, | |
smithy::types::Phase::RefAssignment(path_so_far) => { | |
let document = web_sys::window().unwrap().document().unwrap(); | |
for (path, dom_ref) in ({ | |
let dom_refs: Vec<smithy::types::DomRefWithPath> = | |
vec![(vec![1usize], { &mut my_ref })]; | |
dom_refs | |
}) | |
.into_iter() | |
{ | |
use wasm_bindgen::JsCast; | |
let strs = path_so_far | |
.clone() | |
.into_iter() | |
.chain(path) | |
.map(|x| x.to_string()) | |
.collect::<Vec<String>>(); | |
let selector = strs.join(","); | |
let el_opt: Option<web_sys::HtmlElement> = document | |
.query_selector(&format!("[data-smithy-path=\"{}\"]", selector)) | |
.unwrap() | |
.map(JsCast::unchecked_into); | |
*dom_ref = el_opt; | |
} | |
let mut path = vec![0usize].clone(); | |
path.reverse(); | |
let new_path = path_so_far.clone().into_iter().chain(path).collect(); | |
({ interpolated_item }).handle_ref_assignment(new_path); | |
smithy::types::PhaseResult::RefAssignment | |
}, | |
})); | |
component | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment