virtual_dom.rs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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::innerlude::*;
  22. use futures_util::{pin_mut, Future, FutureExt};
  23. use std::{
  24. any::{Any, TypeId},
  25. pin::Pin,
  26. };
  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. ///
  30. ///
  31. ///
  32. ///
  33. ///
  34. ///
  35. ///
  36. pub struct VirtualDom {
  37. /// All mounted components are arena allocated to make additions, removals, and references easy to work with
  38. /// A generational arena is used to re-use slots of deleted scopes without having to resize the underlying arena.
  39. ///
  40. /// This is wrapped in an UnsafeCell because we will need to get mutable access to unique values in unique bump arenas
  41. /// and rusts's guartnees cannot prove that this is safe. We will need to maintain the safety guarantees manually.
  42. pub shared: SharedResources,
  43. /// The index of the root component
  44. /// Should always be the first (gen=0, id=0)
  45. base_scope: ScopeId,
  46. scheduler: Scheduler,
  47. // for managing the props that were used to create the dom
  48. #[doc(hidden)]
  49. _root_prop_type: std::any::TypeId,
  50. #[doc(hidden)]
  51. _root_props: std::pin::Pin<Box<dyn std::any::Any>>,
  52. }
  53. impl VirtualDom {
  54. /// Create a new VirtualDOM with a component that does not have special props.
  55. ///
  56. /// # Description
  57. ///
  58. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  59. ///
  60. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  61. /// to toss out the entire tree.
  62. ///
  63. ///
  64. /// # Example
  65. /// ```
  66. /// fn Example(cx: Context<SomeProps>) -> VNode {
  67. /// cx.render(rsx!{ div{"hello world"} })
  68. /// }
  69. ///
  70. /// let dom = VirtualDom::new(Example);
  71. /// ```
  72. ///
  73. /// Note: the VirtualDOM is not progressed, you must either "run_with_deadline" or use "rebuild" to progress it.
  74. pub fn new(root: FC<()>) -> Self {
  75. Self::new_with_props(root, ())
  76. }
  77. /// Create a new VirtualDOM with the given properties for the root component.
  78. ///
  79. /// # Description
  80. ///
  81. /// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
  82. ///
  83. /// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
  84. /// to toss out the entire tree.
  85. ///
  86. ///
  87. /// # Example
  88. /// ```
  89. /// fn Example(cx: Context<SomeProps>) -> VNode {
  90. /// cx.render(rsx!{ div{"hello world"} })
  91. /// }
  92. ///
  93. /// let dom = VirtualDom::new(Example);
  94. /// ```
  95. ///
  96. /// Note: the VirtualDOM is not progressed, you must either "run_with_deadline" or use "rebuild" to progress it.
  97. pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
  98. let components = SharedResources::new();
  99. let root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
  100. let props_ptr = root_props.as_ref().downcast_ref::<P>().unwrap() as *const P;
  101. let link = components.clone();
  102. let base_scope = components.insert_scope_with_key(move |myidx| {
  103. let caller = NodeFactory::create_component_caller(root, props_ptr as *const _);
  104. let name = type_name_of(root);
  105. Scope::new(caller, myidx, None, 0, ScopeChildren(&[]), link, name)
  106. });
  107. Self {
  108. base_scope,
  109. _root_props: root_props,
  110. scheduler: Scheduler::new(components.clone()),
  111. shared: components,
  112. _root_prop_type: TypeId::of::<P>(),
  113. }
  114. }
  115. pub fn base_scope(&self) -> &Scope {
  116. self.shared.get_scope(self.base_scope).unwrap()
  117. }
  118. pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> {
  119. self.shared.get_scope(id)
  120. }
  121. /// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
  122. ///
  123. /// The diff machine expects the RealDom's stack to be the root of the application
  124. ///
  125. /// Events like garabge collection, application of refs, etc are not handled by this method and can only be progressed
  126. /// through "run". We completely avoid the task scheduler infrastructure.
  127. pub fn rebuild<'s>(&'s mut self) -> Mutations<'s> {
  128. let mut fut = self.rebuild_async().boxed_local();
  129. loop {
  130. if let Some(edits) = (&mut fut).now_or_never() {
  131. break edits;
  132. }
  133. }
  134. }
  135. /// Rebuild the dom from the ground up
  136. ///
  137. /// This method is asynchronous to prevent the application from blocking while the dom is being rebuilt. Computing
  138. /// the diff and creating nodes can be expensive, so we provide this method to avoid blocking the main thread. This
  139. /// method can be useful when needing to perform some crucial periodic tasks.
  140. pub async fn rebuild_async<'s>(&'s mut self) -> Mutations<'s> {
  141. let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared);
  142. let cur_component = self
  143. .shared
  144. .get_scope_mut(self.base_scope)
  145. .expect("The base scope should never be moved");
  146. // // We run the component. If it succeeds, then we can diff it and add the changes to the dom.
  147. if cur_component.run_scope().is_ok() {
  148. diff_machine
  149. .stack
  150. .create_node(cur_component.frames.fin_head(), MountType::Append);
  151. diff_machine.work().await;
  152. } else {
  153. // todo: should this be a hard error?
  154. log::warn!(
  155. "Component failed to run succesfully during rebuild.
  156. This does not result in a failed rebuild, but indicates a logic failure within your app."
  157. );
  158. }
  159. diff_machine.mutations
  160. }
  161. pub fn diff_sync<'s>(&'s mut self) -> Mutations<'s> {
  162. let mut fut = self.diff_async().boxed_local();
  163. loop {
  164. if let Some(edits) = (&mut fut).now_or_never() {
  165. break edits;
  166. }
  167. }
  168. }
  169. pub async fn diff_async<'s>(&'s mut self) -> Mutations<'s> {
  170. let mut diff_machine = DiffMachine::new(Mutations::new(), self.base_scope, &self.shared);
  171. let cur_component = self
  172. .shared
  173. .get_scope_mut(self.base_scope)
  174. .expect("The base scope should never be moved");
  175. cur_component.run_scope().unwrap();
  176. diff_machine.stack.push(DiffInstruction::DiffNode {
  177. old: cur_component.frames.wip_head(),
  178. new: cur_component.frames.fin_head(),
  179. });
  180. diff_machine.work().await;
  181. diff_machine.mutations
  182. }
  183. /// Runs the virtualdom immediately, not waiting for any suspended nodes to complete.
  184. ///
  185. /// This method will not wait for any suspended nodes to complete.
  186. pub fn run_immediate<'s>(&'s mut self) -> Mutations<'s> {
  187. todo!()
  188. // use futures_util::FutureExt;
  189. // let mut is_ready = || false;
  190. // self.run_with_deadline(futures_util::future::ready(()), &mut is_ready)
  191. // .now_or_never()
  192. // .expect("this future will always resolve immediately")
  193. }
  194. /// Runs the virtualdom with no time limit.
  195. ///
  196. /// If there are pending tasks, they will be progressed before returning. This is useful when rendering an application
  197. /// that has suspended nodes or suspended tasks. Be warned - any async tasks running forever will prevent this method
  198. /// from completing. Consider using `run` and specifing a deadline.
  199. pub async fn run_unbounded<'s>(&'s mut self) -> Mutations<'s> {
  200. self.run_with_deadline(async {}).await.unwrap()
  201. }
  202. /// Run the virtualdom with a deadline.
  203. ///
  204. /// This method will progress async tasks until the deadline is reached. If tasks are completed before the deadline,
  205. /// and no tasks are pending, this method will return immediately. If tasks are still pending, then this method will
  206. /// exhaust the deadline working on them.
  207. ///
  208. /// This method is useful when needing to schedule the virtualdom around other tasks on the main thread to prevent
  209. /// "jank". It will try to finish whatever work it has by the deadline to free up time for other work.
  210. ///
  211. /// Due to platform differences in how time is handled, this method accepts a future that resolves when the deadline
  212. /// is exceeded. However, the deadline won't be met precisely, so you might want to build some wiggle room into the
  213. /// deadline closure manually.
  214. ///
  215. /// The deadline is polled before starting to diff components. This strikes a balance between the overhead of checking
  216. /// the deadline and just completing the work. However, if an individual component takes more than 16ms to render, then
  217. /// the screen will "jank" up. In debug, this will trigger an alert.
  218. ///
  219. /// If there are no in-flight fibers when this method is called, it will await any possible tasks, aborting early if
  220. /// the provided deadline future resolves.
  221. ///
  222. /// For use in the web, it is expected that this method will be called to be executed during "idle times" and the
  223. /// mutations to be applied during the "paint times" IE "animation frames". With this strategy, it is possible to craft
  224. /// entirely jank-free applications that perform a ton of work.
  225. ///
  226. /// # Example
  227. ///
  228. /// ```no_run
  229. /// static App: FC<()> = |cx| rsx!(in cx, div {"hello"} );
  230. /// let mut dom = VirtualDom::new(App);
  231. /// loop {
  232. /// let deadline = TimeoutFuture::from_ms(16);
  233. /// let mutations = dom.run_with_deadline(deadline).await;
  234. /// apply_mutations(mutations);
  235. /// }
  236. /// ```
  237. ///
  238. /// ## Mutations
  239. ///
  240. /// This method returns "mutations" - IE the necessary changes to get the RealDOM to match the VirtualDOM. It also
  241. /// includes a list of NodeRefs that need to be applied and effects that need to be triggered after the RealDOM has
  242. /// applied the edits.
  243. ///
  244. /// Mutations are the only link between the RealDOM and the VirtualDOM.
  245. pub async fn run_with_deadline<'s>(
  246. &'s mut self,
  247. deadline: impl Future<Output = ()>,
  248. ) -> Option<Mutations<'s>> {
  249. let mut committed_mutations = Mutations::new();
  250. let mut deadline = Box::pin(deadline.fuse());
  251. // TODO:
  252. // the scheduler uses a bunch of different receivers to mimic a "topic" queue system. The futures-channel implementation
  253. // doesn't really have a concept of a "topic" queue, so there's a lot of noise in the hand-rolled scheduler. We should
  254. // explore abstracting the scheduler into a topic-queue channel system - similar to Kafka or something similar.
  255. loop {
  256. // Internalize any pending work since the last time we ran
  257. self.scheduler.manually_poll_events();
  258. // Wait for any new events if we have nothing to do
  259. if !self.scheduler.has_any_work() {
  260. self.scheduler.clean_up_garbage();
  261. let deadline_expired = self.scheduler.wait_for_any_trigger(&mut deadline).await;
  262. if deadline_expired {
  263. return Some(committed_mutations);
  264. }
  265. }
  266. // Create work from the pending event queue
  267. self.scheduler.consume_pending_events();
  268. // Work through the current subtree, and commit the results when it finishes
  269. // When the deadline expires, give back the work
  270. match self.scheduler.work_with_deadline(&mut deadline) {
  271. FiberResult::Done(mut mutations) => {
  272. committed_mutations.extend(&mut mutations);
  273. /*
  274. quick return if there's no work left, so we can commit before the deadline expires
  275. When we loop over again, we'll re-wait for any new work.
  276. I'm not quite sure how this *should* work.
  277. It makes sense to try and progress the DOM faster
  278. */
  279. if !self.scheduler.has_any_work() {
  280. return Some(committed_mutations);
  281. }
  282. }
  283. FiberResult::Interrupted => return None,
  284. }
  285. }
  286. }
  287. pub fn get_event_sender(&self) -> futures_channel::mpsc::UnboundedSender<EventTrigger> {
  288. self.shared.ui_event_sender.clone()
  289. }
  290. pub fn has_work(&self) -> bool {
  291. true
  292. }
  293. pub async fn wait_for_any_work(&self) {}
  294. }
  295. // TODO!
  296. // These impls are actually wrong. The DOM needs to have a mutex implemented.
  297. unsafe impl Sync for VirtualDom {}
  298. unsafe impl Send for VirtualDom {}