diff --git a/examples/hello-world/src/App.tsx b/examples/hello-world/src/App.tsx
index 3b79989..ad1bd41 100644
--- a/examples/hello-world/src/App.tsx
+++ b/examples/hello-world/src/App.tsx
@@ -1,37 +1,55 @@
-import {useEffect, useState} from 'react'
-
-function App() {
- const [num, updateNum] = useState(0);
- return (
-
{
- updateNum((num: number) => num + 1);
- }}
- >
-
- {num === 1 ? null : }
-
- );
-}
+import {useState, useEffect} from 'react'
+// function App() {
+// const [num, updateNum] = useState(0)
+// const len = 100
-function Child1({num}: { num: number }) {
- useEffect(() => {
- console.log('child1 create')
- return () => {
- console.log('child1 destroy')
- }
- }, [num]);
- return child1 {num}
;
-}
+// console.log('num', num)
+// return (
+// {
+// updateNum((num: number) => num + 1)
+// }}>
+// {Array(len)
+// .fill(1)
+// .map((_, i) => {
+// return
+// })}
+//
+// )
+// }
+
+// function Child({i}) {
+// return i am child {i}
+// }
+
+// export default App
-function Child2({num}: { num: number }) {
- useEffect(() => {
- console.log('child2 create')
- return () => {
- console.log('child2 destroy')
- }
- }, [num]);
- return child2 {num}
;
+const Item = ({i, children}) => {
+ for (let i = 0; i < 999999; i++) {}
+ return {children}
}
-export default App
+export default () => {
+ const [count, updateCount] = useState(0)
+
+ const onClick = () => {
+ updateCount(2)
+ }
+
+ useEffect(() => {
+ const button = document.querySelector('button')
+ setTimeout(() => updateCount((num) => num + 1), 1000)
+ setTimeout(() => button.click(), 1100)
+ }, [])
+
+ return (
+
+
+
+ {Array.from(new Array(1000)).map((v, index) => (
+ - {count}
+ ))}
+
+
+ )
+}
diff --git a/examples/hello-world/src/main.tsx b/examples/hello-world/src/main.tsx
index 775168b..95cc852 100644
--- a/examples/hello-world/src/main.tsx
+++ b/examples/hello-world/src/main.tsx
@@ -1,21 +1,7 @@
import {createRoot} from 'react-dom'
-import {useEffect} from 'react'
+import App from './App'
const root = createRoot(document.getElementById('root'))
-function Parent() {
- useEffect(() => {
- return () => console.log('Unmount parent')
- })
- return
-}
-
-function Child() {
- useEffect(() => {
- return () => console.log('Unmount child')
- })
- return 'Child'
-}
-
-root.render()
+root.render()
// console.log(root.getChildrenAsJSX())
diff --git a/packages/react-dom/src/lib.rs b/packages/react-dom/src/lib.rs
index 243bd6a..be32ec4 100644
--- a/packages/react-dom/src/lib.rs
+++ b/packages/react-dom/src/lib.rs
@@ -36,6 +36,7 @@ pub fn create_root(container: &JsValue) -> Renderer {
}
};
+ // TODO cache the container
// let mut root;
// unsafe {
// if CONTAINER_TO_ROOT.is_none() {
diff --git a/packages/react-dom/src/renderer.rs b/packages/react-dom/src/renderer.rs
index 9e17638..0004e5b 100644
--- a/packages/react-dom/src/renderer.rs
+++ b/packages/react-dom/src/renderer.rs
@@ -6,6 +6,7 @@ use wasm_bindgen::JsValue;
use react_reconciler::fiber::FiberRootNode;
use react_reconciler::Reconciler;
+use web_sys::Element;
use crate::synthetic_event::init_event;
@@ -28,6 +29,15 @@ impl Renderer {
container: container.clone(),
}
}
+
+ // fn clear_container_dom(&self) {
+ // let ele = self.container.dyn_ref::().unwrap();
+ // if !ele.has_child_nodes() {
+ // return;
+ // }
+
+ // ele.child_nodes
+ // }
}
#[wasm_bindgen]
@@ -37,4 +47,9 @@ impl Renderer {
self.reconciler
.update_container(element.clone(), self.root.clone())
}
+
+ pub fn unmount(&self) -> JsValue {
+ self.reconciler
+ .update_container(JsValue::null(), self.root.clone())
+ }
}
diff --git a/packages/react-dom/src/synthetic_event.rs b/packages/react-dom/src/synthetic_event.rs
index b0854d0..dd89207 100644
--- a/packages/react-dom/src/synthetic_event.rs
+++ b/packages/react-dom/src/synthetic_event.rs
@@ -1,9 +1,11 @@
use gloo::events::EventListener;
-use wasm_bindgen::{JsCast, JsValue};
+use scheduler::{unstable_cancel_callback, unstable_run_with_priority, Priority};
use wasm_bindgen::closure::Closure;
-use web_sys::{Element, Event};
+use wasm_bindgen::{JsCast, JsValue};
use web_sys::js_sys::{Function, Object, Reflect};
+use web_sys::{Element, Event};
+use react_reconciler::fiber_lanes::{lanes_to_scheduler_priority, Lane};
use shared::{derive_from_js_value, is_dev, log};
static VALID_EVENT_TYPE_LIST: [&str; 1] = ["click"];
@@ -23,8 +25,18 @@ impl Paths {
}
}
+fn event_type_to_event_priority(event_type: &str) -> Priority {
+ let lane = match event_type {
+ "click" | "keydown" | "keyup" => Lane::SyncLane,
+ "scroll" => Lane::InputContinuousLane,
+ _ => Lane::DefaultLane,
+ };
+ lanes_to_scheduler_priority(lane)
+}
+
fn create_synthetic_event(e: Event) -> Event {
- Reflect::set(&*e, &"__stopPropagation".into(), &JsValue::from_bool(false)).expect("TODO: panic set __stopPropagation");
+ Reflect::set(&*e, &"__stopPropagation".into(), &JsValue::from_bool(false))
+ .expect("TODO: panic set __stopPropagation");
let e_cloned = e.clone();
let origin_stop_propagation = derive_from_js_value(&*e, "stopPropagation");
@@ -33,21 +45,31 @@ fn create_synthetic_event(e: Event) -> Event {
&*e_cloned,
&"__stopPropagation".into(),
&JsValue::from_bool(true),
- ).expect("TODO: panic __stopPropagation");
+ )
+ .expect("TODO: panic __stopPropagation");
if origin_stop_propagation.is_function() {
let origin_stop_propagation = origin_stop_propagation.dyn_ref::().unwrap();
- origin_stop_propagation.call0(&JsValue::null()).expect("TODO: panic origin_stop_propagation");
+ origin_stop_propagation
+ .call0(&JsValue::null())
+ .expect("TODO: panic origin_stop_propagation");
}
}) as Box);
let function = closure.as_ref().unchecked_ref::().clone();
closure.forget();
- Reflect::set(&*e.clone(), &"stopPropagation".into(), &function.into()).expect("TODO: panic set stopPropagation");
+ Reflect::set(&*e.clone(), &"stopPropagation".into(), &function.into())
+ .expect("TODO: panic set stopPropagation");
e
}
fn trigger_event_flow(paths: Vec, se: &Event) {
for callback in paths {
- callback.call1(&JsValue::null(), se).expect("TODO: panic call callback");
+ unstable_run_with_priority(
+ event_type_to_event_priority(se.type_().as_str()),
+ &callback.bind1(&JsValue::null(), se),
+ );
+ // callback
+ // .call1(&JsValue::null(), se)
+ // .expect("TODO: panic call callback");
if derive_from_js_value(se, "__stopPropagation")
.as_bool()
.unwrap()
@@ -158,16 +180,18 @@ pub fn update_fiber_props(node: Element, props: &JsValue) -> Element {
for callback_name in callback_name_list.clone().unwrap() {
if props.is_object()
&& props
- .dyn_ref::