wip: work on scheduler, async, coroutines, and merge scope into context
This commit is contained in:
1 parent
1e6e5e611b
commit
b56ea6c9a9
11 files changed
+420
-442
No files matched your search
+270
-13
@@ -1,4 +1,5 @@
|
||||
use crate::innerlude::*;
|
||||
|
||||
use fxhash::FxHashMap;
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
@@ -9,6 +10,36 @@ use std::{
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
use crate::{innerlude::*, lazynodes::LazyNodes};
|
||||
use bumpalo::{boxed::Box as BumpBox, Bump};
|
||||
use std::ops::Deref;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// Note: all of these methods are *imperative* - they do not act as hooks! They are meant to be used by hooks
|
||||
/// to provide complex behavior. For instance, calling "add_shared_state" on every render is considered a leak. This method
|
||||
/// exists for the `use_provide_state` hook to provide a shared state object.
|
||||
///
|
||||
/// For the most part, the only method you should be using regularly is `render`.
|
||||
///
|
||||
/// ## Example
|
||||
///
|
||||
/// ```ignore
|
||||
/// #[derive(Properties)]
|
||||
/// struct Props {
|
||||
/// name: String
|
||||
/// }
|
||||
///
|
||||
/// fn example(cx: Context<Props>) -> VNode {
|
||||
/// html! {
|
||||
/// <div> "Hello, {cx.name}" </div>
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
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.
|
||||
@@ -28,7 +59,7 @@ pub struct ScopeInner {
|
||||
|
||||
// Nodes
|
||||
pub(crate) frames: ActiveFrame,
|
||||
pub(crate) caller: *const dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>,
|
||||
pub(crate) caller: BumpBox<'static, dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>>,
|
||||
|
||||
/*
|
||||
we care about:
|
||||
@@ -40,6 +71,9 @@ pub struct ScopeInner {
|
||||
pub(crate) borrowed_props: RefCell<Vec<*const VComponent<'static>>>,
|
||||
pub(crate) suspended_nodes: RefCell<FxHashMap<u64, *const VSuspended<'static>>>,
|
||||
|
||||
pub(crate) tasks: RefCell<Vec<BumpBox<'static, dyn Future<Output = ()>>>>,
|
||||
pub(crate) pending_effects: RefCell<Vec<BumpBox<'static, dyn FnMut()>>>,
|
||||
|
||||
// State
|
||||
pub(crate) hooks: HookList,
|
||||
|
||||
@@ -175,7 +209,7 @@ impl ScopeInner {
|
||||
// Scopes cannot be made anywhere else except for this file
|
||||
// Therefore, their lifetimes are connected exclusively to the virtual dom
|
||||
pub(crate) fn new(
|
||||
caller: &dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>,
|
||||
caller: BumpBox<dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>>,
|
||||
our_arena_idx: ScopeId,
|
||||
parent_idx: Option<ScopeId>,
|
||||
height: u32,
|
||||
@@ -186,8 +220,6 @@ impl ScopeInner {
|
||||
|
||||
let memoized_updater = Rc::new(move || schedule_any_update(our_arena_idx));
|
||||
|
||||
let caller = caller as *const _;
|
||||
|
||||
// wipe away the associated lifetime - we are going to manually manage the one-way lifetime graph
|
||||
let caller = unsafe { std::mem::transmute(caller) };
|
||||
|
||||
@@ -200,9 +232,11 @@ impl ScopeInner {
|
||||
height,
|
||||
subtree: Cell::new(subtree),
|
||||
is_subtree_root: Cell::new(false),
|
||||
|
||||
tasks: Default::default(),
|
||||
frames: ActiveFrame::new(),
|
||||
hooks: Default::default(),
|
||||
|
||||
pending_effects: Default::default(),
|
||||
suspended_nodes: Default::default(),
|
||||
shared_contexts: Default::default(),
|
||||
listeners: Default::default(),
|
||||
@@ -297,14 +331,8 @@ impl ScopeInner {
|
||||
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 cx: SuspendedContext<'a> = SuspendedContext {
|
||||
inner: Context { scope: self },
|
||||
};
|
||||
|
||||
let mut cb = sus.callback.borrow_mut().take().unwrap();
|
||||
|
||||
let new_node: Element<'a> = (cb)(cx);
|
||||
let mut boxed = sus.callback.borrow_mut().take().unwrap();
|
||||
let new_node: Element<'a> = boxed();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,4 +400,233 @@ impl ScopeInner {
|
||||
false
|
||||
}
|
||||
}
|
||||
/// 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> {
|
||||
self.memoized_updater.clone()
|
||||
}
|
||||
|
||||
/// 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.memoized_updater)()
|
||||
}
|
||||
|
||||
/// 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.shared.schedule_any_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)> {
|
||||
self.shared.schedule_any_immediate.clone()
|
||||
}
|
||||
|
||||
/// 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) };
|
||||
|
||||
self.pending_effects.borrow_mut().push(self_ref_fut);
|
||||
self.pending_effects.borrow().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) };
|
||||
|
||||
self.tasks.borrow_mut().push(self_ref_fut);
|
||||
self.tasks.borrow().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>> {
|
||||
let getter = &self.shared.get_shared_context;
|
||||
let ty = TypeId::of::<T>();
|
||||
let idx = self.our_arena_idx;
|
||||
getter(idx, ty).map(|f| f.downcast().unwrap())
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// - Cleanup: closure used to teardown the hook once the dom is cleaned up
|
||||
///
|
||||
///
|
||||
/// # 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, Output, Init, Run, Cleanup>(
|
||||
&'src self,
|
||||
initializer: Init,
|
||||
runner: Run,
|
||||
cleanup: Cleanup,
|
||||
) -> Output
|
||||
where
|
||||
State: 'static,
|
||||
Output: 'src,
|
||||
Init: FnOnce(usize) -> State,
|
||||
Run: FnOnce(&'src mut State) -> Output,
|
||||
Cleanup: FnOnce(Box<State>) + 'static,
|
||||
{
|
||||
// If the idx is the same as the hook length, then we need to add the current hook
|
||||
if self.hooks.at_end() {
|
||||
self.hooks.push_hook(
|
||||
initializer(self.hooks.len()),
|
||||
Box::new(|raw| {
|
||||
let s = raw.downcast::<State>().unwrap();
|
||||
cleanup(s);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
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.
|
||||
"###;
|
||||
Reference in new issue
Block a user