virtual_dom.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //! # VirtualDOM Implementation for Rust
  2. //! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
  3. //!
  4. //! In this file, multiple items are defined. This file is big, but should be documented well to
  5. //! navigate the innerworkings of the Dom. We try to keep these main mechanics in this file to limit
  6. //! the possible exposed API surface (keep fields private). This particular implementation of VDOM
  7. //! is extremely efficient, but relies on some unsafety under the hood to do things like manage
  8. //! micro-heaps for components. We are currently working on refactoring the safety out into safe(r)
  9. //! abstractions, but current tests (MIRI and otherwise) show no issues with the current implementation.
  10. //!
  11. //! Included is:
  12. //! - The [`VirtualDom`] itself
  13. //! - The [`Scope`] object for mangning component lifecycle
  14. //! - The [`ActiveFrame`] object for managing the Scope`s microheap
  15. //! - The [`Context`] object for exposing VirtualDOM API to components
  16. //! - The [`NodeFactory`] object for lazyily exposing the `Context` API to the nodebuilder API
  17. //! - The [`Hook`] object for exposing state management in components.
  18. //!
  19. //! This module includes just the barebones for a complete VirtualDOM API.
  20. //! Additional functionality is defined in the respective files.
  21. use crate::tasks::TaskQueue;
  22. use crate::{arena::SharedArena, innerlude::*};
  23. use slotmap::DefaultKey;
  24. use slotmap::SlotMap;
  25. use std::{any::TypeId, fmt::Debug, rc::Rc};
  26. pub type ScopeIdx = DefaultKey;
  27. /// An integrated virtual node system that progresses events and diffs UI trees.
  28. /// Differences are converted into patches which a renderer can use to draw the UI.
  29. pub struct VirtualDom {
  30. /// All mounted components are arena allocated to make additions, removals, and references easy to work with
  31. /// A generational arena is used to re-use slots of deleted scopes without having to resize the underlying arena.
  32. ///
  33. /// This is wrapped in an UnsafeCell because we will need to get mutable access to unique values in unique bump arenas
  34. /// and rusts's guartnees cannot prove that this is safe. We will need to maintain the safety guarantees manually.
  35. pub components: SharedArena,
  36. /// The index of the root component
  37. /// Should always be the first (gen=0, id=0)
  38. pub base_scope: ScopeIdx,
  39. /// All components dump their updates into a queue to be processed
  40. pub(crate) event_queue: EventQueue,
  41. pub(crate) tasks: TaskQueue,
  42. /// a strong allocation to the "caller" for the original component and its props
  43. #[doc(hidden)]
  44. _root_caller: Rc<WrappedCaller>,
  45. /// Type of the original cx. This is stored as TypeId so VirtualDom does not need to be generic.
  46. ///
  47. /// Whenver props need to be updated, an Error will be thrown if the new props do not
  48. /// match the props used to create the VirtualDom.
  49. #[doc(hidden)]
  50. _root_prop_type: std::any::TypeId,
  51. }
  52. // ======================================
  53. // Public Methods for the VirtualDom
  54. // ======================================
  55. impl VirtualDom {
  56. /// Create a new instance of the Dioxus Virtual Dom with no properties for the root component.
  57. ///
  58. /// This means that the root component must either consumes its own context, or statics are used to generate the page.
  59. /// The root component can access things like routing in its context.
  60. ///
  61. /// As an end-user, you'll want to use the Renderer's "new" method instead of this method.
  62. /// Directly creating the VirtualDOM is only useful when implementing a new renderer.
  63. ///
  64. ///
  65. /// ```ignore
  66. /// // Directly from a closure
  67. ///
  68. /// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
  69. ///
  70. /// // or pass in...
  71. ///
  72. /// let root = |cx| {
  73. /// cx.render(rsx!{
  74. /// div {"hello world"}
  75. /// })
  76. /// }
  77. /// let dom = VirtualDom::new(root);
  78. ///
  79. /// // or directly from a fn
  80. ///
  81. /// fn Example(cx: Context<()>) -> VNode {
  82. /// cx.render(rsx!{ div{"hello world"} })
  83. /// }
  84. ///
  85. /// let dom = VirtualDom::new(Example);
  86. /// ```
  87. pub fn new(root: FC<()>) -> Self {
  88. Self::new_with_props(root, ())
  89. }
  90. /// Start a new VirtualDom instance with a dependent cx.
  91. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  92. ///
  93. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  94. /// to toss out the entire tree.
  95. ///
  96. /// ```ignore
  97. /// // Directly from a closure
  98. ///
  99. /// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
  100. ///
  101. /// // or pass in...
  102. ///
  103. /// let root = |cx| {
  104. /// cx.render(rsx!{
  105. /// div {"hello world"}
  106. /// })
  107. /// }
  108. /// let dom = VirtualDom::new(root);
  109. ///
  110. /// // or directly from a fn
  111. ///
  112. /// fn Example(cx: Context, props: &SomeProps) -> VNode {
  113. /// cx.render(rsx!{ div{"hello world"} })
  114. /// }
  115. ///
  116. /// let dom = VirtualDom::new(Example);
  117. /// ```
  118. pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
  119. let components = SharedArena::new(SlotMap::new());
  120. // Normally, a component would be passed as a child in the RSX macro which automatically produces OpaqueComponents
  121. // Here, we need to make it manually, using an RC to force the Weak reference to stick around for the main scope.
  122. let _root_caller: Rc<WrappedCaller> = Rc::new(move |scope| {
  123. // let _root_caller: Rc<OpaqueComponent<'static>> = Rc::new(move |scope| {
  124. // the lifetime of this closure is just as long as the lifetime on the scope reference
  125. // this closure moves root props (which is static) into this closure
  126. let props = unsafe { &*(&root_props as *const _) };
  127. root(Context {
  128. props,
  129. scope,
  130. tasks: todo!(),
  131. })
  132. });
  133. // Create a weak reference to the OpaqueComponent for the root scope to use as its render function
  134. let caller_ref = Rc::downgrade(&_root_caller);
  135. // Build a funnel for hooks to send their updates into. The `use_hook` method will call into the update funnel.
  136. let event_queue = EventQueue::default();
  137. let _event_queue = event_queue.clone();
  138. // Make the first scope
  139. // We don't run the component though, so renderers will need to call "rebuild" when they initialize their DOM
  140. let link = components.clone();
  141. let base_scope = components
  142. .with(|arena| {
  143. arena.insert_with_key(move |myidx| {
  144. let event_channel = _event_queue.new_channel(0, myidx);
  145. Scope::new(caller_ref, myidx, None, 0, event_channel, link, &[])
  146. })
  147. })
  148. .unwrap();
  149. Self {
  150. _root_caller,
  151. base_scope,
  152. event_queue,
  153. components,
  154. tasks: TaskQueue::new(),
  155. _root_prop_type: TypeId::of::<P>(),
  156. }
  157. }
  158. }
  159. // ======================================
  160. // Private Methods for the VirtualDom
  161. // ======================================
  162. impl VirtualDom {
  163. /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
  164. /// Currently this doesn't do what we want it to do
  165. pub fn rebuild<'s, Dom: RealDom<'s>>(&'s mut self, realdom: &mut Dom) -> Result<()> {
  166. let mut diff_machine = DiffMachine::new(
  167. realdom,
  168. &self.components,
  169. self.base_scope,
  170. self.event_queue.clone(),
  171. );
  172. // Schedule an update and then immediately call it on the root component
  173. // This is akin to a hook being called from a listener and requring a re-render
  174. // Instead, this is done on top-level component
  175. let base = self.components.try_get(self.base_scope)?;
  176. let update = &base.event_channel;
  177. update();
  178. self.progress_completely(&mut diff_machine)?;
  179. Ok(())
  180. }
  181. /// This method is the most sophisticated way of updating the virtual dom after an external event has been triggered.
  182. ///
  183. /// Given a synthetic event, the component that triggered the event, and the index of the callback, this runs the virtual
  184. /// dom to completion, tagging components that need updates, compressing events together, and finally emitting a single
  185. /// change list.
  186. ///
  187. /// If implementing an external renderer, this is the perfect method to combine with an async event loop that waits on
  188. /// listeners, something like this:
  189. ///
  190. /// ```ignore
  191. /// while let Ok(event) = receiver.recv().await {
  192. /// let edits = self.internal_dom.progress_with_event(event)?;
  193. /// for edit in &edits {
  194. /// patch_machine.handle_edit(edit);
  195. /// }
  196. /// }
  197. /// ```
  198. ///
  199. /// Note: this method is not async and does not provide suspense-like functionality. It is up to the renderer to provide the
  200. /// executor and handlers for suspense as show in the example.
  201. ///
  202. /// ```ignore
  203. /// let (sender, receiver) = channel::new();
  204. /// sender.send(EventTrigger::start());
  205. ///
  206. /// let mut dom = VirtualDom::new();
  207. /// dom.suspense_handler(|event| sender.send(event));
  208. ///
  209. /// while let Ok(diffs) = dom.progress_with_event(receiver.recv().await) {
  210. /// render(diffs);
  211. /// }
  212. ///
  213. /// ```
  214. //
  215. // Developer notes:
  216. // ----
  217. // This method has some pretty complex safety guarantees to uphold.
  218. // We interact with bump arenas, raw pointers, and use UnsafeCell to get a partial borrow of the arena.
  219. // The final EditList has edits that pull directly from the Bump Arenas which add significant complexity
  220. // in crafting a 100% safe solution with traditional lifetimes. Consider this method to be internally unsafe
  221. // but the guarantees provide a safe, fast, and efficient abstraction for the VirtualDOM updating framework.
  222. //
  223. // A good project would be to remove all unsafe from this crate and move the unsafety into safer abstractions.
  224. pub fn progress_with_event<'s, Dom: RealDom<'s>>(
  225. &'s mut self,
  226. realdom: &'_ mut Dom,
  227. trigger: EventTrigger,
  228. ) -> Result<()> {
  229. let id = trigger.originator.clone();
  230. self.components.try_get_mut(id)?.call_listener(trigger)?;
  231. let mut diff_machine =
  232. DiffMachine::new(realdom, &self.components, id, self.event_queue.clone());
  233. self.progress_completely(&mut diff_machine)?;
  234. Ok(())
  235. }
  236. /// Consume the event queue, descending depth-first.
  237. /// Only ever run each component once.
  238. ///
  239. /// The DiffMachine logs its progress as it goes which might be useful for certain types of renderers.
  240. pub(crate) fn progress_completely<'a, 'bump, Dom: RealDom<'bump>>(
  241. &'bump self,
  242. diff_machine: &'_ mut DiffMachine<'a, 'bump, Dom>,
  243. ) -> Result<()> {
  244. // Now, there are events in the queue
  245. let mut updates = self.event_queue.queue.as_ref().borrow_mut();
  246. // Order the nodes by their height, we want the nodes with the smallest depth on top
  247. // This prevents us from running the same component multiple times
  248. updates.sort_unstable();
  249. log::debug!("There are: {:#?} updates to be processed", updates.len());
  250. // Iterate through the triggered nodes (sorted by height) and begin to diff them
  251. for update in updates.drain(..) {
  252. log::debug!("Running updates for: {:#?}", update);
  253. // Make sure this isn't a node we've already seen, we don't want to double-render anything
  254. // If we double-renderer something, this would cause memory safety issues
  255. if diff_machine.seen_nodes.contains(&update.idx) {
  256. continue;
  257. }
  258. // Now, all the "seen nodes" are nodes that got notified by running this listener
  259. diff_machine.seen_nodes.insert(update.idx.clone());
  260. // Start a new mutable borrow to components
  261. // We are guaranteeed that this scope is unique because we are tracking which nodes have modified
  262. let cur_component = self.components.try_get_mut(update.idx).unwrap();
  263. cur_component.run_scope()?;
  264. let (old, new) = (cur_component.old_frame(), cur_component.next_frame());
  265. diff_machine.diff_node(old, new);
  266. // log::debug!(
  267. // "Processing update: {:#?} with height {}",
  268. // &update.idx,
  269. // cur_height
  270. // );
  271. }
  272. Ok(())
  273. }
  274. pub fn base_scope(&self) -> &Scope {
  275. let idx = self.base_scope;
  276. self.components.try_get(idx).unwrap()
  277. }
  278. }
  279. // TODO!
  280. // These impls are actually wrong. The DOM needs to have a mutex implemented.
  281. unsafe impl Sync for VirtualDom {}
  282. unsafe impl Send for VirtualDom {}