diff --git a/examples/all_css.rs b/examples/all_css.rs index 46774819..5e646a28 100644 --- a/examples/all_css.rs +++ b/examples/all_css.rs @@ -406,6 +406,8 @@ fn app(cx: Scope) -> Element { wrap_through: "a", writing_mode: "a", z_index: "a", + + "This example isn't quite useful yet" } }) } diff --git a/examples/borrowed.rs b/examples/borrowed.rs index 7de2ff80..95641bbb 100644 --- a/examples/borrowed.rs +++ b/examples/borrowed.rs @@ -29,9 +29,7 @@ fn app(cx: Scope) -> Element { cx.render(rsx! { div { - Child1 { - text: first - } + Child1 { text: first } } }) } @@ -59,9 +57,7 @@ struct C2Props<'a> { fn Child2<'a>(cx: Scope<'a, C2Props<'a>>) -> Element { cx.render(rsx! { - Child3 { - text: cx.props.text - } + Child3 { text: cx.props.text } }) } diff --git a/examples/todomvc.rs b/examples/todomvc.rs index f74f5606..b5277133 100644 --- a/examples/todomvc.rs +++ b/examples/todomvc.rs @@ -22,10 +22,10 @@ pub struct TodoItem { } pub fn app(cx: Scope<()>) -> Element { - let todos = use_state(&cx, im_rc::HashMap::::default); - let filter = use_state(&cx, || FilterState::All); - let draft = use_state(&cx, || "".to_string()); - let todo_id = use_state(&cx, || 0); + let todos = use_state(cx, im_rc::HashMap::::default); + let filter = use_state(cx, || FilterState::All); + let draft = use_state(cx, || "".to_string()); + let todo_id = use_state(cx, || 0); // Filter the todos based on the filter state let mut filtered_todos = todos @@ -58,7 +58,6 @@ pub fn app(cx: Scope<()>) -> Element { value: "{draft}", autofocus: "true", oninput: move |evt| { - println!("calling oninput"); draft.set(evt.value.clone()); }, onkeydown: move |evt| { @@ -117,15 +116,13 @@ pub struct TodoEntryProps<'a> { } pub fn TodoEntry<'a>(cx: Scope<'a, TodoEntryProps<'a>>) -> Element { - let is_editing = use_state(&cx, || false); + let is_editing = use_state(cx, || false); let todos = cx.props.todos.get(); let todo = &todos[&cx.props.id]; let completed = if todo.checked { "completed" } else { "" }; let editing = if **is_editing { "editing" } else { "" }; - println!("rendering todo entry"); - cx.render(rsx!{ li { class: "{completed} {editing}", diff --git a/packages/core/src/arena.rs b/packages/core/src/arena.rs index bce159dd..de75195a 100644 --- a/packages/core/src/arena.rs +++ b/packages/core/src/arena.rs @@ -45,8 +45,6 @@ impl VirtualDom { let entry = self.elements.vacant_entry(); let id = entry.key(); - println!("claiming {:?}", id); - entry.insert(ElementRef { template: template as *const _ as *mut _, path, @@ -67,8 +65,6 @@ impl VirtualDom { ); } - println!("Reclaiming {:?}", el.0); - self.elements.try_remove(el.0) } diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index 7b676441..6ede9be4 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -277,7 +277,10 @@ pub enum TemplateAttribute<'a> { /// The attribute in this position is actually determined dynamically at runtime /// /// This is the index into the dynamic_attributes field on the container VNode - Dynamic(usize), + Dynamic { + /// The index + id: usize, + }, } /// An attribute on a DOM node, such as `id="my-thing"` or `href="https://example.com"` diff --git a/packages/desktop/src/controller.rs b/packages/desktop/src/controller.rs index a71c43b4..c97f5acb 100644 --- a/packages/desktop/src/controller.rs +++ b/packages/desktop/src/controller.rs @@ -76,12 +76,8 @@ impl DesktopController { .render_with_deadline(tokio::time::sleep(Duration::from_millis(16))) .await; - { - let mut queue = edit_queue.lock().unwrap(); - queue.push(serde_json::to_string(&muts.templates).unwrap()); - queue.push(serde_json::to_string(&muts.edits).unwrap()); - let _ = proxy.send_event(UserWindowEvent::EditsReady); - } + edit_queue.lock().unwrap().push(serde_json::to_string(&muts).unwrap()); + let _ = proxy.send_event(UserWindowEvent::EditsReady); } }) }); diff --git a/packages/desktop/src/desktop_context.rs b/packages/desktop/src/desktop_context.rs index 71357e0a..e5e184df 100644 --- a/packages/desktop/src/desktop_context.rs +++ b/packages/desktop/src/desktop_context.rs @@ -211,8 +211,6 @@ pub(super) fn handler( }; let window = webview.window(); - println!("user_event: {:?}", user_event); - match user_event { Initialize | EditsReady => desktop.try_load_ready_webviews(), CloseWindow => *control_flow = ControlFlow::Exit, diff --git a/packages/desktop/src/events.rs b/packages/desktop/src/events.rs index 1dba8978..b82d6f74 100644 --- a/packages/desktop/src/events.rs +++ b/packages/desktop/src/events.rs @@ -42,7 +42,6 @@ macro_rules! match_data { ) => { match $name { $( $($mname)|* => { - println!("casting to type {:?}", std::any::TypeId::of::<$tip>()); let val: $tip = from_value::<$tip>($m).ok()?; Rc::new(val) as Rc })* diff --git a/packages/desktop/src/hot_reload.rs b/packages/desktop/src/hot_reload.rs index e3faf3ef..f78df6ca 100644 --- a/packages/desktop/src/hot_reload.rs +++ b/packages/desktop/src/hot_reload.rs @@ -1,19 +1,20 @@ use dioxus_core::VirtualDom; -use interprocess::local_socket::{LocalSocketListener, LocalSocketStream}; +use interprocess::local_socket::LocalSocketStream; +// use interprocess::local_socket::{LocalSocketListener, LocalSocketStream}; use std::io::{BufRead, BufReader}; use std::time::Duration; use std::{sync::Arc, sync::Mutex}; -fn handle_error(connection: std::io::Result) -> Option { +fn _handle_error(connection: std::io::Result) -> Option { connection .map_err(|error| eprintln!("Incoming connection failed: {}", error)) .ok() } -pub(crate) fn init(dom: &VirtualDom) { +pub(crate) fn _init(_dom: &VirtualDom) { let latest_in_connection: Arc>>> = Arc::new(Mutex::new(None)); - let latest_in_connection_handle = latest_in_connection.clone(); + let _latest_in_connection_handle = latest_in_connection.clone(); // connect to processes for incoming data std::thread::spawn(move || { diff --git a/packages/desktop/src/lib.rs b/packages/desktop/src/lib.rs index a729a2f6..e8961184 100644 --- a/packages/desktop/src/lib.rs +++ b/packages/desktop/src/lib.rs @@ -153,7 +153,6 @@ pub fn launch_with_props(root: Component

, props: P, mut cf } "initialize" => { is_ready.store(true, std::sync::atomic::Ordering::Relaxed); - println!("initializing..."); let _ = proxy.send_event(UserWindowEvent::EditsReady); } "browser_open" => { diff --git a/packages/hooks/src/usecontext.rs b/packages/hooks/src/usecontext.rs index a6e581fd..74f7cea9 100644 --- a/packages/hooks/src/usecontext.rs +++ b/packages/hooks/src/usecontext.rs @@ -1,5 +1,4 @@ use dioxus_core::ScopeState; -use std::rc::Rc; /// Consume some context in the tree, providing a sharable handle to the value pub fn use_context(cx: &ScopeState) -> Option<&T> { diff --git a/packages/interpreter/src/bindings.rs b/packages/interpreter/src/bindings.rs index e4ffdb7a..ee7e4f48 100644 --- a/packages/interpreter/src/bindings.rs +++ b/packages/interpreter/src/bindings.rs @@ -12,7 +12,7 @@ extern "C" { pub fn new(arg: Element) -> Interpreter; #[wasm_bindgen(method)] - pub fn SaveTemplate(this: &Interpreter, nodes: Vec, name: &str); + pub fn SaveTemplate(this: &Interpreter, template: JsValue); #[wasm_bindgen(method)] pub fn MountToRoot(this: &Interpreter); @@ -58,8 +58,8 @@ extern "C" { this: &Interpreter, name: &str, id: u32, - handler: &Function, bubbles: bool, + handler: &Function, ); #[wasm_bindgen(method)] diff --git a/packages/interpreter/src/interpreter.js b/packages/interpreter/src/interpreter.js index 1d28cbc6..2c0e939b 100644 --- a/packages/interpreter/src/interpreter.js +++ b/packages/interpreter/src/interpreter.js @@ -137,7 +137,7 @@ export class Interpreter { this.stack.push(el); this.nodes[root] = el; } - NewEventListener(event_name, root, handler, bubbles) { + NewEventListener(event_name, root, bubbles, handler) { const element = this.nodes[root]; element.setAttribute("data-dioxus-id", `${root}`); this.listeners.create(event_name, element, handler, bubbles); @@ -210,7 +210,7 @@ export class Interpreter { } } handleEdits(edits) { - // a json blob of things + for (let template of edits.templates) { this.SaveTemplate(template); } @@ -219,15 +219,15 @@ export class Interpreter { this.handleEdit(edit); } } + SaveTemplate(template) { - console.log("saving template", template); let roots = []; for (let root of template.roots) { roots.push(this.MakeTemplateNode(root)); } - console.log("saving template", template.name, roots); this.templates[template.name] = roots; } + MakeTemplateNode(node) { console.log("making template node", node); switch (node.type) { @@ -340,17 +340,12 @@ export class Interpreter { this.RemoveAttribute(edit.id, edit.name, edit.ns); break; case "RemoveEventListener": - this.RemoveEventListener(edit.id, edit.event_name); + this.RemoveEventListener(edit.id, edit.name); break; case "NewEventListener": - // console.log("creating listener! ", edit); - // this handler is only provided on desktop implementations since this // method is not used by the web implementation let handler = (event) => { - - console.log("event", event); - let target = event.target; if (target != null) { let realId = target.getAttribute(`data-dioxus-id`); @@ -431,17 +426,14 @@ export class Interpreter { } window.ipc.postMessage( serializeIpcMessage("user_event", { - event: edit.event_name, + event: edit.name, mounted_dom_id: parseInt(realId), contents: contents, }) ); } }; - - console.log("adding event listener", edit); - this.NewEventListener(edit.event_name, edit.id, handler, event_bubbles(edit.event_name)); - + this.NewEventListener(edit.name, edit.id, event_bubbles(edit.name), handler); break; } } diff --git a/packages/rsx/src/lib.rs b/packages/rsx/src/lib.rs index ff8d3e78..adb36d59 100644 --- a/packages/rsx/src/lib.rs +++ b/packages/rsx/src/lib.rs @@ -217,7 +217,7 @@ impl<'a> DynamicContext<'a> { let ct = self.dynamic_attributes.len(); self.dynamic_attributes.push(attr); self.attr_paths.push(self.current_path.clone()); - Some(quote! { ::dioxus::core::TemplateAttribute::Dynamic(#ct) }) + Some(quote! { ::dioxus::core::TemplateAttribute::Dynamic { id: #ct } }) } }); diff --git a/packages/ssr/src/cache.rs b/packages/ssr/src/cache.rs index 27b5a72a..e7fc9e4c 100644 --- a/packages/ssr/src/cache.rs +++ b/packages/ssr/src/cache.rs @@ -65,7 +65,7 @@ impl StringCache { TemplateAttribute::Static { name, value, .. } => { write!(chain, " {}=\"{}\"", name, value)?; } - TemplateAttribute::Dynamic(index) => { + TemplateAttribute::Dynamic { id: index } => { chain.segments.push(Segment::Attr(*index)) } } diff --git a/packages/web/Cargo.toml b/packages/web/Cargo.toml index 5be66b63..94e1e861 100644 --- a/packages/web/Cargo.toml +++ b/packages/web/Cargo.toml @@ -30,6 +30,7 @@ futures-util = "0.3.19" smallstr = "0.2.0" futures-channel = "0.3.21" serde_json = { version = "1.0" } +serde-wasm-bindgen = "0.4.5" [dependencies.web-sys] version = "0.3.56" diff --git a/packages/web/src/dom.rs b/packages/web/src/dom.rs index eac87ade..94315886 100644 --- a/packages/web/src/dom.rs +++ b/packages/web/src/dom.rs @@ -53,61 +53,8 @@ impl WebsysDom { log::debug!("Loading templates {:?}", templates); for template in templates { - let mut roots = vec![]; - - for root in template.roots { - roots.push(self.create_template_node(root)) - } - - self.interpreter.SaveTemplate(roots, template.name); - } - } - - fn create_template_node(&self, v: &TemplateNode) -> web_sys::Node { - use TemplateNode::*; - match v { - Element { - tag, - namespace, - attrs, - children, - .. - } => { - let el = match namespace { - Some(ns) => self.document.create_element_ns(Some(ns), tag).unwrap(), - None => self.document.create_element(tag).unwrap(), - }; - for attr in *attrs { - if let TemplateAttribute::Static { - name, - value, - namespace, - } = attr - { - match namespace { - Some(ns) if *ns == "style" => el - .dyn_ref::() - .unwrap() - .style() - .set_property(name, value) - .unwrap(), - Some(ns) => el.set_attribute_ns(Some(ns), name, value).unwrap(), - None => el.set_attribute(name, value).unwrap(), - } - } - } - for child in *children { - el.append_child(&self.create_template_node(child)); - } - el.dyn_into().unwrap() - } - Text { text: t } => self.document.create_text_node(t).dyn_into().unwrap(), - DynamicText { id: _ } => self.document.create_text_node("p").dyn_into().unwrap(), - Dynamic { id: _ } => { - let el = self.document.create_element("pre").unwrap(); - el.toggle_attribute("hidden"); - el.dyn_into().unwrap() - } + self.interpreter + .SaveTemplate(serde_wasm_bindgen::to_value(&template).unwrap()); } }