2021-07-15 08:09:28 +00:00
|
|
|
use crate::innerlude::*;
|
2021-07-24 04:29:23 +00:00
|
|
|
use bumpalo::boxed::Box as BumpBox;
|
2021-07-09 05:42:26 +00:00
|
|
|
use std::{
|
|
|
|
any::{Any, TypeId},
|
|
|
|
cell::{Cell, RefCell},
|
2021-07-13 20:54:07 +00:00
|
|
|
collections::{HashMap, HashSet},
|
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-23 14:27:43 +00:00
|
|
|
pub(crate) descendents: RefCell<HashSet<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-15 07:38:09 +00:00
|
|
|
pub(crate) frames: ActiveFrame,
|
|
|
|
pub(crate) caller: Rc<WrappedCaller>,
|
2021-07-23 14:27:43 +00:00
|
|
|
pub(crate) child_nodes: &'static [VNode<'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-07-09 05:42:26 +00:00
|
|
|
pub(crate) listener_idx: Cell<usize>,
|
|
|
|
|
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-07-23 14:27:43 +00:00
|
|
|
// A reference to the resources shared by all the comonents
|
|
|
|
pub(crate) vdom: SharedResources,
|
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-07-18 16:39:32 +00:00
|
|
|
pub type WrappedCaller = dyn for<'b> Fn(&'b Scope) -> DomTree<'b>;
|
2021-07-15 08:09:28 +00:00
|
|
|
|
|
|
|
// The type of task that gets sent to the task scheduler
|
2021-07-14 06:04:19 +00:00
|
|
|
pub type FiberTask = Pin<Box<dyn Future<Output = EventTrigger>>>;
|
|
|
|
|
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
|
|
|
|
pub fn new<'creator_node>(
|
2021-07-11 18:49:52 +00:00
|
|
|
caller: Rc<WrappedCaller>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
arena_idx: ScopeId,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-07-15 07:38:09 +00:00
|
|
|
parent: Option<ScopeId>,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
height: u32,
|
2021-07-23 14:27:43 +00:00
|
|
|
|
2021-07-09 05:42:26 +00:00
|
|
|
child_nodes: &'creator_node [VNode<'creator_node>],
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
vdom: SharedResources,
|
2021-07-09 05:42:26 +00:00
|
|
|
) -> Self {
|
|
|
|
let child_nodes = unsafe { std::mem::transmute(child_nodes) };
|
|
|
|
Self {
|
|
|
|
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,
|
2021-07-23 14:27:43 +00:00
|
|
|
vdom,
|
2021-07-09 05:42:26 +00:00
|
|
|
listener_idx: Default::default(),
|
|
|
|
frames: ActiveFrame::new(),
|
|
|
|
hooks: Default::default(),
|
|
|
|
shared_contexts: Default::default(),
|
|
|
|
listeners: Default::default(),
|
|
|
|
descendents: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
pub(crate) fn update_caller<'creator_node>(&mut self, caller: Rc<WrappedCaller>) {
|
2021-07-09 15:54:07 +00:00
|
|
|
self.caller = caller;
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
pub(crate) fn update_children<'creator_node>(
|
2021-07-09 05:42:26 +00:00
|
|
|
&mut self,
|
|
|
|
child_nodes: &'creator_node [VNode<'creator_node>],
|
|
|
|
) {
|
|
|
|
let child_nodes = unsafe { std::mem::transmute(child_nodes) };
|
|
|
|
self.child_nodes = child_nodes;
|
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
pub(crate) fn run_scope<'sel>(&'sel mut self) -> Result<()> {
|
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
|
|
|
|
|
|
|
|
// This is a very dangerous operation
|
2021-07-23 14:27:43 +00:00
|
|
|
let next_frame = self.frames.prev_frame_mut();
|
2021-07-20 23:03:49 +00:00
|
|
|
next_frame.bump.reset();
|
2021-07-09 05:42:26 +00:00
|
|
|
|
|
|
|
self.listeners.borrow_mut().clear();
|
|
|
|
|
|
|
|
unsafe { self.hooks.reset() };
|
|
|
|
self.listener_idx.set(0);
|
|
|
|
|
|
|
|
// Cast the caller ptr from static to one with our own reference
|
2021-07-11 18:49:52 +00:00
|
|
|
let c3: &WrappedCaller = self.caller.as_ref();
|
2021-07-09 05:42:26 +00:00
|
|
|
|
2021-07-20 23:03:49 +00:00
|
|
|
match c3(self) {
|
|
|
|
None => {
|
|
|
|
// the user's component failed. We avoid cycling to the next frame
|
|
|
|
log::error!("Running your component failed! It will no longer receive events.");
|
|
|
|
Err(Error::ComponentFailed)
|
|
|
|
}
|
|
|
|
Some(new_head) => {
|
|
|
|
// the user's component succeeded. We can safely cycle to the next frame
|
2021-07-23 14:27:43 +00:00
|
|
|
self.frames.prev_frame_mut().head_node = unsafe { std::mem::transmute(new_head) };
|
2021-07-20 23:03:49 +00:00
|
|
|
self.frames.cycle_frame();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A safe wrapper around calling listeners
|
|
|
|
// calling listeners will invalidate the list of listeners
|
|
|
|
// The listener list will be completely drained because the next frame will write over previous listeners
|
2021-07-15 08:09:28 +00:00
|
|
|
pub(crate) fn call_listener(&mut self, trigger: EventTrigger) -> Result<()> {
|
2021-07-09 05:42:26 +00:00
|
|
|
let EventTrigger {
|
|
|
|
real_node_id,
|
|
|
|
event,
|
|
|
|
..
|
|
|
|
} = trigger;
|
|
|
|
|
2021-07-15 03:18:02 +00:00
|
|
|
if let &VirtualEvent::AsyncEvent { .. } = &event {
|
2021-07-14 06:04:19 +00:00
|
|
|
log::info!("arrived a fiber event");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-07-11 18:49:52 +00:00
|
|
|
log::debug!(
|
|
|
|
"There are {:?} listeners associated with this scope {:#?}",
|
|
|
|
self.listeners.borrow().len(),
|
2021-07-15 07:38:09 +00:00
|
|
|
self.our_arena_idx
|
2021-07-11 18:49:52 +00:00
|
|
|
);
|
|
|
|
|
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-07-11 18:49:52 +00:00
|
|
|
log::info!("searching listener {:#?}", search_id);
|
2021-07-23 14:27:43 +00:00
|
|
|
|
|
|
|
match (real_node_id, search_id) {
|
|
|
|
(Some(e), Some(search_id)) => search_id == e,
|
|
|
|
_ => 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();
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
#[inline]
|
2021-07-09 05:42:26 +00:00
|
|
|
pub(crate) fn next_frame<'bump>(&'bump self) -> &'bump VNode<'bump> {
|
|
|
|
self.frames.current_head_node()
|
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
#[inline]
|
2021-07-09 05:42:26 +00:00
|
|
|
pub(crate) fn old_frame<'bump>(&'bump self) -> &'bump VNode<'bump> {
|
|
|
|
self.frames.prev_head_node()
|
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:27 +00:00
|
|
|
#[inline]
|
2021-07-09 05:42:26 +00:00
|
|
|
pub(crate) fn cur_frame(&self) -> &BumpFrame {
|
|
|
|
self.frames.cur_frame()
|
|
|
|
}
|
|
|
|
|
2021-07-15 08:09:28 +00:00
|
|
|
/// Get the root VNode of this component
|
2021-07-12 22:19:27 +00:00
|
|
|
#[inline]
|
2021-07-11 18:49:52 +00:00
|
|
|
pub fn root<'a>(&'a self) -> &'a VNode<'a> {
|
2021-07-23 14:27:43 +00:00
|
|
|
&self.frames.current_head_node()
|
2021-07-09 05:42:26 +00:00
|
|
|
}
|
2021-07-23 14:27:43 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn child_nodes<'a>(&'a self) -> &'a [VNode<'a>] {
|
|
|
|
unsafe { std::mem::transmute(self.child_nodes) }
|
2021-07-18 16:39:32 +00:00
|
|
|
}
|
|
|
|
}
|