use crate::innerlude::*; use crate::nodes::VNode; use crate::{context::hooks::Hook, diff::diff}; use bumpalo::Bump; use generational_arena::Index; use std::{ any::TypeId, borrow::Borrow, cell::RefCell, future::Future, marker::PhantomData, sync::atomic::AtomicUsize, }; /// 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. pub struct Scope { // TODO @Jon // These hooks are actually references into the hook arena // These two could be combined with "OwningRef" to remove unsafe usage // could also use ourborous pub hooks: RefCell>, pub hook_arena: typed_arena::Arena, // Map to the parent pub parent: Option, // todo, do better with the active frame stuff pub frames: [Bump; 2], // somehow build this vnode with a lifetime tied to self pub cur_node: *mut VNode<'static>, pub active_frame: u8, // IE Which listeners need to be woken up? pub listeners: Vec>, // pub props_type: TypeId, pub caller: *const i32, } impl Scope { // create a new scope from a function pub(crate) fn new(f: FC, parent: Option) -> Self { // Capture the props type let props_type = TypeId::of::(); let hook_arena = typed_arena::Arena::new(); let hooks = RefCell::new(Vec::new()); let caller = f as *const i32; let frames = [Bump::new(), Bump::new()]; let listeners = Vec::new(); let active_frame = 1; let new = frames[0].alloc(VNode::Text(VText::new(""))); let cur_node = new as *mut _; Self { hook_arena, hooks, props_type, caller, active_frame, listeners, parent, frames, cur_node, } } /// Create a new context and run the component with references from the Virtual Dom /// This function downcasts the function pointer based on the stored props_type /// /// Props is ?Sized because we borrow the props and don't need to know the size. P (sized) is used as a marker (unsized) pub(crate) fn run<'a, P: Properties + ?Sized>(&self, props: &'a P) { let bump = &self.frames[0]; let ctx = Context { scope: &*self, _p: PhantomData {}, arena: &self.hook_arena, hooks: &self.hooks, idx: 0.into(), bump, }; /* SAFETY ALERT This particular usage of transmute is outlined in its docs https://doc.rust-lang.org/std/mem/fn.transmute.html We hide the generic bound on the function item by casting it to raw pointer. When the function is actually called, we transmute the function back using the props as reference. we could do a better check to make sure that the TypeID is correct before casting -- This is safe because we check that the generic type matches before casting. */ let caller = unsafe { std::mem::transmute::<*const i32, FC

>(self.caller) }; let new_nodes = caller(ctx, props); let old_nodes: &mut VNode<'static> = unsafe { &mut *self.cur_node }; // TODO: Iterate through the new nodes // move any listeners into ourself // perform the diff, dumping into the mutable change list // this doesnt perform any "diff compression" where an event and a re-render crate::diff::diff(old_nodes, &new_nodes); } }