virtual_dom.rs 32 KB

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