dioxus/packages/core/src/virtual_dom.rs

321 lines
13 KiB
Rust
Raw Normal View History

2021-05-16 06:06:02 +00:00
//! # VirtualDOM Implementation for Rust
//! This module provides the primary mechanics to create a hook-based, concurrent VDOM for Rust.
//!
//! In this file, multiple items are defined. This file is big, but should be documented well to
//! navigate the innerworkings of the Dom. We try to keep these main mechanics in this file to limit
//! the possible exposed API surface (keep fields private). This particular implementation of VDOM
//! is extremely efficient, but relies on some unsafety under the hood to do things like manage
2021-05-18 05:16:43 +00:00
//! micro-heaps for components. We are currently working on refactoring the safety out into safe(r)
//! abstractions, but current tests (MIRI and otherwise) show no issues with the current implementation.
2021-05-16 06:06:02 +00:00
//!
//! Included is:
//! - The [`VirtualDom`] itself
//! - The [`Scope`] object for mangning component lifecycle
//! - The [`ActiveFrame`] object for managing the Scope`s microheap
//! - The [`Context`] object for exposing VirtualDOM API to components
2021-07-01 18:14:59 +00:00
//! - The [`NodeFactory`] object for lazyily exposing the `Context` API to the nodebuilder API
2021-05-16 06:06:02 +00:00
//! - The [`Hook`] object for exposing state management in components.
//!
//! This module includes just the barebones for a complete VirtualDOM API.
//! Additional functionality is defined in the respective files.
2021-07-09 07:39:45 +00:00
use crate::tasks::TaskQueue;
2021-07-09 15:54:07 +00:00
use crate::{arena::SharedArena, innerlude::*};
2021-06-30 02:44:21 +00:00
use slotmap::DefaultKey;
use slotmap::SlotMap;
2021-07-09 07:39:45 +00:00
use std::{any::TypeId, fmt::Debug, rc::Rc};
2021-07-09 05:36:18 +00:00
2021-06-30 02:44:21 +00:00
pub type ScopeIdx = DefaultKey;
2021-02-03 07:26:04 +00:00
/// An integrated virtual node system that progresses events and diffs UI trees.
/// Differences are converted into patches which a renderer can use to draw the UI.
pub struct VirtualDom {
2021-02-03 07:26:04 +00:00
/// All mounted components are arena allocated to make additions, removals, and references easy to work with
2021-03-05 04:57:25 +00:00
/// A generational arena is used to re-use slots of deleted scopes without having to resize the underlying arena.
2021-05-15 16:03:08 +00:00
///
/// This is wrapped in an UnsafeCell because we will need to get mutable access to unique values in unique bump arenas
/// and rusts's guartnees cannot prove that this is safe. We will need to maintain the safety guarantees manually.
2021-07-09 15:54:07 +00:00
pub components: SharedArena,
2021-05-16 06:06:02 +00:00
/// The index of the root component
2021-05-18 05:16:43 +00:00
/// Should always be the first (gen=0, id=0)
2021-03-29 16:31:47 +00:00
pub base_scope: ScopeIdx,
2021-05-15 16:03:08 +00:00
/// All components dump their updates into a queue to be processed
2021-05-16 06:06:02 +00:00
pub(crate) event_queue: EventQueue,
2021-02-24 06:31:19 +00:00
2021-07-09 07:39:45 +00:00
pub(crate) tasks: TaskQueue,
2021-05-16 06:06:02 +00:00
/// a strong allocation to the "caller" for the original component and its props
#[doc(hidden)]
2021-07-09 15:54:07 +00:00
_root_caller: Rc<WrappedCaller>,
2021-07-09 07:39:45 +00:00
2021-06-26 01:15:33 +00:00
/// Type of the original cx. This is stored as TypeId so VirtualDom does not need to be generic.
2021-05-16 06:06:02 +00:00
///
/// Whenver props need to be updated, an Error will be thrown if the new props do not
/// match the props used to create the VirtualDom.
#[doc(hidden)]
_root_prop_type: std::any::TypeId,
2021-02-03 07:26:04 +00:00
}
2021-05-15 16:03:08 +00:00
// ======================================
2021-05-16 06:06:02 +00:00
// Public Methods for the VirtualDom
2021-05-15 16:03:08 +00:00
// ======================================
impl VirtualDom {
2021-02-03 07:26:04 +00:00
/// Create a new instance of the Dioxus Virtual Dom with no properties for the root component.
///
/// This means that the root component must either consumes its own context, or statics are used to generate the page.
/// The root component can access things like routing in its context.
2021-05-16 06:06:02 +00:00
///
/// As an end-user, you'll want to use the Renderer's "new" method instead of this method.
/// Directly creating the VirtualDOM is only useful when implementing a new renderer.
///
///
/// ```ignore
/// // Directly from a closure
///
2021-06-26 01:15:33 +00:00
/// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
2021-05-16 06:06:02 +00:00
///
/// // or pass in...
///
2021-06-26 01:15:33 +00:00
/// let root = |cx| {
/// cx.render(rsx!{
2021-05-16 06:06:02 +00:00
/// div {"hello world"}
/// })
/// }
/// let dom = VirtualDom::new(root);
///
/// // or directly from a fn
///
2021-06-26 01:15:33 +00:00
/// fn Example(cx: Context<()>) -> VNode {
/// cx.render(rsx!{ div{"hello world"} })
2021-05-16 06:06:02 +00:00
/// }
///
/// let dom = VirtualDom::new(Example);
/// ```
2021-06-23 05:44:48 +00:00
pub fn new(root: FC<()>) -> Self {
Self::new_with_props(root, ())
2021-02-03 07:26:04 +00:00
}
2021-06-26 01:15:33 +00:00
/// Start a new VirtualDom instance with a dependent cx.
2021-02-03 07:26:04 +00:00
/// Later, the props can be updated by calling "update" with a new set of props, causing a set of re-renders.
///
/// This is useful when a component tree can be driven by external state (IE SSR) but it would be too expensive
/// to toss out the entire tree.
2021-05-16 06:06:02 +00:00
///
/// ```ignore
/// // Directly from a closure
///
2021-06-26 01:15:33 +00:00
/// let dom = VirtualDom::new(|cx| cx.render(rsx!{ div {"hello world"} }));
2021-05-16 06:06:02 +00:00
///
/// // or pass in...
///
2021-06-26 01:15:33 +00:00
/// let root = |cx| {
/// cx.render(rsx!{
2021-05-16 06:06:02 +00:00
/// div {"hello world"}
/// })
/// }
/// let dom = VirtualDom::new(root);
///
/// // or directly from a fn
///
2021-06-26 01:15:33 +00:00
/// fn Example(cx: Context, props: &SomeProps) -> VNode {
/// cx.render(rsx!{ div{"hello world"} })
2021-05-16 06:06:02 +00:00
/// }
///
/// let dom = VirtualDom::new(Example);
/// ```
2021-06-23 05:44:48 +00:00
pub fn new_with_props<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
2021-07-09 15:54:07 +00:00
let components = SharedArena::new(SlotMap::new());
2021-05-16 06:06:02 +00:00
// Normally, a component would be passed as a child in the RSX macro which automatically produces OpaqueComponents
// Here, we need to make it manually, using an RC to force the Weak reference to stick around for the main scope.
2021-07-09 15:54:07 +00:00
let _root_caller: Rc<WrappedCaller> = Rc::new(move |scope| {
2021-06-07 18:14:49 +00:00
// let _root_caller: Rc<OpaqueComponent<'static>> = Rc::new(move |scope| {
2021-06-02 15:07:30 +00:00
// the lifetime of this closure is just as long as the lifetime on the scope reference
// this closure moves root props (which is static) into this closure
let props = unsafe { &*(&root_props as *const _) };
root(Context {
props,
scope,
tasks: todo!(),
})
});
2021-05-16 06:06:02 +00:00
// Create a weak reference to the OpaqueComponent for the root scope to use as its render function
let caller_ref = Rc::downgrade(&_root_caller);
// Build a funnel for hooks to send their updates into. The `use_hook` method will call into the update funnel.
let event_queue = EventQueue::default();
let _event_queue = event_queue.clone();
// Make the first scope
// We don't run the component though, so renderers will need to call "rebuild" when they initialize their DOM
2021-05-18 05:16:43 +00:00
let link = components.clone();
2021-06-07 18:14:49 +00:00
2021-05-16 06:06:02 +00:00
let base_scope = components
2021-05-18 05:16:43 +00:00
.with(|arena| {
2021-06-30 02:44:21 +00:00
arena.insert_with_key(move |myidx| {
2021-06-07 18:14:49 +00:00
let event_channel = _event_queue.new_channel(0, myidx);
2021-06-03 17:57:41 +00:00
Scope::new(caller_ref, myidx, None, 0, event_channel, link, &[])
2021-05-18 05:16:43 +00:00
})
})
.unwrap();
2021-02-07 22:38:17 +00:00
2021-03-11 17:27:01 +00:00
Self {
2021-05-16 06:06:02 +00:00
_root_caller,
base_scope,
event_queue,
2021-05-18 05:16:43 +00:00
components,
2021-07-09 07:39:45 +00:00
tasks: TaskQueue::new(),
2021-03-11 17:27:01 +00:00
_root_prop_type: TypeId::of::<P>(),
}
}
}
2021-02-03 07:26:04 +00:00
// ======================================
// Private Methods for the VirtualDom
// ======================================
impl VirtualDom {
2021-05-18 05:16:43 +00:00
/// Performs a *full* rebuild of the virtual dom, returning every edit required to generate the actual dom rom scratch
2021-06-03 17:57:41 +00:00
/// Currently this doesn't do what we want it to do
pub fn rebuild<'s, Dom: RealDom<'s>>(&'s mut self, realdom: &mut Dom) -> Result<()> {
2021-06-07 18:14:49 +00:00
let mut diff_machine = DiffMachine::new(
2021-06-20 06:16:42 +00:00
realdom,
&self.components,
2021-06-07 18:14:49 +00:00
self.base_scope,
self.event_queue.clone(),
);
2021-06-07 18:14:49 +00:00
// Schedule an update and then immediately call it on the root component
// This is akin to a hook being called from a listener and requring a re-render
// Instead, this is done on top-level component
let base = self.components.try_get(self.base_scope)?;
2021-06-03 17:57:41 +00:00
2021-06-07 18:14:49 +00:00
let update = &base.event_channel;
update();
2021-05-16 06:06:02 +00:00
2021-06-20 06:16:42 +00:00
self.progress_completely(&mut diff_machine)?;
2021-03-05 20:02:36 +00:00
Ok(())
2021-02-24 08:51:26 +00:00
}
2021-02-12 21:11:33 +00:00
/// This method is the most sophisticated way of updating the virtual dom after an external event has been triggered.
///
/// Given a synthetic event, the component that triggered the event, and the index of the callback, this runs the virtual
/// dom to completion, tagging components that need updates, compressing events together, and finally emitting a single
/// change list.
///
/// If implementing an external renderer, this is the perfect method to combine with an async event loop that waits on
2021-05-15 16:03:08 +00:00
/// listeners, something like this:
///
/// ```ignore
/// while let Ok(event) = receiver.recv().await {
/// let edits = self.internal_dom.progress_with_event(event)?;
/// for edit in &edits {
/// patch_machine.handle_edit(edit);
/// }
/// }
/// ```
2021-02-12 21:11:33 +00:00
///
2021-02-24 06:31:19 +00:00
/// Note: this method is not async and does not provide suspense-like functionality. It is up to the renderer to provide the
/// executor and handlers for suspense as show in the example.
2021-02-12 21:11:33 +00:00
///
2021-02-24 06:31:19 +00:00
/// ```ignore
/// let (sender, receiver) = channel::new();
/// sender.send(EventTrigger::start());
2021-02-12 21:11:33 +00:00
///
2021-02-24 06:31:19 +00:00
/// let mut dom = VirtualDom::new();
/// dom.suspense_handler(|event| sender.send(event));
2021-02-12 21:11:33 +00:00
///
2021-02-24 06:31:19 +00:00
/// while let Ok(diffs) = dom.progress_with_event(receiver.recv().await) {
/// render(diffs);
/// }
2021-02-12 21:11:33 +00:00
///
/// ```
2021-05-15 16:03:08 +00:00
//
// Developer notes:
// ----
// This method has some pretty complex safety guarantees to uphold.
// We interact with bump arenas, raw pointers, and use UnsafeCell to get a partial borrow of the arena.
// The final EditList has edits that pull directly from the Bump Arenas which add significant complexity
// in crafting a 100% safe solution with traditional lifetimes. Consider this method to be internally unsafe
// but the guarantees provide a safe, fast, and efficient abstraction for the VirtualDOM updating framework.
2021-05-16 06:06:02 +00:00
//
// A good project would be to remove all unsafe from this crate and move the unsafety into safer abstractions.
pub fn progress_with_event<'s, Dom: RealDom<'s>>(
&'s mut self,
realdom: &'_ mut Dom,
2021-06-23 05:44:48 +00:00
trigger: EventTrigger,
) -> Result<()> {
2021-07-09 07:39:45 +00:00
let id = trigger.originator.clone();
2021-05-15 16:03:08 +00:00
2021-06-23 05:44:48 +00:00
self.components.try_get_mut(id)?.call_listener(trigger)?;
let mut diff_machine =
DiffMachine::new(realdom, &self.components, id, self.event_queue.clone());
2021-06-07 18:14:49 +00:00
2021-06-20 06:16:42 +00:00
self.progress_completely(&mut diff_machine)?;
2021-05-16 06:06:02 +00:00
Ok(())
2021-05-16 06:06:02 +00:00
}
/// Consume the event queue, descending depth-first.
/// Only ever run each component once.
///
/// The DiffMachine logs its progress as it goes which might be useful for certain types of renderers.
pub(crate) fn progress_completely<'a, 'bump, Dom: RealDom<'bump>>(
&'bump self,
diff_machine: &'_ mut DiffMachine<'a, 'bump, Dom>,
) -> Result<()> {
2021-05-15 16:03:08 +00:00
// Now, there are events in the queue
2021-07-09 15:54:07 +00:00
let mut updates = self.event_queue.queue.as_ref().borrow_mut();
2021-05-15 16:03:08 +00:00
2021-06-17 03:37:55 +00:00
// Order the nodes by their height, we want the nodes with the smallest depth on top
2021-05-15 16:03:08 +00:00
// This prevents us from running the same component multiple times
updates.sort_unstable();
2021-06-07 18:14:49 +00:00
log::debug!("There are: {:#?} updates to be processed", updates.len());
2021-05-15 16:03:08 +00:00
// Iterate through the triggered nodes (sorted by height) and begin to diff them
for update in updates.drain(..) {
2021-06-07 18:14:49 +00:00
log::debug!("Running updates for: {:#?}", update);
2021-07-09 15:54:07 +00:00
2021-05-15 16:03:08 +00:00
// Make sure this isn't a node we've already seen, we don't want to double-render anything
// If we double-renderer something, this would cause memory safety issues
2021-06-07 18:14:49 +00:00
if diff_machine.seen_nodes.contains(&update.idx) {
2021-05-15 16:03:08 +00:00
continue;
}
2021-03-29 16:31:47 +00:00
2021-05-15 16:03:08 +00:00
// Now, all the "seen nodes" are nodes that got notified by running this listener
2021-06-07 18:14:49 +00:00
diff_machine.seen_nodes.insert(update.idx.clone());
2021-05-15 16:03:08 +00:00
// Start a new mutable borrow to components
// We are guaranteeed that this scope is unique because we are tracking which nodes have modified
2021-05-19 14:14:02 +00:00
let cur_component = self.components.try_get_mut(update.idx).unwrap();
2021-05-15 16:03:08 +00:00
cur_component.run_scope()?;
2021-05-15 16:03:08 +00:00
let (old, new) = (cur_component.old_frame(), cur_component.next_frame());
2021-06-20 06:16:42 +00:00
diff_machine.diff_node(old, new);
2021-05-15 16:03:08 +00:00
2021-06-07 18:14:49 +00:00
// log::debug!(
// "Processing update: {:#?} with height {}",
// &update.idx,
// cur_height
// );
2021-05-15 16:03:08 +00:00
}
2021-05-16 06:06:02 +00:00
Ok(())
2021-05-15 16:03:08 +00:00
}
2021-06-15 14:02:46 +00:00
pub fn base_scope(&self) -> &Scope {
2021-06-26 07:06:29 +00:00
let idx = self.base_scope;
self.components.try_get(idx).unwrap()
2021-06-15 14:02:46 +00:00
}
2021-04-05 01:47:53 +00:00
}
// TODO!
// These impls are actually wrong. The DOM needs to have a mutex implemented.
unsafe impl Sync for VirtualDom {}
unsafe impl Send for VirtualDom {}