virtual_dom.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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::Any;
  26. use std::any::TypeId;
  27. use std::cell::RefCell;
  28. use std::pin::Pin;
  29. slotmap::new_key_type! {
  30. pub struct ScopeId;
  31. }
  32. /// An integrated virtual node system that progresses events and diffs UI trees.
  33. /// Differences are converted into patches which a renderer can use to draw the UI.
  34. ///
  35. ///
  36. ///
  37. ///
  38. ///
  39. ///
  40. ///
  41. pub struct VirtualDom {
  42. /// All mounted components are arena allocated to make additions, removals, and references easy to work with
  43. /// A generational arena is used to re-use slots of deleted scopes without having to resize the underlying arena.
  44. ///
  45. /// This is wrapped in an UnsafeCell because we will need to get mutable access to unique values in unique bump arenas
  46. /// and rusts's guartnees cannot prove that this is safe. We will need to maintain the safety guarantees manually.
  47. pub components: SharedArena,
  48. /// The index of the root component
  49. /// Should always be the first (gen=0, id=0)
  50. pub base_scope: ScopeId,
  51. pub triggers: RefCell<Vec<EventTrigger>>,
  52. /// All components dump their updates into a queue to be processed
  53. pub event_queue: EventQueue,
  54. pub tasks: TaskQueue,
  55. heuristics: HeuristicsEngine,
  56. root_props: std::pin::Pin<Box<dyn std::any::Any>>,
  57. /// Type of the original props. This is stored as TypeId so VirtualDom does not need to be generic.
  58. ///
  59. /// Whenver props need to be updated, an Error will be thrown if the new props do not
  60. /// match the props used to create the VirtualDom.
  61. #[doc(hidden)]
  62. _root_prop_type: std::any::TypeId,
  63. }
  64. // ======================================
  65. // Public Methods for the VirtualDom
  66. // ======================================
  67. impl VirtualDom {
  68. /// Create a new instance of the Dioxus Virtual Dom with no properties for the root component.
  69. ///
  70. /// This means that the root component must either consumes its own context, or statics are used to generate the page.
  71. /// The root component can access things like routing in its context.
  72. ///
  73. /// As an end-user, you'll want to use the Renderer's "new" method instead of this method.
  74. /// Directly creating the VirtualDOM is only useful when implementing a new renderer.
  75. ///
  76. ///
  77. /// ```ignore
  78. /// // Directly from a closure
  79. ///
  80. /// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
  81. ///
  82. /// // or pass in...
  83. ///
  84. /// let root = |cx| {
  85. /// cx.render(rsx!{
  86. /// div {"hello world"}
  87. /// })
  88. /// }
  89. /// let dom = VirtualDom::new(root);
  90. ///
  91. /// // or directly from a fn
  92. ///
  93. /// fn Example(cx: Context<()>) -> DomTree {
  94. /// cx.render(rsx!{ div{"hello world"} })
  95. /// }
  96. ///
  97. /// let dom = VirtualDom::new(Example);
  98. /// ```
  99. pub fn new(root: FC<()>) -> Self {
  100. Self::new_with_props(root, ())
  101. }
  102. /// Start a new VirtualDom instance with a dependent cx.
  103. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  104. ///
  105. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  106. /// to toss out the entire tree.
  107. ///
  108. /// ```ignore
  109. /// // Directly from a closure
  110. ///
  111. /// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
  112. ///
  113. /// // or pass in...
  114. ///
  115. /// let root = |cx| {
  116. /// cx.render(rsx!{
  117. /// div {"hello world"}
  118. /// })
  119. /// }
  120. /// let dom = VirtualDom::new(root);
  121. ///
  122. /// // or directly from a fn
  123. ///
  124. /// fn Example(cx: Context, props: &SomeProps) -> VNode {
  125. /// cx.render(rsx!{ div{"hello world"} })
  126. /// }
  127. ///
  128. /// let dom = VirtualDom::new(Example);
  129. /// ```
  130. pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
  131. let components = SharedArena::new(SlotMap::<ScopeId, Scope>::with_key());
  132. let root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
  133. let props_ptr = root_props.as_ref().downcast_ref::<P>().unwrap() as *const P;
  134. // Build a funnel for hooks to send their updates into. The `use_hook` method will call into the update funnel.
  135. let event_queue = EventQueue::default();
  136. let _event_queue = event_queue.clone();
  137. let link = components.clone();
  138. let tasks = TaskQueue::new();
  139. let submitter = tasks.new_submitter();
  140. let base_scope = components
  141. .with(|arena| {
  142. arena.insert_with_key(move |myidx| {
  143. let event_channel = _event_queue.new_channel(0, myidx);
  144. let caller = NodeFactory::create_component_caller(root, props_ptr as *const _);
  145. Scope::new(caller, myidx, None, 0, event_channel, link, &[], submitter)
  146. })
  147. })
  148. .unwrap();
  149. Self {
  150. base_scope,
  151. event_queue,
  152. components,
  153. root_props,
  154. tasks,
  155. heuristics: HeuristicsEngine::new(),
  156. triggers: Default::default(),
  157. _root_prop_type: TypeId::of::<P>(),
  158. }
  159. }
  160. pub fn launch_in_place(root: FC<()>) -> Self {
  161. let mut s = Self::new(root);
  162. s.rebuild_in_place();
  163. s
  164. }
  165. /// Creates a new virtualdom and immediately rebuilds it in place, not caring about the RealDom to write into.
  166. ///
  167. pub fn launch_with_props_in_place<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
  168. let mut s = Self::new_with_props(root, root_props);
  169. s.rebuild_in_place();
  170. s
  171. }
  172. /// Rebuilds the VirtualDOM from scratch, but uses a "dummy" RealDom.
  173. ///
  174. /// Used in contexts where a real copy of the structure doesn't matter, and the VirtualDOM is the source of truth.
  175. ///
  176. /// ## Why?
  177. ///
  178. /// This method uses the `DebugDom` under the hood - essentially making the VirtualDOM's diffing patches a "no-op".
  179. ///
  180. /// SSR takes advantage of this by using Dioxus itself as the source of truth, and rendering from the tree directly.
  181. pub fn rebuild_in_place(&mut self) -> Result<()> {
  182. let mut realdom = DebugDom::new();
  183. let mut edits = Vec::new();
  184. self.rebuild(&mut realdom, &mut edits)
  185. }
  186. /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
  187. ///
  188. /// Currently this doesn't do what we want it to do
  189. ///
  190. /// The diff machine expects the RealDom's stack to be the root of the application
  191. pub fn rebuild<'s, Dom: RealDom<'s>>(
  192. &'s mut self,
  193. realdom: &mut Dom,
  194. edits: &mut Vec<DomEdit<'s>>,
  195. ) -> Result<()> {
  196. let mut diff_machine = DiffMachine::new(
  197. edits,
  198. realdom,
  199. &self.components,
  200. self.base_scope,
  201. self.event_queue.clone(),
  202. &self.tasks,
  203. );
  204. let cur_component = self.components.get_mut(self.base_scope).unwrap();
  205. // We run the component. If it succeeds, then we can diff it and add the changes to the dom.
  206. if cur_component.run_scope().is_ok() {
  207. let meta = diff_machine.create(cur_component.next_frame());
  208. diff_machine.edits.append_children(meta.added_to_stack);
  209. }
  210. Ok(())
  211. }
  212. ///
  213. ///
  214. ///
  215. ///
  216. ///
  217. pub fn queue_event(&self, trigger: EventTrigger) -> Result<()> {
  218. let mut triggers = self.triggers.borrow_mut();
  219. triggers.push(trigger);
  220. Ok(())
  221. }
  222. /// This method is the most sophisticated way of updating the virtual dom after an external event has been triggered.
  223. ///
  224. /// Given a synthetic event, the component that triggered the event, and the index of the callback, this runs the virtual
  225. /// dom to completion, tagging components that need updates, compressing events together, and finally emitting a single
  226. /// change list.
  227. ///
  228. /// If implementing an external renderer, this is the perfect method to combine with an async event loop that waits on
  229. /// listeners, something like this:
  230. ///
  231. /// ```ignore
  232. /// while let Ok(event) = receiver.recv().await {
  233. /// let edits = self.internal_dom.progress_with_event(event)?;
  234. /// for edit in &edits {
  235. /// patch_machine.handle_edit(edit);
  236. /// }
  237. /// }
  238. /// ```
  239. ///
  240. /// Note: this method is not async and does not provide suspense-like functionality. It is up to the renderer to provide the
  241. /// executor and handlers for suspense as show in the example.
  242. ///
  243. /// ```ignore
  244. /// let (sender, receiver) = channel::new();
  245. /// sender.send(EventTrigger::start());
  246. ///
  247. /// let mut dom = VirtualDom::new();
  248. /// dom.suspense_handler(|event| sender.send(event));
  249. ///
  250. /// while let Ok(diffs) = dom.progress_with_event(receiver.recv().await) {
  251. /// render(diffs);
  252. /// }
  253. ///
  254. /// ```
  255. //
  256. // Developer notes:
  257. // ----
  258. // This method has some pretty complex safety guarantees to uphold.
  259. // We interact with bump arenas, raw pointers, and use UnsafeCell to get a partial borrow of the arena.
  260. // The final EditList has edits that pull directly from the Bump Arenas which add significant complexity
  261. // in crafting a 100% safe solution with traditional lifetimes. Consider this method to be internally unsafe
  262. // but the guarantees provide a safe, fast, and efficient abstraction for the VirtualDOM updating framework.
  263. //
  264. // A good project would be to remove all unsafe from this crate and move the unsafety into safer abstractions.
  265. pub async fn progress_with_event<'s, Dom: RealDom<'s>>(
  266. &'s mut self,
  267. realdom: &'_ mut Dom,
  268. edits: &mut Vec<DomEdit<'s>>,
  269. ) -> Result<()> {
  270. let trigger = self.triggers.borrow_mut().pop().expect("failed");
  271. let mut diff_machine = DiffMachine::new(
  272. edits,
  273. realdom,
  274. &self.components,
  275. trigger.originator,
  276. self.event_queue.clone(),
  277. &self.tasks,
  278. );
  279. match &trigger.event {
  280. VirtualEvent::OtherEvent => todo!(),
  281. // Nothing yet
  282. VirtualEvent::AsyncEvent { .. } => {}
  283. // Suspense Events! A component's suspended node is updated
  284. VirtualEvent::SuspenseEvent { hook_idx, domnode } => {
  285. let scope = self.components.get_mut(trigger.originator).unwrap();
  286. // safety: we are sure that there are no other references to the inner content of this hook
  287. let hook = unsafe { scope.hooks.get_mut::<SuspenseHook>(*hook_idx) }.unwrap();
  288. let cx = Context { scope, props: &() };
  289. let scx = SuspendedContext { inner: cx };
  290. // generate the new node!
  291. let nodes: Option<VNode<'s>> = (&hook.callback)(scx);
  292. let nodes = nodes.unwrap_or_else(|| errored_fragment());
  293. let nodes = scope.cur_frame().bump.alloc(nodes);
  294. // push the old node's root onto the stack
  295. diff_machine.edits.push(domnode.get());
  296. // push these new nodes onto the diff machines stack
  297. let meta = diff_machine.create(&*nodes);
  298. // replace the placeholder with the new nodes we just pushed on the stack
  299. diff_machine.edits.replace_with(meta.added_to_stack);
  300. }
  301. // This is the "meat" of our cooperative scheduler
  302. // As updates flow in, we re-evalute the event queue and decide if we should be switching the type of work
  303. //
  304. // We use the reconciler to request new IDs and then commit/uncommit the IDs when the scheduler is finished
  305. _ => {
  306. self.components
  307. .get_mut(trigger.originator)
  308. .map(|f| f.call_listener(trigger));
  309. // Now, there are events in the queue
  310. let mut updates = self.event_queue.queue.as_ref().borrow_mut();
  311. // Order the nodes by their height, we want the nodes with the smallest depth on top
  312. // This prevents us from running the same component multiple times
  313. updates.sort_unstable();
  314. log::debug!("There are: {:#?} updates to be processed", updates.len());
  315. // Iterate through the triggered nodes (sorted by height) and begin to diff them
  316. for update in updates.drain(..) {
  317. log::debug!("Running updates for: {:#?}", update);
  318. // Make sure this isn't a node we've already seen, we don't want to double-render anything
  319. // If we double-renderer something, this would cause memory safety issues
  320. if diff_machine.seen_nodes.contains(&update.idx) {
  321. continue;
  322. }
  323. // Now, all the "seen nodes" are nodes that got notified by running this listener
  324. diff_machine.seen_nodes.insert(update.idx.clone());
  325. // Start a new mutable borrow to components
  326. // We are guaranteeed that this scope is unique because we are tracking which nodes have modified
  327. let cur_component = self.components.get_mut(update.idx).unwrap();
  328. if cur_component.run_scope().is_ok() {
  329. let (old, new) = (cur_component.old_frame(), cur_component.next_frame());
  330. diff_machine.diff_node(old, new);
  331. }
  332. }
  333. }
  334. }
  335. Ok(())
  336. }
  337. pub fn base_scope(&self) -> &Scope {
  338. self.components.get(self.base_scope).unwrap()
  339. }
  340. }
  341. // TODO!
  342. // These impls are actually wrong. The DOM needs to have a mutex implemented.
  343. unsafe impl Sync for VirtualDom {}
  344. unsafe impl Send for VirtualDom {}