Feat: a few bugs, but the event system works!

This commit is contained in:
Jonathan Kelley committed 2021-02-28 03:08:08 -05:00
1 parent 2041c88d07
commit 3b30fa61b8
13 files changed
+308 -117

No files matched your search

+33 -10
View File
@@ -59,12 +59,19 @@ impl WebsysRenderer {
}
pub async fn run(&mut self) -> dioxus_core::error::Result<()> {
let (sender, mut receiver) = mpsc::unbounded::<EventTrigger>();
let (mut sender, mut receiver) = mpsc::unbounded::<EventTrigger>();
let body_element = prepare_websys_dom();
let mut patch_machine = interpreter::PatchMachine::new(body_element.clone(), |_| {});
let mut patch_machine = interpreter::PatchMachine::new(body_element.clone(), move |ev| {
log::debug!("Event trigger! {:#?}", ev);
let mut c = sender.clone();
wasm_bindgen_futures::spawn_local(async move {
c.send(ev).await.unwrap();
});
});
let root_node = body_element.first_child().unwrap();
patch_machine.stack.push(root_node);
patch_machine.stack.push(root_node.clone());
// todo: initialize the event registry properly on the root
@@ -72,17 +79,32 @@ impl WebsysRenderer {
log::debug!("patching with {:?}", edit);
patch_machine.handle_edit(edit);
});
log::debug!("patch stack size {:?}", patch_machine.stack);
// Event loop waits for the receiver to finish up
// TODO! Connect the sender to the virtual dom's suspense system
// Suspense is basically an external event that can force renders to specific nodes
while let Some(event) = receiver.next().await {
log::debug!("patch stack size before {:#?}", patch_machine.stack);
// patch_machine.reset();
// patch_machine.stack.push(root_node.clone());
self.internal_dom
.progress_with_event(event)?
.iter()
.for_each(|edit| {
log::debug!("edit stream {:?}", edit);
patch_machine.handle_edit(edit);
});
log::debug!("patch stack size after {:#?}", patch_machine.stack);
patch_machine.reset();
// our root node reference gets invalidated
// not sure why
// for now, just select the first child again.
// eventually, we'll just make our own root element instead of using body
// or just use body directly IDEK
let root_node = body_element.first_child().unwrap();
patch_machine.stack.push(root_node.clone());
}
Ok(()) // should actually never return from this, should be an error, rustc just cant see it
@@ -146,13 +168,14 @@ mod tests {
log::info!("Hello!");
wasm_bindgen_futures::spawn_local(WebsysRenderer::start(|ctx, _| {
ctx.view(html! {
<div>
"Hello world"
<button onclick={move |_| log::info!("button1 clicked!")}> "click me" </button>
<button onclick={move |_| log::info!("button2 clicked!")}> "click me" </button>
</div>
})
todo!()
// ctx.view(html! {
// <div>
// "Hello world"
// <button onclick={move |_| log::info!("button1 clicked!")}> "click me" </button>
// <button onclick={move |_| log::info!("button2 clicked!")}> "click me" </button>
// </div>
// })
}))
}
}