wip: making progress on diffing and hydration
This commit is contained in:
1 parent
e5c88fe3a4
commit
49856ccd68
34 files changed
+1023
-750
No files matched your search
+54
-33
@@ -2,38 +2,66 @@
|
||||
//! --------------
|
||||
//! This crate implements a renderer of the Dioxus Virtual DOM for the web browser using Websys.
|
||||
|
||||
pub use crate::cfg::WebConfig;
|
||||
use crate::dom::load_document;
|
||||
use dioxus::prelude::{Context, Properties, VNode};
|
||||
use dioxus::virtual_dom::VirtualDom;
|
||||
pub use dioxus_core as dioxus;
|
||||
use dioxus_core::error::Result;
|
||||
use dioxus_core::{events::EventTrigger, prelude::FC};
|
||||
use futures_util::{pin_mut, Stream, StreamExt};
|
||||
use fxhash::FxHashMap;
|
||||
use web_sys::{window, Document, Element, Event, Node};
|
||||
use js_sys::Iterator;
|
||||
use web_sys::{window, Document, Element, Event, Node, NodeList};
|
||||
|
||||
mod cache;
|
||||
mod new;
|
||||
mod cfg;
|
||||
mod dom;
|
||||
mod nodeslab;
|
||||
|
||||
/// Launches the VirtualDOM from the specified component function.
|
||||
///
|
||||
/// This method will block the thread with `spawn_local`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
///
|
||||
///
|
||||
pub fn launch<F>(root: FC<()>, config: F)
|
||||
where
|
||||
F: FnOnce(()),
|
||||
F: FnOnce(WebConfig) -> WebConfig,
|
||||
{
|
||||
wasm_bindgen_futures::spawn_local(run(root))
|
||||
launch_with_props(root, (), config)
|
||||
}
|
||||
|
||||
/// Launches the VirtualDOM from the specified component function and props.
|
||||
///
|
||||
/// This method will block the thread with `spawn_local`
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
///
|
||||
pub fn launch_with_props<T, F>(root: FC<T>, root_props: T, config: F)
|
||||
where
|
||||
T: Properties + 'static,
|
||||
F: FnOnce(()),
|
||||
F: FnOnce(WebConfig) -> WebConfig,
|
||||
{
|
||||
wasm_bindgen_futures::spawn_local(run_with_props(root, root_props))
|
||||
let config = config(WebConfig::default());
|
||||
let fut = run_with_props(root, root_props, config);
|
||||
|
||||
wasm_bindgen_futures::spawn_local(async {
|
||||
match fut.await {
|
||||
Ok(_) => log::error!("Your app completed running... somehow?"),
|
||||
Err(e) => log::error!("Your app crashed! {}", e),
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// This method is the primary entrypoint for Websys Dioxus apps. Will panic if an error occurs while rendering.
|
||||
/// See DioxusErrors for more information on how these errors could occour.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// fn main() {
|
||||
/// wasm_bindgen_futures::spawn_local(WebsysRenderer::start(Example));
|
||||
@@ -42,29 +70,19 @@ where
|
||||
///
|
||||
/// Run the app to completion, panicing if any error occurs while rendering.
|
||||
/// Pairs well with the wasm_bindgen async handler
|
||||
pub async fn run(root: FC<()>) {
|
||||
run_with_props(root, ()).await;
|
||||
}
|
||||
|
||||
pub async fn run_with_props<T: Properties + 'static>(root: FC<T>, root_props: T) {
|
||||
pub async fn run_with_props<T: Properties + 'static>(
|
||||
root: FC<T>,
|
||||
root_props: T,
|
||||
cfg: WebConfig,
|
||||
) -> Result<()> {
|
||||
let dom = VirtualDom::new_with_props(root, root_props);
|
||||
event_loop(dom).await.expect("Event loop failed");
|
||||
}
|
||||
|
||||
pub async fn event_loop(mut internal_dom: VirtualDom) -> dioxus_core::error::Result<()> {
|
||||
use wasm_bindgen::JsCast;
|
||||
let root_el = load_document().get_element_by_id("dioxus_root").unwrap();
|
||||
let mut websys_dom = dom::WebsysDom::new(root_el, cfg);
|
||||
|
||||
let root = prepare_websys_dom();
|
||||
let root_node = root.clone().dyn_into::<Node>().unwrap();
|
||||
|
||||
let mut websys_dom = crate::new::WebsysDom::new(root.clone());
|
||||
|
||||
websys_dom.stack.push(root_node.clone());
|
||||
websys_dom.stack.push(root_node);
|
||||
|
||||
let mut edits = Vec::new();
|
||||
internal_dom.rebuild(&mut websys_dom, &mut edits)?;
|
||||
websys_dom.process_edits(&mut edits);
|
||||
// let mut edits = Vec::new();
|
||||
// internal_dom.rebuild(&mut websys_dom, &mut edits)?;
|
||||
// websys_dom.process_edits(&mut edits);
|
||||
|
||||
log::info!("Going into event loop");
|
||||
loop {
|
||||
@@ -105,11 +123,14 @@ pub async fn event_loop(mut internal_dom: VirtualDom) -> dioxus_core::error::Res
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepare_websys_dom() -> Element {
|
||||
web_sys::window()
|
||||
.expect("should have access to the Window")
|
||||
.document()
|
||||
.expect("should have access to the Document")
|
||||
.get_element_by_id("dioxusroot")
|
||||
.unwrap()
|
||||
fn iter_node_list() {}
|
||||
|
||||
// struct NodeListIter {
|
||||
// node_list: NodeList,
|
||||
// }
|
||||
// impl Iterator for NodeListIter {}
|
||||
|
||||
struct HydrationNode {
|
||||
id: usize,
|
||||
node: Node,
|
||||
}
|
||||
Reference in new issue
Block a user