virtual_dom.rs 27 KB

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