123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- use crate::innerlude::*;
- use futures_channel::mpsc::UnboundedSender;
- use fxhash::FxHashMap;
- use std::{
- any::{Any, TypeId},
- cell::{Cell, RefCell},
- collections::HashMap,
- future::Future,
- rc::Rc,
- };
- use bumpalo::{boxed::Box as BumpBox, Bump};
- /// Components in Dioxus use the "Context" object to interact with their lifecycle.
- ///
- /// This lets components access props, schedule updates, integrate hooks, and expose shared state.
- ///
- /// For the most part, the only method you should be using regularly is `render`.
- ///
- /// ## Example
- ///
- /// ```ignore
- /// #[derive(Props)]
- /// struct ExampleProps {
- /// name: String
- /// }
- ///
- /// fn Example((cx, props): Scope<Props>) -> Element {
- /// cx.render(rsx!{ div {"Hello, {props.name}"} })
- /// }
- /// ```
- pub type Context<'a> = &'a ScopeInner;
- /// 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.
- ///
- /// We expose the `Scope` type so downstream users can traverse the Dioxus VirtualDOM for whatever
- /// use case they might have.
- pub struct ScopeInner {
- // Book-keeping about our spot in the arena
- // yes, a raw pointer
- // it's bump allocated so it's stable
- // the safety of parent pointers is guaranteed by the logic in this crate
- pub(crate) parent_scope: Option<*mut ScopeInner>,
- pub(crate) our_arena_idx: ScopeId,
- pub(crate) height: u32,
- pub(crate) subtree: Cell<u32>,
- pub(crate) is_subtree_root: Cell<bool>,
- // Nodes
- pub(crate) frames: ActiveFrame,
- pub(crate) vcomp: *const VComponent<'static>,
- /*
- we care about:
- - listeners (and how to call them when an event is triggered)
- - borrowed props (and how to drop them when the parent is dropped)
- - suspended nodes (and how to call their callback when their associated tasks are complete)
- */
- pub(crate) items: RefCell<SelfReferentialItems<'static>>,
- // State
- pub(crate) hooks: HookList,
- // todo: move this into a centralized place - is more memory efficient
- pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
- pub(crate) sender: UnboundedSender<SchedulerMsg>,
- }
- pub struct SelfReferentialItems<'a> {
- pub(crate) listeners: Vec<*const Listener<'a>>,
- pub(crate) borrowed_props: Vec<*const VComponent<'a>>,
- pub(crate) suspended_nodes: FxHashMap<u64, *const VSuspended<'a>>,
- pub(crate) tasks: Vec<BumpBox<'a, dyn Future<Output = ()>>>,
- pub(crate) pending_effects: Vec<BumpBox<'a, dyn FnMut()>>,
- }
- pub struct ScopeVcomp {
- // important things
- }
- impl ScopeInner {
- /// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
- /// causing 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.
- ///
- /// References 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.
- ///
- /// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
- /// be dropped.
- pub(crate) fn ensure_drop_safety(&mut self) {
- // 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 Reference)
- // right now, we don't drop
- self.items
- .get_mut()
- .borrowed_props
- .drain(..)
- .map(|li| unsafe { &*li })
- .for_each(|comp| {
- // First drop the component's undropped references
- let scope_id = comp
- .associated_scope
- .get()
- .expect("VComponents should be associated with a valid Scope");
- let scope = unsafe { &mut *scope_id };
- scope.ensure_drop_safety();
- todo!("drop the component's props");
- // let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
- // drop_props();
- });
- // Now that all the references are gone, we can safely drop our own references in our listeners.
- self.items
- .get_mut()
- .listeners
- .drain(..)
- .map(|li| unsafe { &*li })
- .for_each(|listener| drop(listener.callback.borrow_mut().take()));
- }
- /// A safe wrapper around calling listeners
- pub(crate) fn call_listener(&mut self, event: UserEvent, element: ElementId) {
- let listners = &mut self.items.get_mut().listeners;
- let raw_listener = listners.iter().find(|lis| {
- let search = unsafe { &***lis };
- if search.event == event.name {
- let search_id = search.mounted_node.get();
- search_id.map(|f| f == element).unwrap_or(false)
- } else {
- false
- }
- });
- if let Some(raw_listener) = raw_listener {
- let listener = unsafe { &**raw_listener };
- let mut cb = listener.callback.borrow_mut();
- if let Some(cb) = cb.as_mut() {
- (cb)(event.event);
- }
- } else {
- log::warn!("An event was triggered but there was no listener to handle it");
- }
- }
- // General strategy here is to load up the appropriate suspended task and then run it.
- // Suspended nodes cannot be called repeatedly.
- pub(crate) fn call_suspended_node<'a>(&'a mut self, task_id: u64) {
- let mut nodes = &mut self.items.get_mut().suspended_nodes;
- if let Some(suspended) = nodes.remove(&task_id) {
- let sus: &'a VSuspended<'static> = unsafe { &*suspended };
- let sus: &'a VSuspended<'a> = unsafe { std::mem::transmute(sus) };
- let mut boxed = sus.callback.borrow_mut().take().unwrap();
- let new_node: Element<'a> = boxed();
- }
- }
- // run the list of effects
- pub(crate) fn run_effects(&mut self) {
- // pub(crate) fn run_effects(&mut self, pool: &ResourcePool) {
- for mut effect in self.items.get_mut().pending_effects.drain(..) {
- effect();
- }
- }
- /// Render this component.
- ///
- /// Returns true if the scope completed successfully and false if running failed (IE a None error was propagated).
- pub(crate) fn run_scope<'sel>(&'sel mut self) -> bool {
- // Cycle to the next frame and then reset it
- // This breaks any latent references, invalidating every pointer referencing into it.
- // Remove all the outdated listeners
- self.ensure_drop_safety();
- // 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
- unsafe { self.hooks.reset() };
- // Safety:
- // - We've dropped all references to the wip bump frame
- unsafe { self.frames.reset_wip_frame() };
- let items = self.items.get_mut();
- // just forget about our suspended nodes while we're at it
- items.suspended_nodes.clear();
- // guarantee that we haven't screwed up - there should be no latent references anywhere
- debug_assert!(items.listeners.is_empty());
- debug_assert!(items.suspended_nodes.is_empty());
- debug_assert!(items.borrowed_props.is_empty());
- log::debug!("Borrowed stuff is successfully cleared");
- // temporarily cast the vcomponent to the right lifetime
- let vcomp = self.load_vcomp();
- let render: &dyn for<'b> Fn(&'b ScopeInner) -> Element<'b> = todo!();
- // Todo: see if we can add stronger guarantees around internal bookkeeping and failed component renders.
- if let Some(builder) = render(self) {
- let new_head = builder.into_vnode(NodeFactory {
- bump: &self.frames.wip_frame().bump,
- });
- log::debug!("Render is successful");
- // the user's component succeeded. We can safely cycle to the next frame
- self.frames.wip_frame_mut().head_node = unsafe { std::mem::transmute(new_head) };
- self.frames.cycle_frame();
- true
- } else {
- false
- }
- }
- pub(crate) fn new_subtree(&self) -> Option<u32> {
- todo!()
- // if self.is_subtree_root.get() {
- // None
- // } else {
- // let cur = self.shared.cur_subtree.get();
- // self.shared.cur_subtree.set(cur + 1);
- // Some(cur)
- // }
- }
- pub(crate) fn update_vcomp(&mut self, vcomp: &VComponent) {
- let f: *const _ = vcomp;
- self.vcomp = unsafe { std::mem::transmute(f) };
- }
- pub(crate) fn load_vcomp<'a>(&'a mut self) -> &'a VComponent<'a> {
- unsafe { std::mem::transmute(&*self.vcomp) }
- }
- /// Get the root VNode for this Scope.
- ///
- /// This VNode is the "entrypoint" VNode. If the component renders multiple nodes, then this VNode will be a fragment.
- ///
- /// # Example
- /// ```rust
- /// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
- /// dom.rebuild();
- ///
- /// let base = dom.base_scope();
- ///
- /// if let VNode::VElement(node) = base.root_node() {
- /// assert_eq!(node.tag_name, "div");
- /// }
- /// ```
- pub fn root_node(&self) -> &VNode {
- self.frames.fin_head()
- }
- /// Get the subtree ID that this scope belongs to.
- ///
- /// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
- /// the mutations to the correct window/portal/subtree.
- ///
- ///
- /// # Example
- ///
- /// ```rust
- /// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
- /// dom.rebuild();
- ///
- /// let base = dom.base_scope();
- ///
- /// assert_eq!(base.subtree(), 0);
- /// ```
- pub fn subtree(&self) -> u32 {
- self.subtree.get()
- }
- /// Get the height of this Scope - IE the number of scopes above it.
- ///
- /// A Scope with a height of `0` is the root scope - there are no other scopes above it.
- ///
- /// # Example
- ///
- /// ```rust
- /// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
- /// dom.rebuild();
- ///
- /// let base = dom.base_scope();
- ///
- /// assert_eq!(base.height(), 0);
- /// ```
- pub fn height(&self) -> u32 {
- self.height
- }
- /// Get the Parent of this Scope within this Dioxus VirtualDOM.
- ///
- /// This ID is not unique across Dioxus VirtualDOMs or across time. IDs will be reused when components are unmounted.
- ///
- /// The base component will not have a parent, and will return `None`.
- ///
- /// # Example
- ///
- /// ```rust
- /// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
- /// dom.rebuild();
- ///
- /// let base = dom.base_scope();
- ///
- /// assert_eq!(base.parent(), None);
- /// ```
- pub fn parent(&self) -> Option<ScopeId> {
- match self.parent_scope {
- Some(p) => Some(unsafe { &*p }.our_arena_idx),
- None => None,
- }
- }
- /// Get the ID of this Scope within this Dioxus VirtualDOM.
- ///
- /// This ID is not unique across Dioxus VirtualDOMs or across time. IDs will be reused when components are unmounted.
- ///
- /// # Example
- ///
- /// ```rust
- /// let mut dom = VirtualDom::new(|(cx, props)|cx.render(rsx!{ div {} }));
- /// dom.rebuild();
- /// let base = dom.base_scope();
- ///
- /// assert_eq!(base.scope_id(), 0);
- /// ```
- pub fn scope_id(&self) -> ScopeId {
- self.our_arena_idx
- }
- /// Create a subscription that schedules a future render for the reference component
- ///
- /// ## Notice: you should prefer using prepare_update and get_scope_id
- pub fn schedule_update(&self) -> Rc<dyn Fn() + 'static> {
- // pub fn schedule_update(&self) -> Rc<dyn Fn() + 'static> {
- let chan = self.sender.clone();
- let id = self.scope_id();
- Rc::new(move || {
- chan.unbounded_send(SchedulerMsg::Immediate(id));
- })
- }
- /// Schedule an update for any component given its ScopeId.
- ///
- /// A component's ScopeId can be obtained from `use_hook` or the [`Context::scope_id`] method.
- ///
- /// This method should be used when you want to schedule an update for a component
- pub fn schedule_update_any(&self) -> Rc<dyn Fn(ScopeId)> {
- let chan = self.sender.clone();
- Rc::new(move |id| {
- chan.unbounded_send(SchedulerMsg::Immediate(id));
- })
- }
- /// Get the [`ScopeId`] of a mounted component.
- ///
- /// `ScopeId` is not unique for the lifetime of the VirtualDom - a ScopeId will be reused if a component is unmounted.
- pub fn needs_update(&self) {
- self.needs_update_any(self.scope_id())
- }
- /// Get the [`ScopeId`] of a mounted component.
- ///
- /// `ScopeId` is not unique for the lifetime of the VirtualDom - a ScopeId will be reused if a component is unmounted.
- pub fn needs_update_any(&self, id: ScopeId) {
- self.sender
- .unbounded_send(SchedulerMsg::Immediate(id))
- .unwrap();
- }
- /// Get the [`ScopeId`] of a mounted component.
- ///
- /// `ScopeId` is not unique for the lifetime of the VirtualDom - a ScopeId will be reused if a component is unmounted.
- pub fn bump(&self) -> &Bump {
- let bump = &self.frames.wip_frame().bump;
- bump
- }
- /// Take a lazy VNode structure and actually build it with the context of the VDom's efficient VNode allocator.
- ///
- /// This function consumes the context and absorb the lifetime, so these VNodes *must* be returned.
- ///
- /// ## Example
- ///
- /// ```ignore
- /// fn Component(cx: Context<()>) -> VNode {
- /// // Lazy assemble the VNode tree
- /// let lazy_tree = html! {<div> "Hello World" </div>};
- ///
- /// // Actually build the tree and allocate it
- /// cx.render(lazy_tree)
- /// }
- ///```
- pub fn render<'src>(
- &'src self,
- lazy_nodes: Option<LazyNodes<'src, '_>>,
- ) -> Option<VNode<'src>> {
- let bump = &self.frames.wip_frame().bump;
- let factory = NodeFactory { bump };
- lazy_nodes.map(|f| f.call(factory))
- }
- /// Push an effect to be ran after the component has been successfully mounted to the dom
- /// Returns the effect's position in the stack
- pub fn push_effect<'src>(&'src self, effect: impl FnOnce() + 'src) -> usize {
- // this is some tricker to get around not being able to actually call fnonces
- let mut slot = Some(effect);
- let fut: &mut dyn FnMut() = self.bump().alloc(move || slot.take().unwrap()());
- // wrap it in a type that will actually drop the contents
- let boxed_fut = unsafe { BumpBox::from_raw(fut) };
- // erase the 'src lifetime for self-referential storage
- let self_ref_fut = unsafe { std::mem::transmute(boxed_fut) };
- let mut items = self.items.borrow_mut();
- items.pending_effects.push(self_ref_fut);
- items.pending_effects.len() - 1
- }
- /// Pushes the future onto the poll queue to be polled
- /// The future is forcibly dropped if the component is not ready by the next render
- pub fn push_task<'src>(&'src self, fut: impl Future<Output = ()> + 'src) -> usize {
- // allocate the future
- let fut: &mut dyn Future<Output = ()> = self.bump().alloc(fut);
- // wrap it in a type that will actually drop the contents
- let boxed_fut: BumpBox<dyn Future<Output = ()>> = unsafe { BumpBox::from_raw(fut) };
- // erase the 'src lifetime for self-referential storage
- let self_ref_fut = unsafe { std::mem::transmute(boxed_fut) };
- let mut items = self.items.borrow_mut();
- items.tasks.push(self_ref_fut);
- items.tasks.len() - 1
- }
- /// This method enables the ability to expose state to children further down the VirtualDOM Tree.
- ///
- /// This is a "fundamental" operation and should only be called during initialization of a hook.
- ///
- /// For a hook that provides the same functionality, use `use_provide_state` and `use_consume_state` instead.
- ///
- /// When the component is dropped, so is the context. Be aware of this behavior when consuming
- /// the context via Rc/Weak.
- ///
- /// # Example
- ///
- /// ```
- /// struct SharedState(&'static str);
- ///
- /// static App: FC<()> = |(cx, props)|{
- /// cx.use_hook(|_| cx.provide_state(SharedState("world")), |_| {}, |_| {});
- /// rsx!(cx, Child {})
- /// }
- ///
- /// static Child: FC<()> = |(cx, props)|{
- /// let state = cx.consume_state::<SharedState>();
- /// rsx!(cx, div { "hello {state.0}" })
- /// }
- /// ```
- pub fn provide_state<T>(&self, value: T)
- where
- T: 'static,
- {
- self.shared_contexts
- .borrow_mut()
- .insert(TypeId::of::<T>(), Rc::new(value))
- .map(|f| f.downcast::<T>().ok())
- .flatten();
- }
- /// Try to retrieve a SharedState with type T from the any parent Scope.
- pub fn consume_state<T: 'static>(&self) -> Option<Rc<T>> {
- if let Some(shared) = self.shared_contexts.borrow().get(&TypeId::of::<T>()) {
- Some(shared.clone().downcast::<T>().unwrap())
- } else {
- let mut search_parent = self.parent_scope;
- while let Some(parent_ptr) = search_parent {
- let parent = unsafe { &*parent_ptr };
- if let Some(shared) = parent.shared_contexts.borrow().get(&TypeId::of::<T>()) {
- return Some(shared.clone().downcast::<T>().unwrap());
- }
- search_parent = parent.parent_scope;
- }
- None
- }
- }
- /// Create a new subtree with this scope as the root of the subtree.
- ///
- /// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
- /// the mutations to the correct window/portal/subtree.
- ///
- /// This method
- ///
- /// # Example
- ///
- /// ```rust
- /// static App: FC<()> = |(cx, props)| {
- /// todo!();
- /// rsx!(cx, div { "Subtree {id}"})
- /// };
- /// ```
- pub fn create_subtree(&self) -> Option<u32> {
- self.new_subtree()
- }
- /// Get the subtree ID that this scope belongs to.
- ///
- /// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
- /// the mutations to the correct window/portal/subtree.
- ///
- /// # Example
- ///
- /// ```rust
- /// static App: FC<()> = |(cx, props)| {
- /// let id = cx.get_current_subtree();
- /// rsx!(cx, div { "Subtree {id}"})
- /// };
- /// ```
- pub fn get_current_subtree(&self) -> u32 {
- self.subtree()
- }
- /// Store a value between renders
- ///
- /// This is *the* foundational hook for all other hooks.
- ///
- /// - Initializer: closure used to create the initial hook state
- /// - Runner: closure used to output a value every time the hook is used
- ///
- /// To "cleanup" the hook, implement `Drop` on the stored hook value. Whenever the component is dropped, the hook
- /// will be dropped as well.
- ///
- /// # Example
- ///
- /// ```ignore
- /// // use_ref is the simplest way of storing a value between renders
- /// fn use_ref<T: 'static>(initial_value: impl FnOnce() -> T) -> &RefCell<T> {
- /// use_hook(
- /// || Rc::new(RefCell::new(initial_value())),
- /// |state| state,
- /// )
- /// }
- /// ```
- pub fn use_hook<'src, State: 'static, Output: 'src>(
- &'src self,
- initializer: impl FnOnce(usize) -> State,
- runner: impl FnOnce(&'src mut State) -> Output,
- ) -> Output {
- if self.hooks.at_end() {
- self.hooks.push_hook(initializer(self.hooks.len()));
- }
- runner(self.hooks.next::<State>().expect(HOOK_ERR_MSG))
- }
- }
- const HOOK_ERR_MSG: &str = r###"
- Unable to retrieve the hook that was initialized at this index.
- Consult the `rules of hooks` to understand how to use hooks properly.
- You likely used the hook in a conditional. Hooks rely on consistent ordering between renders.
- Functions prefixed with "use" should never be called conditionally.
- "###;
|