dioxus/packages/core/src/virtual_dom.rs

432 lines
17 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-30 21:04:04 +00:00
use futures_util::StreamExt;
2021-07-27 15:28:05 +00:00
use crate::hooks::{SuspendedContext, SuspenseHook};
use crate::{arena::SharedResources, innerlude::*};
2021-07-13 20:48:47 +00:00
use std::any::Any;
2021-07-13 20:48:47 +00:00
use std::any::TypeId;
2021-07-29 22:04:09 +00:00
use std::cell::{Ref, RefCell, RefMut};
use std::pin::Pin;
2021-07-09 05:36:18 +00:00
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.
pub shared: SharedResources,
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)
pub base_scope: ScopeId,
2021-03-29 16:31:47 +00:00
pub triggers: RefCell<Vec<EventTrigger>>,
// for managing the props that were used to create the dom
#[doc(hidden)]
_root_prop_type: std::any::TypeId,
#[doc(hidden)]
_root_props: std::pin::Pin<Box<dyn std::any::Any>>,
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-07-18 16:39:32 +00:00
/// fn Example(cx: Context<()>) -> DomTree {
2021-06-26 01:15:33 +00:00
/// 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 {
let components = SharedResources::new();
let root_props: Pin<Box<dyn Any>> = Box::pin(root_props);
let props_ptr = root_props.as_ref().downcast_ref::<P>().unwrap() as *const P;
2021-05-16 06:06:02 +00:00
2021-05-18 05:16:43 +00:00
let link = components.clone();
2021-06-07 18:14:49 +00:00
let base_scope = components.insert_scope_with_key(move |myidx| {
let caller = NodeFactory::create_component_caller(root, props_ptr as *const _);
2021-07-26 16:14:48 +00:00
Scope::new(caller, myidx, None, 0, ScopeChildren(&[]), link)
});
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
base_scope,
_root_props: root_props,
shared: components,
triggers: Default::default(),
2021-03-11 17:27:01 +00:00
_root_prop_type: TypeId::of::<P>(),
}
}
2021-02-03 07:26:04 +00:00
2021-07-11 21:24:47 +00:00
pub fn launch_in_place(root: FC<()>) -> Self {
let mut s = Self::new(root);
s.rebuild_in_place();
s
}
/// Creates a new virtualdom and immediately rebuilds it in place, not caring about the RealDom to write into.
///
pub fn launch_with_props_in_place<P: Properties + 'static>(root: FC<P>, root_props: P) -> Self {
let mut s = Self::new_with_props(root, root_props);
s.rebuild_in_place();
s
}
/// Rebuilds the VirtualDOM from scratch, but uses a "dummy" RealDom.
///
/// Used in contexts where a real copy of the structure doesn't matter, and the VirtualDOM is the source of truth.
///
/// ## Why?
///
/// This method uses the `DebugDom` under the hood - essentially making the VirtualDOM's diffing patches a "no-op".
///
/// SSR takes advantage of this by using Dioxus itself as the source of truth, and rendering from the tree directly.
2021-07-30 20:07:42 +00:00
pub fn rebuild_in_place(&mut self) -> Result<Vec<DomEdit>> {
let mut realdom = DebugDom::new();
let mut edits = Vec::new();
2021-07-30 20:07:42 +00:00
self.rebuild(&mut realdom, &mut edits)?;
Ok(edits)
}
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
///
/// The diff machine expects the RealDom's stack to be the root of the application
2021-07-23 21:03:51 +00:00
pub fn rebuild<'s>(
&'s mut self,
realdom: &'_ mut dyn RealDom<'s>,
edits: &'_ mut Vec<DomEdit<'s>>,
) -> Result<()> {
let mut diff_machine = DiffMachine::new(edits, realdom, self.base_scope, &self.shared);
let cur_component = diff_machine
.get_scope_mut(&self.base_scope)
.expect("The base scope should never be moved");
2021-06-03 17:57:41 +00:00
2021-07-20 23:03:49 +00:00
// We run the component. If it succeeds, then we can diff it and add the changes to the dom.
if cur_component.run_scope().is_ok() {
2021-07-29 22:04:09 +00:00
let meta = diff_machine.create_vnode(cur_component.frames.fin_head());
diff_machine.edit_append_children(meta.added_to_stack);
} else {
// todo: should this be a hard error?
log::warn!(
"Component failed to run succesfully during rebuild.
This does not result in a failed rebuild, but indicates a logic failure within your app."
);
2021-07-20 23:03:49 +00:00
}
2021-05-16 06:06:02 +00:00
Ok(())
}
2021-03-05 20:02:36 +00:00
///
///
///
///
///
pub fn queue_event(&self, trigger: EventTrigger) {
let mut triggers = self.triggers.borrow_mut();
triggers.push(trigger);
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 async fn progress_with_event<'a, 's>(
&'s mut self,
realdom: &'a mut dyn RealDom<'s>,
edits: &'a mut Vec<DomEdit<'s>>,
) -> Result<()> {
let trigger = self.triggers.borrow_mut().pop().expect("failed");
let mut diff_machine = DiffMachine::new(edits, realdom, trigger.originator, &self.shared);
2021-06-07 18:14:49 +00:00
match &trigger.event {
2021-07-29 22:04:09 +00:00
// When a scope gets destroyed during a diff, it gets its own garbage collection event
// However, an old scope might be attached
VirtualEvent::GarbageCollection => {
let scope = diff_machine.get_scope_mut(&trigger.originator).unwrap();
let mut garbage_list = scope.consume_garbage();
while let Some(node) = garbage_list.pop() {
match &node.kind {
VNodeKind::Text(_) => {
//
self.shared.collect_garbage(node.direct_id())
}
VNodeKind::Anchor(anchor) => {
//
}
VNodeKind::Element(el) => {
self.shared.collect_garbage(node.direct_id());
for child in el.children {
garbage_list.push(child);
}
}
VNodeKind::Fragment(frag) => {
for child in frag.children {
garbage_list.push(child);
}
}
VNodeKind::Component(comp) => {
// run the destructors
todo!();
}
VNodeKind::Suspended(node) => {
// make sure the task goes away
todo!();
}
}
}
}
2021-07-15 03:18:02 +00:00
// Nothing yet
VirtualEvent::AsyncEvent { .. } => {}
// Suspense Events! A component's suspended node is updated
2021-07-15 04:40:37 +00:00
VirtualEvent::SuspenseEvent { hook_idx, domnode } => {
// Safety: this handler is the only thing that can mutate shared items at this moment in tim
let scope = diff_machine.get_scope_mut(&trigger.originator).unwrap();
2021-07-15 03:18:02 +00:00
// safety: we are sure that there are no other references to the inner content of suspense hooks
2021-07-15 03:18:02 +00:00
let hook = unsafe { scope.hooks.get_mut::<SuspenseHook>(*hook_idx) }.unwrap();
2021-07-15 04:40:37 +00:00
let cx = Context { scope, props: &() };
2021-07-18 16:39:32 +00:00
let scx = SuspendedContext { inner: cx };
2021-07-15 03:18:02 +00:00
// generate the new node!
2021-07-18 16:39:32 +00:00
let nodes: Option<VNode<'s>> = (&hook.callback)(scx);
match nodes {
None => {
log::warn!("Suspense event came through, but there was no mounted node to update >:(");
}
Some(nodes) => {
2021-07-26 16:14:48 +00:00
todo!("using the wrong frame");
let nodes = scope.frames.finished_frame().bump.alloc(nodes);
2021-07-15 03:18:02 +00:00
// push the old node's root onto the stack
let real_id = domnode.get().ok_or(Error::NotMounted)?;
2021-07-29 22:04:09 +00:00
diff_machine.edit_push_root(real_id);
2021-07-15 03:18:02 +00:00
// push these new nodes onto the diff machines stack
2021-07-29 22:04:09 +00:00
let meta = diff_machine.create_vnode(&*nodes);
2021-07-15 03:18:02 +00:00
// replace the placeholder with the new nodes we just pushed on the stack
2021-07-29 22:56:09 +00:00
diff_machine.edit_replace_with(1, meta.added_to_stack);
}
}
2021-07-15 03:18:02 +00:00
}
2021-05-16 06:06:02 +00:00
// This is the "meat" of our cooperative scheduler
// As updates flow in, we re-evalute the event queue and decide if we should be switching the type of work
//
// We use the reconciler to request new IDs and then commit/uncommit the IDs when the scheduler is finished
_ => {
diff_machine
.get_scope_mut(&trigger.originator)
2021-07-18 07:54:42 +00:00
.map(|f| f.call_listener(trigger));
2021-05-15 16:03:08 +00:00
// Now, there are events in the queue
let mut updates = self.shared.borrow_queue();
2021-05-15 16:03:08 +00:00
// Order the nodes by their height, we want the nodes with the smallest depth on top
// 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-07-09 15:54:07 +00:00
// Iterate through the triggered nodes (sorted by height) and begin to diff them
for update in updates.drain(..) {
log::debug!("Running updates for: {:#?}", update);
2021-03-29 16:31:47 +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-07-29 22:04:09 +00:00
if diff_machine.seen_scopes.contains(&update.idx) {
2021-07-26 16:14:48 +00:00
log::debug!("Skipping update for: {:#?}", update);
continue;
}
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 in the diff machine
let cur_component = diff_machine
.get_scope_mut(&update.idx)
.expect("Failed to find scope or borrow would be aliasing");
2021-05-15 16:03:08 +00:00
2021-07-26 16:14:48 +00:00
// Now, all the "seen nodes" are nodes that got notified by running this listener
2021-07-29 22:04:09 +00:00
diff_machine.seen_scopes.insert(update.idx.clone());
2021-07-26 16:14:48 +00:00
2021-07-20 23:03:49 +00:00
if cur_component.run_scope().is_ok() {
2021-07-26 16:14:48 +00:00
let (old, new) = (
cur_component.frames.wip_head(),
cur_component.frames.fin_head(),
);
2021-07-20 23:03:49 +00:00
diff_machine.diff_node(old, new);
}
}
}
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
2021-07-30 21:04:04 +00:00
pub async fn wait_for_event(&mut self) -> Option<EventTrigger> {
let r = self.shared.tasks.clone();
let mut r = r.borrow_mut();
let gh = r.next().await;
gh
}
pub fn any_pending_events(&self) -> bool {
let r = self.shared.tasks.clone();
let r = r.borrow();
!r.is_empty()
}
2021-06-15 14:02:46 +00:00
pub fn base_scope(&self) -> &Scope {
unsafe { self.shared.get_scope(self.base_scope).unwrap() }
2021-06-15 14:02:46 +00:00
}
pub fn get_scope(&self, id: ScopeId) -> Option<&Scope> {
unsafe { self.shared.get_scope(id) }
}
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 {}