wip: consolidation, simplification
This commit is contained in:
1 parent
b56ea6c9a9
commit
70b983d090
17 files changed
+1228
-836
No files matched your search
+258
-284
@@ -6,36 +6,27 @@ use std::{
|
||||
cell::{Cell, RefCell},
|
||||
collections::HashMap,
|
||||
future::Future,
|
||||
pin::Pin,
|
||||
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 {
|
||||
/// #[derive(Props)]
|
||||
/// struct ExampleProps {
|
||||
/// name: String
|
||||
/// }
|
||||
///
|
||||
/// fn example(cx: Context<Props>) -> VNode {
|
||||
/// html! {
|
||||
/// <div> "Hello, {cx.name}" </div>
|
||||
/// }
|
||||
/// fn Example((cx, props): Scope<Props>) -> Element {
|
||||
/// cx.render(rsx!{ div {"Hello, {props.name}"} })
|
||||
/// }
|
||||
/// ```
|
||||
pub type Context<'a> = &'a ScopeInner;
|
||||
@@ -59,7 +50,8 @@ pub struct ScopeInner {
|
||||
|
||||
// Nodes
|
||||
pub(crate) frames: ActiveFrame,
|
||||
pub(crate) caller: BumpBox<'static, dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>>,
|
||||
|
||||
pub(crate) vcomp: *const VComponent<'static>,
|
||||
|
||||
/*
|
||||
we care about:
|
||||
@@ -67,12 +59,7 @@ pub struct ScopeInner {
|
||||
- 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) listeners: RefCell<Vec<*const Listener<'static>>>,
|
||||
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()>>>,
|
||||
pub(crate) items: RefCell<SelfReferentialItems<'static>>,
|
||||
|
||||
// State
|
||||
pub(crate) hooks: HookList,
|
||||
@@ -80,15 +67,221 @@ pub struct ScopeInner {
|
||||
// todo: move this into a centralized place - is more memory efficient
|
||||
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
|
||||
|
||||
// whenever set_state is called, we fire off a message to the scheduler
|
||||
// this closure _is_ the method called by schedule_update that marks this component as dirty
|
||||
pub(crate) memoized_updater: Rc<dyn Fn()>,
|
||||
|
||||
pub(crate) shared: EventChannel,
|
||||
}
|
||||
|
||||
/// Public interface for Scopes.
|
||||
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()>>,
|
||||
}
|
||||
|
||||
impl ScopeInner {
|
||||
// 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(crate) fn new(
|
||||
vcomp: &VComponent,
|
||||
our_arena_idx: ScopeId,
|
||||
parent_idx: Option<ScopeId>,
|
||||
height: u32,
|
||||
subtree: u32,
|
||||
shared: EventChannel,
|
||||
) -> Self {
|
||||
let vcomp = unsafe { std::mem::transmute(vcomp as *const VComponent) };
|
||||
|
||||
Self {
|
||||
shared,
|
||||
parent_idx,
|
||||
our_arena_idx,
|
||||
height,
|
||||
subtree: Cell::new(subtree),
|
||||
is_subtree_root: Cell::new(false),
|
||||
frames: ActiveFrame::new(),
|
||||
vcomp,
|
||||
|
||||
hooks: Default::default(),
|
||||
shared_contexts: Default::default(),
|
||||
|
||||
items: RefCell::new(SelfReferentialItems {
|
||||
listeners: Default::default(),
|
||||
borrowed_props: Default::default(),
|
||||
suspended_nodes: Default::default(),
|
||||
tasks: Default::default(),
|
||||
pending_effects: Default::default(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// 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, pool: &ResourcePool) {
|
||||
// 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");
|
||||
|
||||
if let Some(scope) = pool.get_scope_mut(&scope_id) {
|
||||
scope.ensure_drop_safety(pool);
|
||||
|
||||
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, 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, pool: &ResourcePool) -> 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(pool);
|
||||
|
||||
// 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.
|
||||
@@ -128,16 +321,6 @@ impl ScopeInner {
|
||||
self.subtree.get()
|
||||
}
|
||||
|
||||
pub(crate) fn new_subtree(&self) -> Option<u32> {
|
||||
if self.is_subtree_root.get() {
|
||||
None
|
||||
} else {
|
||||
let cur = self.shared.cur_subtree.get();
|
||||
self.shared.cur_subtree.set(cur + 1);
|
||||
Some(cur)
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
@@ -192,233 +375,17 @@ impl ScopeInner {
|
||||
pub fn scope_id(&self) -> ScopeId {
|
||||
self.our_arena_idx
|
||||
}
|
||||
}
|
||||
|
||||
// The type of closure that wraps calling components
|
||||
/// 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>>>;
|
||||
|
||||
/// Private interface for Scopes.
|
||||
impl ScopeInner {
|
||||
// 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(crate) fn new(
|
||||
caller: BumpBox<dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>>,
|
||||
our_arena_idx: ScopeId,
|
||||
parent_idx: Option<ScopeId>,
|
||||
height: u32,
|
||||
subtree: u32,
|
||||
shared: EventChannel,
|
||||
) -> Self {
|
||||
let schedule_any_update = shared.schedule_any_immediate.clone();
|
||||
|
||||
let memoized_updater = Rc::new(move || schedule_any_update(our_arena_idx));
|
||||
|
||||
// wipe away the associated lifetime - we are going to manually manage the one-way lifetime graph
|
||||
let caller = unsafe { std::mem::transmute(caller) };
|
||||
|
||||
Self {
|
||||
memoized_updater,
|
||||
shared,
|
||||
caller,
|
||||
parent_idx,
|
||||
our_arena_idx,
|
||||
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(),
|
||||
borrowed_props: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_scope_dependencies(
|
||||
&mut self,
|
||||
caller: &dyn for<'b> Fn(&'b ScopeInner) -> Element<'b>,
|
||||
) {
|
||||
log::debug!("Updating scope dependencies {:?}", self.our_arena_idx);
|
||||
let caller = caller as *const _;
|
||||
self.caller = unsafe { std::mem::transmute(caller) };
|
||||
}
|
||||
|
||||
/// 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, pool: &ResourcePool) {
|
||||
// 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.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()
|
||||
.expect("VComponents should be associated with a valid Scope");
|
||||
|
||||
if let Some(scope) = pool.get_scope_mut(scope_id) {
|
||||
scope.ensure_drop_safety(pool);
|
||||
|
||||
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.listeners
|
||||
.get_mut()
|
||||
.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 = self.listeners.borrow_mut();
|
||||
|
||||
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 = self.suspended_nodes.borrow_mut();
|
||||
|
||||
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, pool: &ResourcePool) {
|
||||
todo!()
|
||||
// let mut effects = self.frames.effects.borrow_mut();
|
||||
// let mut effects = effects.drain(..).collect::<Vec<_>>();
|
||||
|
||||
// for effect in effects {
|
||||
// let effect = unsafe { &*effect };
|
||||
// let effect = effect.as_ref();
|
||||
|
||||
// let mut effect = effect.borrow_mut();
|
||||
// let mut effect = effect.as_mut();
|
||||
|
||||
// effect.run(pool);
|
||||
// }
|
||||
}
|
||||
|
||||
/// 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, pool: &ResourcePool) -> 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(pool);
|
||||
|
||||
// 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() };
|
||||
|
||||
// just forget about our suspended nodes while we're at it
|
||||
self.suspended_nodes.get_mut().clear();
|
||||
|
||||
// guarantee that we haven't screwed up - there should be no latent references anywhere
|
||||
debug_assert!(self.listeners.borrow().is_empty());
|
||||
debug_assert!(self.suspended_nodes.borrow().is_empty());
|
||||
debug_assert!(self.borrowed_props.borrow().is_empty());
|
||||
|
||||
log::debug!("Borrowed stuff is successfully cleared");
|
||||
|
||||
// Cast the caller ptr from static to one with our own reference
|
||||
let render: &dyn for<'b> Fn(&'b ScopeInner) -> Element<'b> = unsafe { &*self.caller };
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
/// 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)
|
||||
// pub fn schedule_update(&self) -> Rc<dyn Fn() + 'static> {
|
||||
let chan = self.shared.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.
|
||||
@@ -427,7 +394,27 @@ impl ScopeInner {
|
||||
///
|
||||
/// 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()
|
||||
let chan = self.shared.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.shared
|
||||
.sender
|
||||
.unbounded_send(SchedulerMsg::Immediate(id))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Get the [`ScopeId`] of a mounted component.
|
||||
@@ -475,8 +462,9 @@ impl ScopeInner {
|
||||
// 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
|
||||
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
|
||||
@@ -491,8 +479,9 @@ impl ScopeInner {
|
||||
// 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
|
||||
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.
|
||||
@@ -580,8 +569,9 @@ impl ScopeInner {
|
||||
///
|
||||
/// - 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
|
||||
///
|
||||
/// To "cleanup" the hook, implement `Drop` on the stored hook value. Whenever the component is dropped, the hook
|
||||
/// will be dropped as well.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
@@ -591,32 +581,16 @@ impl ScopeInner {
|
||||
/// use_hook(
|
||||
/// || Rc::new(RefCell::new(initial_value())),
|
||||
/// |state| state,
|
||||
/// |_| {},
|
||||
/// )
|
||||
/// }
|
||||
/// ```
|
||||
pub fn use_hook<'src, State, Output, Init, Run, Cleanup>(
|
||||
pub fn use_hook<'src, State: 'static, Output: 'src>(
|
||||
&'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
|
||||
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()),
|
||||
Box::new(|raw| {
|
||||
let s = raw.downcast::<State>().unwrap();
|
||||
cleanup(s);
|
||||
}),
|
||||
);
|
||||
self.hooks.push_hook(initializer(self.hooks.len()));
|
||||
}
|
||||
|
||||
runner(self.hooks.next::<State>().expect(HOOK_ERR_MSG))
|
||||
|
||||
Reference in new issue
Block a user