virtual_dom.rs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. //! # Virtual DOM Implementation for Rust
  2. //!
  3. //! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
  4. use crate::innerlude::Work;
  5. use crate::properties::RootProps;
  6. use crate::root_wrapper::RootScopeWrapper;
  7. use crate::{
  8. arena::ElementId,
  9. innerlude::{
  10. ElementRef, NoOpMutations, SchedulerMsg, ScopeOrder, ScopeState, VNodeMount, VProps,
  11. WriteMutations,
  12. },
  13. nodes::{Template, TemplateId},
  14. runtime::{Runtime, RuntimeGuard},
  15. scopes::ScopeId,
  16. AttributeValue, ComponentFunction, Element, Event, Mutations,
  17. };
  18. use crate::{Task, VComponent};
  19. use futures_util::StreamExt;
  20. use rustc_hash::FxHashMap;
  21. use slab::Slab;
  22. use std::collections::BTreeSet;
  23. use std::{any::Any, rc::Rc};
  24. use tracing::instrument;
  25. /// A virtual node system that progresses user events and diffs UI trees.
  26. ///
  27. /// ## Guide
  28. ///
  29. /// Components are defined as simple functions that take [`crate::properties::Properties`] and return an [`Element`].
  30. ///
  31. /// ```rust
  32. /// # use dioxus::prelude::*;
  33. ///
  34. /// #[derive(Props, PartialEq, Clone)]
  35. /// struct AppProps {
  36. /// title: String
  37. /// }
  38. ///
  39. /// fn app(cx: AppProps) -> Element {
  40. /// rsx!(
  41. /// div {"hello, {cx.title}"}
  42. /// )
  43. /// }
  44. /// ```
  45. ///
  46. /// Components may be composed to make complex apps.
  47. ///
  48. /// ```rust
  49. /// # #![allow(unused)]
  50. /// # use dioxus::prelude::*;
  51. ///
  52. /// # #[derive(Props, PartialEq, Clone)]
  53. /// # struct AppProps {
  54. /// # title: String
  55. /// # }
  56. ///
  57. /// static ROUTES: &str = "";
  58. ///
  59. /// #[component]
  60. /// fn app(cx: AppProps) -> Element {
  61. /// rsx!(
  62. /// NavBar { routes: ROUTES }
  63. /// Title { "{cx.title}" }
  64. /// Footer {}
  65. /// )
  66. /// }
  67. ///
  68. /// #[component]
  69. /// fn NavBar( routes: &'static str) -> Element {
  70. /// rsx! {
  71. /// div { "Routes: {routes}" }
  72. /// }
  73. /// }
  74. ///
  75. /// #[component]
  76. /// fn Footer() -> Element {
  77. /// rsx! { div { "Footer" } }
  78. /// }
  79. ///
  80. /// #[component]
  81. /// fn Title( children: Element) -> Element {
  82. /// rsx! {
  83. /// div { id: "title", {children} }
  84. /// }
  85. /// }
  86. /// ```
  87. ///
  88. /// To start an app, create a [`VirtualDom`] and call [`VirtualDom::rebuild`] to get the list of edits required to
  89. /// draw the UI.
  90. ///
  91. /// ```rust
  92. /// # use dioxus::prelude::*;
  93. /// # use dioxus_core::*;
  94. /// # fn app() -> Element { rsx! { div {} } }
  95. ///
  96. /// let mut vdom = VirtualDom::new(app);
  97. /// let edits = vdom.rebuild_to_vec();
  98. /// ```
  99. ///
  100. /// To call listeners inside the VirtualDom, call [`VirtualDom::handle_event`] with the appropriate event data.
  101. ///
  102. /// ```rust, no_run
  103. /// # use dioxus::prelude::*;
  104. /// # use dioxus_core::*;
  105. /// # fn app() -> Element { rsx! { div {} } }
  106. /// # let mut vdom = VirtualDom::new(app);
  107. /// let event = std::rc::Rc::new(0);
  108. /// vdom.handle_event("onclick", event, ElementId(0), true);
  109. /// ```
  110. ///
  111. /// While no events are ready, call [`VirtualDom::wait_for_work`] to poll any futures inside the VirtualDom.
  112. ///
  113. /// ```rust, no_run
  114. /// # use dioxus::prelude::*;
  115. /// # use dioxus_core::*;
  116. /// # fn app() -> Element { rsx! { div {} } }
  117. /// # let mut vdom = VirtualDom::new(app);
  118. /// tokio::runtime::Runtime::new().unwrap().block_on(async {
  119. /// vdom.wait_for_work().await;
  120. /// });
  121. /// ```
  122. ///
  123. /// Once work is ready, call [`VirtualDom::render_immediate`] to compute the differences between the previous and
  124. /// current UI trees. This will write edits to a [`WriteMutations`] object you pass in that contains with edits that need to be
  125. /// handled by the renderer.
  126. ///
  127. /// ```rust, no_run
  128. /// # use dioxus::prelude::*;
  129. /// # use dioxus_core::*;
  130. /// # fn app() -> Element { rsx! { div {} } }
  131. /// # let mut vdom = VirtualDom::new(app);
  132. /// let mut mutations = Mutations::default();
  133. ///
  134. /// vdom.render_immediate(&mut mutations);
  135. /// ```
  136. ///
  137. /// To not wait for suspense while diffing the VirtualDom, call [`VirtualDom::render_immediate`].
  138. ///
  139. ///
  140. /// ## Building an event loop around Dioxus:
  141. ///
  142. /// Putting everything together, you can build an event loop around Dioxus by using the methods outlined above.
  143. /// ```rust, no_run
  144. /// # use dioxus::prelude::*;
  145. /// # use dioxus_core::*;
  146. /// # struct RealDom;
  147. /// # struct Event {}
  148. /// # impl RealDom {
  149. /// # fn new() -> Self {
  150. /// # Self {}
  151. /// # }
  152. /// # fn apply(&mut self) -> Mutations {
  153. /// # unimplemented!()
  154. /// # }
  155. /// # async fn wait_for_event(&mut self) -> std::rc::Rc<dyn std::any::Any> {
  156. /// # unimplemented!()
  157. /// # }
  158. /// # }
  159. /// #
  160. /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
  161. /// let mut real_dom = RealDom::new();
  162. ///
  163. /// #[component]
  164. /// fn app() -> Element {
  165. /// rsx! {
  166. /// div { "Hello World" }
  167. /// }
  168. /// }
  169. ///
  170. /// let mut dom = VirtualDom::new(app);
  171. ///
  172. /// dom.rebuild(&mut real_dom.apply());
  173. ///
  174. /// loop {
  175. /// tokio::select! {
  176. /// _ = dom.wait_for_work() => {}
  177. /// evt = real_dom.wait_for_event() => dom.handle_event("onclick", evt, ElementId(0), true),
  178. /// }
  179. ///
  180. /// dom.render_immediate(&mut real_dom.apply());
  181. /// }
  182. /// # });
  183. /// ```
  184. ///
  185. /// ## Waiting for suspense
  186. ///
  187. /// Because Dioxus supports suspense, you can use it for server-side rendering, static site generation, and other use cases
  188. /// where waiting on portions of the UI to finish rendering is important. To wait for suspense, use the
  189. /// [`VirtualDom::wait_for_suspense`] method:
  190. ///
  191. /// ```rust, no_run
  192. /// # use dioxus::prelude::*;
  193. /// # use dioxus_core::*;
  194. /// # fn app() -> Element { rsx! { div {} } }
  195. /// tokio::runtime::Runtime::new().unwrap().block_on(async {
  196. /// let mut dom = VirtualDom::new(app);
  197. ///
  198. /// dom.rebuild_in_place();
  199. /// dom.wait_for_suspense().await;
  200. /// });
  201. ///
  202. /// // Render the virtual dom
  203. /// ```
  204. pub struct VirtualDom {
  205. pub(crate) scopes: Slab<ScopeState>,
  206. pub(crate) dirty_scopes: BTreeSet<ScopeOrder>,
  207. // A map of overridden templates?
  208. pub(crate) templates: FxHashMap<TemplateId, Template>,
  209. // Templates changes that are queued for the next render
  210. pub(crate) queued_templates: Vec<Template>,
  211. // The element ids that are used in the renderer
  212. // These mark a specific place in a whole rsx block
  213. pub(crate) elements: Slab<Option<ElementRef>>,
  214. // Once nodes are mounted, the information about where they are mounted is stored here
  215. // We need to store this information on the virtual dom so that we know what nodes are mounted where when we bubble events
  216. // Each mount is associated with a whole rsx block. [`VirtualDom::elements`] link to a specific node in the block
  217. pub(crate) mounts: Slab<VNodeMount>,
  218. pub(crate) runtime: Rc<Runtime>,
  219. // The scopes that have been resolved since the last render
  220. pub(crate) resolved_scopes: Vec<ScopeId>,
  221. rx: futures_channel::mpsc::UnboundedReceiver<SchedulerMsg>,
  222. }
  223. impl VirtualDom {
  224. /// Create a new VirtualDom with a component that does not have special props.
  225. ///
  226. /// # Description
  227. ///
  228. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  229. ///
  230. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  231. /// to toss out the entire tree.
  232. ///
  233. ///
  234. /// # Example
  235. /// ```rust, no_run
  236. /// # use dioxus::prelude::*;
  237. /// # use dioxus_core::*;
  238. /// fn Example() -> Element {
  239. /// rsx!( div { "hello world" } )
  240. /// }
  241. ///
  242. /// let dom = VirtualDom::new(Example);
  243. /// ```
  244. ///
  245. /// Note: the VirtualDom is not progressed, you must either "run_with_deadline" or use "rebuild" to progress it.
  246. pub fn new(app: fn() -> Element) -> Self {
  247. Self::new_with_props(
  248. move || {
  249. use warnings::Warning;
  250. // The root props don't come from a vcomponent so we need to manually rerun them sometimes
  251. crate::properties::component_called_as_function::allow(app)
  252. },
  253. (),
  254. )
  255. }
  256. /// Create a new VirtualDom with the given properties for the root component.
  257. ///
  258. /// # Description
  259. ///
  260. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  261. ///
  262. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  263. /// to toss out the entire tree.
  264. ///
  265. ///
  266. /// # Example
  267. /// ```rust, no_run
  268. /// # use dioxus::prelude::*;
  269. /// # use dioxus_core::*;
  270. /// #[derive(PartialEq, Props, Clone)]
  271. /// struct SomeProps {
  272. /// name: &'static str
  273. /// }
  274. ///
  275. /// fn Example(cx: SomeProps) -> Element {
  276. /// rsx!{ div { "hello {cx.name}" } }
  277. /// }
  278. ///
  279. /// let dom = VirtualDom::new_with_props(Example, SomeProps { name: "world" });
  280. /// ```
  281. ///
  282. /// Note: the VirtualDom is not progressed on creation. You must either "run_with_deadline" or use "rebuild" to progress it.
  283. ///
  284. /// ```rust, no_run
  285. /// # use dioxus::prelude::*;
  286. /// # use dioxus_core::*;
  287. /// # #[derive(PartialEq, Props, Clone)]
  288. /// # struct SomeProps {
  289. /// # name: &'static str
  290. /// # }
  291. /// # fn Example(cx: SomeProps) -> Element {
  292. /// # rsx!{ div { "hello {cx.name}" } }
  293. /// # }
  294. /// let mut dom = VirtualDom::new_with_props(Example, SomeProps { name: "jane" });
  295. /// dom.rebuild_in_place();
  296. /// ```
  297. pub fn new_with_props<P: Clone + 'static, M: 'static>(
  298. root: impl ComponentFunction<P, M>,
  299. root_props: P,
  300. ) -> Self {
  301. let render_fn = root.id();
  302. let props = VProps::new(root, |_, _| true, root_props, "Root");
  303. Self::new_with_component(VComponent {
  304. name: "root",
  305. render_fn,
  306. props: Box::new(props),
  307. })
  308. }
  309. /// Create a new virtualdom and build it immediately
  310. pub fn prebuilt(app: fn() -> Element) -> Self {
  311. let mut dom = Self::new(app);
  312. dom.rebuild_in_place();
  313. dom
  314. }
  315. /// Create a new VirtualDom from something that implements [`AnyProps`]
  316. #[instrument(skip(root), level = "trace", name = "VirtualDom::new")]
  317. pub(crate) fn new_with_component(root: VComponent) -> Self {
  318. let (tx, rx) = futures_channel::mpsc::unbounded();
  319. let mut dom = Self {
  320. rx,
  321. runtime: Runtime::new(tx),
  322. scopes: Default::default(),
  323. dirty_scopes: Default::default(),
  324. templates: Default::default(),
  325. queued_templates: Default::default(),
  326. elements: Default::default(),
  327. mounts: Default::default(),
  328. resolved_scopes: Default::default(),
  329. };
  330. let root = VProps::new(
  331. RootScopeWrapper,
  332. |_, _| true,
  333. RootProps(root),
  334. "RootWrapper",
  335. );
  336. dom.new_scope(Box::new(root), "app");
  337. // the root element is always given element ID 0 since it's the container for the entire tree
  338. dom.elements.insert(None);
  339. dom
  340. }
  341. /// Get the state for any scope given its ID
  342. ///
  343. /// This is useful for inserting or removing contexts from a scope, or rendering out its root node
  344. pub fn get_scope(&self, id: ScopeId) -> Option<&ScopeState> {
  345. self.scopes.get(id.0)
  346. }
  347. /// Get the single scope at the top of the VirtualDom tree that will always be around
  348. ///
  349. /// This scope has a ScopeId of 0 and is the root of the tree
  350. pub fn base_scope(&self) -> &ScopeState {
  351. self.get_scope(ScopeId::ROOT).unwrap()
  352. }
  353. /// Run a closure inside the dioxus runtime
  354. #[instrument(skip(self, f), level = "trace", name = "VirtualDom::in_runtime")]
  355. pub fn in_runtime<O>(&self, f: impl FnOnce() -> O) -> O {
  356. let _runtime = RuntimeGuard::new(self.runtime.clone());
  357. f()
  358. }
  359. /// Build the virtualdom with a global context inserted into the base scope
  360. ///
  361. /// This is useful for what is essentially dependency injection when building the app
  362. pub fn with_root_context<T: Clone + 'static>(self, context: T) -> Self {
  363. self.base_scope().state().provide_context(context);
  364. self
  365. }
  366. /// Provide a context to the root scope
  367. pub fn provide_root_context<T: Clone + 'static>(&self, context: T) {
  368. self.base_scope().state().provide_context(context);
  369. }
  370. /// Build the virtualdom with a global context inserted into the base scope
  371. ///
  372. /// This method is useful for when you want to provide a context in your app without knowing its type
  373. pub fn insert_any_root_context(&mut self, context: Box<dyn Any>) {
  374. self.base_scope().state().provide_any_context(context);
  375. }
  376. /// Manually mark a scope as requiring a re-render
  377. ///
  378. /// Whenever the Runtime "works", it will re-render this scope
  379. pub fn mark_dirty(&mut self, id: ScopeId) {
  380. let Some(scope) = self.runtime.get_state(id) else {
  381. return;
  382. };
  383. tracing::event!(tracing::Level::TRACE, "Marking scope {:?} as dirty", id);
  384. let order = ScopeOrder::new(scope.height(), id);
  385. drop(scope);
  386. self.queue_scope(order);
  387. }
  388. /// Mark a task as dirty
  389. fn mark_task_dirty(&mut self, task: Task) {
  390. let Some(scope) = self.runtime.task_scope(task) else {
  391. return;
  392. };
  393. let Some(scope) = self.runtime.get_state(scope) else {
  394. return;
  395. };
  396. tracing::event!(
  397. tracing::Level::TRACE,
  398. "Marking task {:?} (spawned in {:?}) as dirty",
  399. task,
  400. scope.id,
  401. );
  402. let order = ScopeOrder::new(scope.height(), scope.id);
  403. drop(scope);
  404. self.queue_task(task, order);
  405. }
  406. /// Call a listener inside the VirtualDom with data from outside the VirtualDom. **The ElementId passed in must be the id of an element with a listener, not a static node or a text node.**
  407. ///
  408. /// This method will identify the appropriate element. The data must match up with the listener declared. Note that
  409. /// this method does not give any indication as to the success of the listener call. If the listener is not found,
  410. /// nothing will happen.
  411. ///
  412. /// It is up to the listeners themselves to mark nodes as dirty.
  413. ///
  414. /// If you have multiple events, you can call this method multiple times before calling "render_with_deadline"
  415. #[instrument(skip(self), level = "trace", name = "VirtualDom::handle_event")]
  416. pub fn handle_event(
  417. &mut self,
  418. name: &str,
  419. data: Rc<dyn Any>,
  420. element: ElementId,
  421. bubbles: bool,
  422. ) {
  423. let _runtime = RuntimeGuard::new(self.runtime.clone());
  424. if let Some(Some(parent_path)) = self.elements.get(element.0).copied() {
  425. if bubbles {
  426. self.handle_bubbling_event(parent_path, name, Event::new(data, bubbles));
  427. } else {
  428. self.handle_non_bubbling_event(parent_path, name, Event::new(data, bubbles));
  429. }
  430. }
  431. }
  432. /// Wait for the scheduler to have any work.
  433. ///
  434. /// This method polls the internal future queue, waiting for suspense nodes, tasks, or other work. This completes when
  435. /// any work is ready. If multiple scopes are marked dirty from a task or a suspense tree is finished, this method
  436. /// will exit.
  437. ///
  438. /// This method is cancel-safe, so you're fine to discard the future in a select block.
  439. ///
  440. /// This lets us poll async tasks and suspended trees during idle periods without blocking the main thread.
  441. ///
  442. /// # Example
  443. ///
  444. /// ```rust, no_run
  445. /// # use dioxus::prelude::*;
  446. /// # fn app() -> Element { rsx! { div {} } }
  447. /// let dom = VirtualDom::new(app);
  448. /// ```
  449. #[instrument(skip(self), level = "trace", name = "VirtualDom::wait_for_work")]
  450. pub async fn wait_for_work(&mut self) {
  451. loop {
  452. // Process all events - Scopes are marked dirty, etc
  453. // Sometimes when wakers fire we get a slew of updates at once, so its important that we drain this completely
  454. self.process_events();
  455. // Now that we have collected all queued work, we should check if we have any dirty scopes. If there are not, then we can poll any queued futures
  456. if self.has_dirty_scopes() {
  457. return;
  458. }
  459. // Make sure we set the runtime since we're running user code
  460. let _runtime = RuntimeGuard::new(self.runtime.clone());
  461. // There isn't any more work we can do synchronously. Wait for any new work to be ready
  462. self.wait_for_event().await;
  463. }
  464. }
  465. /// Wait for the next event to trigger and add it to the queue
  466. #[instrument(skip(self), level = "trace", name = "VirtualDom::wait_for_event")]
  467. async fn wait_for_event(&mut self) {
  468. match self.rx.next().await.expect("channel should never close") {
  469. SchedulerMsg::Immediate(id) => self.mark_dirty(id),
  470. SchedulerMsg::TaskNotified(id) => {
  471. // Instead of running the task immediately, we insert it into the runtime's task queue.
  472. // The task may be marked dirty at the same time as the scope that owns the task is dropped.
  473. self.mark_task_dirty(Task::from_id(id));
  474. }
  475. SchedulerMsg::EffectQueued => {}
  476. };
  477. }
  478. /// Queue any pending events
  479. fn queue_events(&mut self) {
  480. // Prevent a task from deadlocking the runtime by repeatedly queueing itself
  481. while let Ok(Some(msg)) = self.rx.try_next() {
  482. match msg {
  483. SchedulerMsg::Immediate(id) => self.mark_dirty(id),
  484. SchedulerMsg::TaskNotified(task) => self.mark_task_dirty(Task::from_id(task)),
  485. SchedulerMsg::EffectQueued => {}
  486. }
  487. }
  488. }
  489. /// Process all events in the queue until there are no more left
  490. #[instrument(skip(self), level = "trace", name = "VirtualDom::process_events")]
  491. pub fn process_events(&mut self) {
  492. self.queue_events();
  493. // Now that we have collected all queued work, we should check if we have any dirty scopes. If there are not, then we can poll any queued futures
  494. if self.has_dirty_scopes() {
  495. return;
  496. }
  497. self.poll_tasks()
  498. }
  499. /// Poll any queued tasks
  500. #[instrument(skip(self), level = "trace", name = "VirtualDom::poll_tasks")]
  501. fn poll_tasks(&mut self) {
  502. // Make sure we set the runtime since we're running user code
  503. let _runtime = RuntimeGuard::new(self.runtime.clone());
  504. // Keep polling tasks until there are no more effects or tasks to run
  505. // Or until we have no more dirty scopes
  506. while !self.runtime.dirty_tasks.borrow().is_empty()
  507. || !self.runtime.pending_effects.borrow().is_empty()
  508. {
  509. // Next, run any queued tasks
  510. // We choose not to poll the deadline since we complete pretty quickly anyways
  511. while let Some(task) = self.pop_task() {
  512. let _ = self.runtime.handle_task_wakeup(task);
  513. // Running that task, may mark a scope higher up as dirty. If it does, return from the function early
  514. self.queue_events();
  515. if self.has_dirty_scopes() {
  516. return;
  517. }
  518. }
  519. // At this point, we have finished running all tasks that are pending and we haven't found any scopes to rerun. This means it is safe to run our lowest priority work: effects
  520. while let Some(effect) = self.pop_effect() {
  521. effect.run(&self.runtime);
  522. // Check if any new scopes are queued for rerun
  523. self.queue_events();
  524. if self.has_dirty_scopes() {
  525. return;
  526. }
  527. }
  528. }
  529. }
  530. /// Replace a template at runtime. This will re-render all components that use this template.
  531. /// This is the primitive that enables hot-reloading.
  532. ///
  533. /// The caller must ensure that the template references the same dynamic attributes and nodes as the original template.
  534. ///
  535. /// This will only replace the parent template, not any nested templates.
  536. #[instrument(skip(self), level = "trace", name = "VirtualDom::replace_template")]
  537. pub fn replace_template(&mut self, template: Template) {
  538. // we only replace templates if hot reloading is enabled
  539. #[cfg(debug_assertions)]
  540. {
  541. // Save the template ID
  542. self.templates.insert(template.name, template);
  543. // Only queue the template to be written if its not completely dynamic
  544. if !template.is_completely_dynamic() {
  545. self.queued_templates.push(template);
  546. }
  547. // iterating a slab is very inefficient, but this is a rare operation that will only happen during development so it's fine
  548. let mut dirty = Vec::new();
  549. for (id, scope) in self.scopes.iter() {
  550. // Recurse into the dynamic nodes of the existing mounted node to see if the template is alive in the tree
  551. fn check_node_for_templates(node: &crate::VNode, template: Template) -> bool {
  552. if node.template.get().name == template.name {
  553. return true;
  554. }
  555. for dynamic in node.dynamic_nodes.iter() {
  556. if let crate::DynamicNode::Fragment(nodes) = dynamic {
  557. for node in nodes {
  558. if check_node_for_templates(node, template) {
  559. return true;
  560. }
  561. }
  562. }
  563. }
  564. false
  565. }
  566. if let Some(sync) = scope.try_root_node() {
  567. if check_node_for_templates(sync, template) {
  568. dirty.push(ScopeId(id));
  569. }
  570. }
  571. }
  572. for dirty in dirty {
  573. self.mark_dirty(dirty);
  574. }
  575. }
  576. }
  577. /// Rebuild the virtualdom without handling any of the mutations
  578. ///
  579. /// This is useful for testing purposes and in cases where you render the output of the virtualdom without
  580. /// handling any of its mutations.
  581. pub fn rebuild_in_place(&mut self) {
  582. self.rebuild(&mut NoOpMutations);
  583. }
  584. /// [`VirtualDom::rebuild`] to a vector of mutations for testing purposes
  585. pub fn rebuild_to_vec(&mut self) -> Mutations {
  586. let mut mutations = Mutations::default();
  587. self.rebuild(&mut mutations);
  588. mutations
  589. }
  590. /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom from scratch.
  591. ///
  592. /// The mutations item expects the RealDom's stack to be the root of the application.
  593. ///
  594. /// Tasks will not be polled with this method, nor will any events be processed from the event queue. Instead, the
  595. /// root component will be run once and then diffed. All updates will flow out as mutations.
  596. ///
  597. /// All state stored in components will be completely wiped away.
  598. ///
  599. /// Any templates previously registered will remain.
  600. ///
  601. /// # Example
  602. /// ```rust, no_run
  603. /// # use dioxus::prelude::*;
  604. /// # use dioxus_core::*;
  605. /// fn app() -> Element {
  606. /// rsx! { "hello world" }
  607. /// }
  608. ///
  609. /// let mut dom = VirtualDom::new(app);
  610. /// let mut mutations = Mutations::default();
  611. /// dom.rebuild(&mut mutations);
  612. /// ```
  613. #[instrument(skip(self, to), level = "trace", name = "VirtualDom::rebuild")]
  614. pub fn rebuild(&mut self, to: &mut impl WriteMutations) {
  615. self.flush_templates(to);
  616. let _runtime = RuntimeGuard::new(self.runtime.clone());
  617. let new_nodes = self.run_scope(ScopeId::ROOT);
  618. self.scopes[ScopeId::ROOT.0].last_rendered_node = Some(new_nodes.clone());
  619. // Rebuilding implies we append the created elements to the root
  620. let m = self.create_scope(Some(to), ScopeId::ROOT, new_nodes, None);
  621. to.append_children(ElementId(0), m);
  622. }
  623. /// Render whatever the VirtualDom has ready as fast as possible without requiring an executor to progress
  624. /// suspended subtrees.
  625. #[instrument(skip(self, to), level = "trace", name = "VirtualDom::render_immediate")]
  626. pub fn render_immediate(&mut self, to: &mut impl WriteMutations) {
  627. self.flush_templates(to);
  628. // Process any events that might be pending in the queue
  629. // Signals marked with .write() need a chance to be handled by the effect driver
  630. // This also processes futures which might progress into immediately rerunning a scope
  631. self.process_events();
  632. // Next, diff any dirty scopes
  633. // We choose not to poll the deadline since we complete pretty quickly anyways
  634. let _runtime = RuntimeGuard::new(self.runtime.clone());
  635. while let Some(work) = self.pop_work() {
  636. match work {
  637. Work::PollTask(task) => {
  638. _ = self.runtime.handle_task_wakeup(task);
  639. // Make sure we process any new events
  640. self.queue_events();
  641. }
  642. Work::RerunScope(scope) => {
  643. // If the scope is dirty, run the scope and get the mutations
  644. self.run_and_diff_scope(Some(to), scope.id);
  645. }
  646. }
  647. }
  648. self.runtime.finish_render();
  649. }
  650. /// [`Self::render_immediate`] to a vector of mutations for testing purposes
  651. pub fn render_immediate_to_vec(&mut self) -> Mutations {
  652. let mut mutations = Mutations::default();
  653. self.render_immediate(&mut mutations);
  654. mutations
  655. }
  656. /// Render the virtual dom, waiting for all suspense to be finished
  657. ///
  658. /// The mutations will be thrown out, so it's best to use this method for things like SSR that have async content
  659. ///
  660. /// We don't call "flush_sync" here since there's no sync work to be done. Futures will be progressed like usual,
  661. /// however any futures waiting on flush_sync will remain pending
  662. #[instrument(skip(self), level = "trace", name = "VirtualDom::wait_for_suspense")]
  663. pub async fn wait_for_suspense(&mut self) {
  664. loop {
  665. if !self.suspended_tasks_remaining() {
  666. break;
  667. }
  668. self.wait_for_suspense_work().await;
  669. self.render_suspense_immediate().await;
  670. }
  671. }
  672. /// Check if there are any suspended tasks remaining
  673. pub fn suspended_tasks_remaining(&self) -> bool {
  674. self.runtime.suspended_tasks.get() > 0
  675. }
  676. /// Wait for the scheduler to have any work that should be run during suspense.
  677. pub async fn wait_for_suspense_work(&mut self) {
  678. // Wait for a work to be ready (IE new suspense leaves to pop up)
  679. loop {
  680. // Process all events - Scopes are marked dirty, etc
  681. // Sometimes when wakers fire we get a slew of updates at once, so its important that we drain this completely
  682. self.queue_events();
  683. // Now that we have collected all queued work, we should check if we have any dirty scopes. If there are not, then we can poll any queued futures
  684. if self.has_dirty_scopes() {
  685. break;
  686. }
  687. {
  688. // Make sure we set the runtime since we're running user code
  689. let _runtime = RuntimeGuard::new(self.runtime.clone());
  690. // Next, run any queued tasks
  691. // We choose not to poll the deadline since we complete pretty quickly anyways
  692. let mut tasks_polled = 0;
  693. while let Some(task) = self.pop_task() {
  694. if self.runtime.task_runs_during_suspense(task) {
  695. let _ = self.runtime.handle_task_wakeup(task);
  696. // Running that task, may mark a scope higher up as dirty. If it does, return from the function early
  697. self.queue_events();
  698. if self.has_dirty_scopes() {
  699. return;
  700. }
  701. }
  702. tasks_polled += 1;
  703. // Once we have polled a few tasks, we manually yield to the scheduler to give it a chance to run other pending work
  704. if tasks_polled > 32 {
  705. yield_now().await;
  706. tasks_polled = 0;
  707. }
  708. }
  709. }
  710. self.wait_for_event().await;
  711. }
  712. }
  713. /// Render any dirty scopes immediately, but don't poll any futures that are client only on that scope
  714. /// Returns a list of suspense boundaries that were resolved
  715. pub async fn render_suspense_immediate(&mut self) -> Vec<ScopeId> {
  716. // Queue any new events before we start working
  717. self.queue_events();
  718. // Render whatever work needs to be rendered, unlocking new futures and suspense leaves
  719. let _runtime = RuntimeGuard::new(self.runtime.clone());
  720. let mut work_done = 0;
  721. while let Some(work) = self.pop_work() {
  722. match work {
  723. Work::PollTask(task) => {
  724. // During suspense, we only want to run tasks that are suspended
  725. if self.runtime.task_runs_during_suspense(task) {
  726. let _ = self.runtime.handle_task_wakeup(task);
  727. }
  728. }
  729. Work::RerunScope(scope) => {
  730. let scope_id: ScopeId = scope.id;
  731. let run_scope = self
  732. .runtime
  733. .get_state(scope.id)
  734. .filter(|scope| scope.should_run_during_suspense())
  735. .is_some();
  736. if run_scope {
  737. // If the scope is dirty, run the scope and get the mutations
  738. self.run_and_diff_scope(None::<&mut NoOpMutations>, scope_id);
  739. tracing::trace!("Ran scope {:?} during suspense", scope_id);
  740. } else {
  741. tracing::warn!(
  742. "Scope {:?} was marked as dirty, but will not rerun during suspense. Only nodes that are under a suspense boundary rerun during suspense",
  743. scope_id
  744. );
  745. }
  746. }
  747. }
  748. // Queue any new events
  749. self.queue_events();
  750. work_done += 1;
  751. // Once we have polled a few tasks, we manually yield to the scheduler to give it a chance to run other pending work
  752. if work_done > 32 {
  753. yield_now().await;
  754. work_done = 0;
  755. }
  756. }
  757. self.resolved_scopes
  758. .sort_by_key(|&id| self.runtime.get_state(id).unwrap().height);
  759. std::mem::take(&mut self.resolved_scopes)
  760. }
  761. /// Get the current runtime
  762. pub fn runtime(&self) -> Rc<Runtime> {
  763. self.runtime.clone()
  764. }
  765. /// Flush any queued template changes
  766. #[instrument(skip(self, to), level = "trace", name = "VirtualDom::flush_templates")]
  767. fn flush_templates(&mut self, to: &mut impl WriteMutations) {
  768. for template in self.queued_templates.drain(..) {
  769. to.register_template(template);
  770. }
  771. }
  772. /*
  773. ------------------------
  774. The algorithm works by walking through the list of dynamic attributes, checking their paths, and breaking when
  775. we find the target path.
  776. With the target path, we try and move up to the parent until there is no parent.
  777. Due to how bubbling works, we call the listeners before walking to the parent.
  778. If we wanted to do capturing, then we would accumulate all the listeners and call them in reverse order.
  779. ----------------------
  780. For a visual demonstration, here we present a tree on the left and whether or not a listener is collected on the
  781. right.
  782. | <-- yes (is ascendant)
  783. | | | <-- no (is not direct ascendant)
  784. | | <-- yes (is ascendant)
  785. | | | | | <--- target element, break early, don't check other listeners
  786. | | | <-- no, broke early
  787. | <-- no, broke early
  788. */
  789. #[instrument(
  790. skip(self, uievent),
  791. level = "trace",
  792. name = "VirtualDom::handle_bubbling_event"
  793. )]
  794. fn handle_bubbling_event(&mut self, parent: ElementRef, name: &str, uievent: Event<dyn Any>) {
  795. // If the event bubbles, we traverse through the tree until we find the target element.
  796. // Loop through each dynamic attribute (in a depth first order) in this template before moving up to the template's parent.
  797. let mut parent = Some(parent);
  798. while let Some(path) = parent {
  799. let mut listeners = vec![];
  800. let Some(mount) = self.mounts.get(path.mount.0) else {
  801. // If the node is suspended and not mounted, we can just ignore the event
  802. return;
  803. };
  804. let el_ref = &mount.node;
  805. let node_template = el_ref.template.get();
  806. let target_path = path.path;
  807. // Accumulate listeners into the listener list bottom to top
  808. for (idx, this_path) in node_template.breadth_first_attribute_paths() {
  809. let attrs = &*el_ref.dynamic_attrs[idx];
  810. for attr in attrs.iter() {
  811. // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one
  812. if attr.name.get(2..) == Some(name) && target_path.is_descendant(this_path) {
  813. listeners.push(&attr.value);
  814. // Break if this is the exact target element.
  815. // This means we won't call two listeners with the same name on the same element. This should be
  816. // documented, or be rejected from the rsx! macro outright
  817. if target_path == this_path {
  818. break;
  819. }
  820. }
  821. }
  822. }
  823. // Now that we've accumulated all the parent attributes for the target element, call them in reverse order
  824. // We check the bubble state between each call to see if the event has been stopped from bubbling
  825. tracing::event!(
  826. tracing::Level::TRACE,
  827. "Calling {} listeners",
  828. listeners.len()
  829. );
  830. for listener in listeners.into_iter().rev() {
  831. if let AttributeValue::Listener(listener) = listener {
  832. self.runtime.rendering.set(false);
  833. listener.call(uievent.clone());
  834. self.runtime.rendering.set(true);
  835. if !uievent.propagates.get() {
  836. return;
  837. }
  838. }
  839. }
  840. let mount = el_ref.mount.get().as_usize();
  841. parent = mount.and_then(|id| self.mounts.get(id).and_then(|el| el.parent));
  842. }
  843. }
  844. /// Call an event listener in the simplest way possible without bubbling upwards
  845. #[instrument(
  846. skip(self, uievent),
  847. level = "trace",
  848. name = "VirtualDom::handle_non_bubbling_event"
  849. )]
  850. fn handle_non_bubbling_event(&mut self, node: ElementRef, name: &str, uievent: Event<dyn Any>) {
  851. let Some(mount) = self.mounts.get(node.mount.0) else {
  852. // If the node is suspended and not mounted, we can just ignore the event
  853. return;
  854. };
  855. let el_ref = &mount.node;
  856. let node_template = el_ref.template.get();
  857. let target_path = node.path;
  858. for (idx, this_path) in node_template.breadth_first_attribute_paths() {
  859. let attrs = &*el_ref.dynamic_attrs[idx];
  860. for attr in attrs.iter() {
  861. // Remove the "on" prefix if it exists, TODO, we should remove this and settle on one
  862. // Only call the listener if this is the exact target element.
  863. if attr.name.get(2..) == Some(name) && target_path == this_path {
  864. if let AttributeValue::Listener(listener) = &attr.value {
  865. self.runtime.rendering.set(false);
  866. listener.call(uievent.clone());
  867. self.runtime.rendering.set(true);
  868. break;
  869. }
  870. }
  871. }
  872. }
  873. }
  874. }
  875. impl Drop for VirtualDom {
  876. fn drop(&mut self) {
  877. // Drop all scopes in order of height
  878. let mut scopes = self.scopes.drain().collect::<Vec<_>>();
  879. scopes.sort_by_key(|scope| scope.state().height);
  880. for scope in scopes.into_iter().rev() {
  881. drop(scope);
  882. }
  883. }
  884. }
  885. /// Yield control back to the async scheduler. This is used to give the scheduler a chance to run other pending work. Or cancel the task if the client has disconnected.
  886. async fn yield_now() {
  887. let mut yielded = false;
  888. std::future::poll_fn::<(), _>(move |cx| {
  889. if !yielded {
  890. cx.waker().wake_by_ref();
  891. yielded = true;
  892. std::task::Poll::Pending
  893. } else {
  894. std::task::Poll::Ready(())
  895. }
  896. })
  897. .await;
  898. }