2021-07-15 08:09:28 +00:00
|
|
|
use crate::innerlude::*;
|
2021-07-29 22:04:09 +00:00
|
|
|
use fxhash::FxHashSet;
|
2021-07-09 05:42:26 +00:00
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
2021-09-01 04:57:04 +00:00
|
|
|
cell::RefCell,
|
|
|
|
collections::HashMap,
|
2021-09-13 04:59:08 +00:00
|
|
|
fmt::Formatter,
|
2021-07-09 05:42:26 +00:00
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
2021-07-11 18:49:52 +00:00
|
|
|
rc::Rc,
|
2021-07-09 05:42:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Every component in Dioxus is represented by a `Scope`.
|
|
|
|
///
|
|
|
|
/// Scopes contain the state for hooks, the component's props, and other lifecycle information.
|
|
|
|
///
|
|
|
|
/// Scopes are allocated in a generational arena. As components are mounted/unmounted, they will replace slots of dead components.
|
|
|
|
/// The actual contents of the hooks, though, will be allocated with the standard allocator. These should not allocate as frequently.
|
2021-07-15 08:09:28 +00:00
|
|
|
///
|
|
|
|
/// We expose the `Scope` type so downstream users can traverse the Dioxus VirtualDOM for whatever
|
|
|
|
/// usecase they might have.
|
2021-07-09 05:42:26 +00:00
|
|
|
pub struct Scope {
|
2021-07-23 14:27:43 +00:00
|
|
|
// Book-keeping about our spot in the arena
|
2021-07-15 07:38:09 +00:00
|
|
|
pub(crate) parent_idx: Option<ScopeId>,
|
|
|
|
pub(crate) our_arena_idx: ScopeId,
|
|
|
|
pub(crate) height: u32,
|
2021-07-29 22:04:09 +00:00
|
|
|
pub(crate) descendents: RefCell<FxHashSet<ScopeId>>,
|
2021-07-09 05:42:26 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Nodes
|
2021-07-09 05:42:26 +00:00
|
|
|
// an internal, highly efficient storage of vnodes
|
2021-07-29 22:04:09 +00:00
|
|
|
// lots of safety condsiderations
|
2021-07-15 07:38:09 +00:00
|
|
|
pub(crate) frames: ActiveFrame,
|
2021-09-13 04:59:08 +00:00
|
|
|
pub(crate) caller: *const dyn for<'b> Fn(&'b Scope) -> DomTree<'b>,
|
2021-07-26 16:14:48 +00:00
|
|
|
pub(crate) child_nodes: ScopeChildren<'static>,
|
2021-07-09 05:42:26 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// Listeners
|
2021-07-24 04:29:23 +00:00
|
|
|
pub(crate) listeners: RefCell<Vec<*const Listener<'static>>>,
|
2021-08-08 19:15:16 +00:00
|
|
|
pub(crate) borrowed_props: RefCell<Vec<*const VComponent<'static>>>,
|
2021-09-01 19:45:53 +00:00
|
|
|
pub(crate) suspended_nodes: RefCell<HashMap<u64, *const VSuspended<'static>>>,
|
2021-08-25 20:40:18 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
// State
|
|
|
|
pub(crate) hooks: HookList,
|
|
|
|
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
|
2021-07-09 05:42:26 +00:00
|
|
|
|
2021-08-25 19:54:33 +00:00
|
|
|
pub(crate) memoized_updater: Rc<dyn Fn() + 'static>,
|
|
|
|
|
|
|
|
pub(crate) shared: EventChannel,
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
// The type of closure that wraps calling components
|
|
|
|
|
2021-08-25 20:40:18 +00:00
|
|
|
/// The type of task that gets sent to the task scheduler
|
|
|
|
/// Submitting a fiber task returns a handle to that task, which can be used to wake up suspended nodes
|
|
|
|
pub type FiberTask = Pin<Box<dyn Future<Output = ScopeId>>>;
|
2021-07-14 06:04:19 +00:00
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
impl Scope {
|
|
|
|
// we are being created in the scope of an existing component (where the creator_node lifetime comes into play)
|
|
|
|
// we are going to break this lifetime by force in order to save it on ourselves.
|
|
|
|
// To make sure that the lifetime isn't truly broken, we receive a Weak RC so we can't keep it around after the parent dies.
|
|
|
|
// This should never happen, but is a good check to keep around
|
|
|
|
//
|
|
|
|
// Scopes cannot be made anywhere else except for this file
|
|
|
|
// Therefore, their lifetimes are connected exclusively to the virtual dom
|
2021-09-02 04:10:09 +00:00
|
|
|
pub(crate) fn new<'creator_node>(
|
2021-09-13 04:59:08 +00:00
|
|
|
caller: &'creator_node dyn for<'b> Fn(&'b Scope) -> DomTree<'b>,
|
2021-07-15 07:38:09 +00:00
|
|
|
arena_idx: ScopeId,
|
|
|
|
parent: Option<ScopeId>,
|
2021-07-09 05:42:26 +00:00
|
|
|
height: u32,
|
2021-07-26 16:14:48 +00:00
|
|
|
child_nodes: ScopeChildren,
|
2021-08-25 19:54:33 +00:00
|
|
|
shared: EventChannel,
|
2021-07-09 05:42:26 +00:00
|
|
|
) -> Self {
|
2021-07-26 16:14:48 +00:00
|
|
|
let child_nodes = unsafe { child_nodes.extend_lifetime() };
|
|
|
|
|
2021-08-25 19:54:33 +00:00
|
|
|
let up = shared.schedule_any_immediate.clone();
|
|
|
|
let memoized_updater = Rc::new(move || up(arena_idx));
|
2021-07-29 22:04:09 +00:00
|
|
|
|
2021-09-13 04:59:08 +00:00
|
|
|
let caller = caller as *const _;
|
|
|
|
let caller = unsafe { std::mem::transmute(caller) };
|
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
Self {
|
2021-08-25 19:54:33 +00:00
|
|
|
memoized_updater,
|
|
|
|
shared,
|
2021-07-09 05:42:26 +00:00
|
|
|
child_nodes,
|
|
|
|
caller,
|
2021-07-15 07:38:09 +00:00
|
|
|
parent_idx: parent,
|
|
|
|
our_arena_idx: arena_idx,
|
2021-07-09 05:42:26 +00:00
|
|
|
height,
|
|
|
|
frames: ActiveFrame::new(),
|
|
|
|
hooks: Default::default(),
|
2021-08-25 20:40:18 +00:00
|
|
|
suspended_nodes: Default::default(),
|
2021-07-09 05:42:26 +00:00
|
|
|
shared_contexts: Default::default(),
|
|
|
|
listeners: Default::default(),
|
2021-08-08 19:15:16 +00:00
|
|
|
borrowed_props: Default::default(),
|
2021-07-09 05:42:26 +00:00
|
|
|
descendents: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:46:53 +00:00
|
|
|
pub(crate) fn update_scope_dependencies<'creator_node>(
|
2021-07-09 05:42:26 +00:00
|
|
|
&mut self,
|
2021-09-13 04:59:08 +00:00
|
|
|
caller: &'creator_node dyn for<'b> Fn(&'b Scope) -> DomTree<'b>,
|
2021-07-26 16:14:48 +00:00
|
|
|
child_nodes: ScopeChildren,
|
2021-07-09 05:42:26 +00:00
|
|
|
) {
|
2021-09-13 04:59:08 +00:00
|
|
|
let caller = caller as *const _;
|
|
|
|
self.caller = unsafe { std::mem::transmute(caller) };
|
|
|
|
|
2021-07-26 16:14:48 +00:00
|
|
|
let child_nodes = unsafe { child_nodes.extend_lifetime() };
|
2021-07-09 05:42:26 +00:00
|
|
|
self.child_nodes = child_nodes;
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:40:04 +00:00
|
|
|
/// Returns true if the scope completed successfully
|
|
|
|
///
|
2021-08-27 13:53:26 +00:00
|
|
|
pub(crate) fn run_scope<'sel>(&'sel mut self, pool: &ResourcePool) -> bool {
|
2021-07-09 05:42:26 +00:00
|
|
|
// Cycle to the next frame and then reset it
|
|
|
|
// This breaks any latent references, invalidating every pointer referencing into it.
|
2021-07-15 08:09:28 +00:00
|
|
|
// Remove all the outdated listeners
|
2021-08-27 13:53:26 +00:00
|
|
|
self.ensure_drop_safety(pool);
|
2021-07-26 16:14:48 +00:00
|
|
|
|
2021-07-27 04:27:07 +00:00
|
|
|
// Safety:
|
|
|
|
// - We dropped the listeners, so no more &mut T can be used while these are held
|
|
|
|
// - All children nodes that rely on &mut T are replaced with a new reference
|
2021-07-09 05:42:26 +00:00
|
|
|
unsafe { self.hooks.reset() };
|
2021-07-26 16:14:48 +00:00
|
|
|
|
2021-07-27 04:27:07 +00:00
|
|
|
// Safety:
|
|
|
|
// - We've dropped all references to the wip bump frame
|
|
|
|
unsafe { self.frames.reset_wip_frame() };
|
2021-07-26 16:14:48 +00:00
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
// Cast the caller ptr from static to one with our own reference
|
2021-09-13 04:59:08 +00:00
|
|
|
let render: &dyn for<'b> Fn(&'b Scope) -> DomTree<'b> = unsafe { &*self.caller };
|
2021-07-09 05:42:26 +00:00
|
|
|
|
2021-07-30 20:07:42 +00:00
|
|
|
match render(self) {
|
2021-08-27 13:40:04 +00:00
|
|
|
None => false,
|
2021-07-20 23:03:49 +00:00
|
|
|
Some(new_head) => {
|
|
|
|
// the user's component succeeded. We can safely cycle to the next frame
|
2021-07-26 16:14:48 +00:00
|
|
|
self.frames.wip_frame_mut().head_node = unsafe { std::mem::transmute(new_head) };
|
2021-07-20 23:03:49 +00:00
|
|
|
self.frames.cycle_frame();
|
2021-08-27 13:40:04 +00:00
|
|
|
true
|
2021-07-20 23:03:49 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 19:15:16 +00:00
|
|
|
/// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
|
|
|
|
/// causuing UB in our tree.
|
|
|
|
///
|
|
|
|
/// This works by cleaning up our references from the bottom of the tree to the top. The directed graph of components
|
|
|
|
/// essentially forms a dependency tree that we can traverse from the bottom to the top. As we traverse, we remove
|
|
|
|
/// any possible references to the data in the hook list.
|
|
|
|
///
|
|
|
|
/// Refrences to hook data can only be stored in listeners and component props. During diffing, we make sure to log
|
|
|
|
/// all listeners and borrowed props so we can clear them here.
|
2021-09-01 19:45:53 +00:00
|
|
|
pub(crate) fn ensure_drop_safety(&mut self, pool: &ResourcePool) {
|
2021-08-27 13:53:26 +00:00
|
|
|
// make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
|
|
|
|
// run the hooks (which hold an &mut Referrence)
|
|
|
|
// right now, we don't drop
|
|
|
|
// let vdom = &self.vdom;
|
|
|
|
self.borrowed_props
|
|
|
|
.get_mut()
|
|
|
|
.drain(..)
|
|
|
|
.map(|li| unsafe { &*li })
|
|
|
|
.for_each(|comp| {
|
|
|
|
// First drop the component's undropped references
|
|
|
|
let scope_id = comp.associated_scope.get().unwrap();
|
|
|
|
let scope = pool.get_scope_mut(scope_id).unwrap();
|
|
|
|
scope.ensure_drop_safety(pool);
|
|
|
|
|
|
|
|
// Now, drop our own reference
|
|
|
|
let mut dropper = comp.drop_props.borrow_mut().take().unwrap();
|
|
|
|
dropper();
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now that all the references are gone, we can safely drop our own references in our listeners.
|
|
|
|
self.listeners
|
|
|
|
.get_mut()
|
|
|
|
.drain(..)
|
|
|
|
.map(|li| unsafe { &*li })
|
|
|
|
.for_each(|listener| {
|
|
|
|
listener.callback.borrow_mut().take();
|
|
|
|
});
|
2021-08-08 19:15:16 +00:00
|
|
|
}
|
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
// A safe wrapper around calling listeners
|
2021-08-10 04:29:53 +00:00
|
|
|
//
|
|
|
|
//
|
2021-08-31 16:28:44 +00:00
|
|
|
pub(crate) fn call_listener(&mut self, event: SyntheticEvent, element: ElementId) {
|
2021-07-13 20:48:47 +00:00
|
|
|
let listners = self.listeners.borrow_mut();
|
2021-07-11 18:49:52 +00:00
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
let raw_listener = listners.iter().find(|lis| {
|
2021-07-24 04:29:23 +00:00
|
|
|
let search = unsafe { &***lis };
|
2021-07-23 14:27:43 +00:00
|
|
|
let search_id = search.mounted_node.get();
|
2021-08-10 04:29:53 +00:00
|
|
|
|
|
|
|
// this assumes the node might not be mounted - should we assume that though?
|
|
|
|
match search_id.map(|f| f == element) {
|
|
|
|
Some(same) => same,
|
|
|
|
None => false,
|
2021-07-11 18:49:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-07-23 14:27:43 +00:00
|
|
|
if let Some(raw_listener) = raw_listener {
|
2021-07-24 04:29:23 +00:00
|
|
|
let listener = unsafe { &**raw_listener };
|
|
|
|
let mut cb = listener.callback.borrow_mut();
|
2021-07-26 16:14:48 +00:00
|
|
|
if let Some(cb) = cb.as_mut() {
|
|
|
|
(cb)(event);
|
|
|
|
}
|
2021-07-23 14:27:43 +00:00
|
|
|
} else {
|
|
|
|
log::warn!("An event was triggered but there was no listener to handle it");
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 04:10:09 +00:00
|
|
|
pub(crate) fn child_nodes<'a>(&'a self) -> ScopeChildren {
|
2021-09-01 04:57:04 +00:00
|
|
|
unsafe { self.child_nodes.shorten_lifetime() }
|
2021-07-29 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 04:10:09 +00:00
|
|
|
pub(crate) fn call_suspended_node<'a>(&'a self, task: u64) {
|
2021-09-01 19:45:53 +00:00
|
|
|
let g = self.suspended_nodes.borrow_mut();
|
|
|
|
|
|
|
|
if let Some(suspended) = g.get(&task) {
|
|
|
|
let sus: &'a VSuspended<'static> = unsafe { &**suspended };
|
|
|
|
let sus: &'a VSuspended<'a> = unsafe { std::mem::transmute(sus) };
|
|
|
|
|
|
|
|
let bump = self.frames.wip_frame();
|
|
|
|
let mut cb = sus.callback.borrow_mut();
|
|
|
|
let mut _cb = cb.take().unwrap();
|
|
|
|
let cx: SuspendedContext<'a> = SuspendedContext {
|
|
|
|
inner: Context {
|
|
|
|
props: &(),
|
|
|
|
scope: &self,
|
|
|
|
},
|
|
|
|
};
|
2021-09-03 15:28:09 +00:00
|
|
|
let new_node: DomTree<'a> = (_cb)(cx);
|
2021-09-01 19:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 04:10:09 +00:00
|
|
|
pub fn root(&self) -> &VNode {
|
|
|
|
self.frames.fin_head()
|
|
|
|
}
|
2021-07-18 16:39:32 +00:00
|
|
|
}
|