restore a few unneeded breaking changes
This commit is contained in:
1 parent
14c852010b
commit
4ee4cf23d3
6 files changed
+219
-6
No files matched your search
@@ -135,7 +135,7 @@ impl<T: std::fmt::Debug> std::fmt::Debug for Event<T> {
|
|||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub struct EventHandler<'bump, T: ?Sized = ()> {
|
pub struct EventHandler<'bump, T = ()> {
|
||||||
pub(crate) origin: ScopeId,
|
pub(crate) origin: ScopeId,
|
||||||
pub(super) callback: RefCell<Option<ExternalListenerCallback<'bump, T>>>,
|
pub(super) callback: RefCell<Option<ExternalListenerCallback<'bump, T>>>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ where
|
|||||||
.flatten()
|
.flatten()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Runtime {
|
pub(crate) struct Runtime {
|
||||||
pub(crate) scope_contexts: RefCell<Vec<Option<ScopeContext>>>,
|
pub(crate) scope_contexts: RefCell<Vec<Option<ScopeContext>>>,
|
||||||
pub(crate) scheduler: Rc<Scheduler>,
|
pub(crate) scheduler: Rc<Scheduler>,
|
||||||
|
|
||||||
@@ -93,10 +93,10 @@ impl Runtime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RuntimeGuard(Rc<Runtime>);
|
pub(crate) struct RuntimeGuard(Rc<Runtime>);
|
||||||
|
|
||||||
impl RuntimeGuard {
|
impl RuntimeGuard {
|
||||||
pub fn new(runtime: Rc<Runtime>) -> Self {
|
pub(crate) fn new(runtime: Rc<Runtime>) -> Self {
|
||||||
push_runtime(runtime.clone());
|
push_runtime(runtime.clone());
|
||||||
Self(runtime)
|
Self(runtime)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ use std::{
|
|||||||
/// A component's state separate from its props.
|
/// A component's state separate from its props.
|
||||||
///
|
///
|
||||||
/// This struct exists to provide a common interface for all scopes without relying on generics.
|
/// This struct exists to provide a common interface for all scopes without relying on generics.
|
||||||
pub struct ScopeContext {
|
pub(crate) struct ScopeContext {
|
||||||
pub(crate) name: &'static str,
|
pub(crate) name: &'static str,
|
||||||
|
|
||||||
pub(crate) id: ScopeId,
|
pub(crate) id: ScopeId,
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ impl<'src> ScopeState {
|
|||||||
///
|
///
|
||||||
/// assert_eq!(base.parent(), None);
|
/// assert_eq!(base.parent(), None);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn parent_id(&self) -> Option<ScopeId> {
|
pub fn parent(&self) -> Option<ScopeId> {
|
||||||
// safety: the pointer to our parent is *always* valid thanks to the bump arena
|
// safety: the pointer to our parent is *always* valid thanks to the bump arena
|
||||||
self.context().parent_id()
|
self.context().parent_id()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,210 @@
|
|||||||
|
use dioxus_core::{ScopeId, ScopeState};
|
||||||
|
use slab::Slab;
|
||||||
|
use std::{
|
||||||
|
cell::{RefCell, RefMut},
|
||||||
|
collections::HashSet,
|
||||||
|
ops::{Deref, DerefMut},
|
||||||
|
rc::Rc,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Create a new tracked state.
|
||||||
|
/// Tracked state is state that can drive Selector state
|
||||||
|
///
|
||||||
|
/// It will efficiently update any Selector state that is reading from it, but it is not readable on its own.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use dioxus::prelude::*;
|
||||||
|
///
|
||||||
|
/// fn Parent(cx: Scope) -> Element {
|
||||||
|
/// let count = use_tracked_state(cx, || 0);
|
||||||
|
///
|
||||||
|
/// render! {
|
||||||
|
/// Child {
|
||||||
|
/// count: count.clone(),
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[inline_props]
|
||||||
|
/// fn Child(cx: Scope, count: Tracked<usize>) -> Element {
|
||||||
|
/// let less_than_five = use_selector(cx, count, |count| *count < 5);
|
||||||
|
///
|
||||||
|
/// render! {
|
||||||
|
/// "{less_than_five}"
|
||||||
|
/// }
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn use_tracked_state<T: 'static>(cx: &ScopeState, init: impl FnOnce() -> T) -> &Tracked<T> {
|
||||||
|
cx.use_hook(|| {
|
||||||
|
let init = init();
|
||||||
|
Tracked::new(cx, init)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tracked state is state that can drive Selector state
|
||||||
|
///
|
||||||
|
/// Tracked state will efficiently update any Selector state that is reading from it, but it is not readable on it's own.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Tracked<I> {
|
||||||
|
state: Rc<RefCell<I>>,
|
||||||
|
update_any: std::sync::Arc<dyn Fn(ScopeId)>,
|
||||||
|
subscribers: SubscribedCallbacks<I>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: PartialEq> PartialEq for Tracked<I> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.state == other.state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I> Tracked<I> {
|
||||||
|
/// Create a new tracked state
|
||||||
|
pub fn new(cx: &ScopeState, state: I) -> Self {
|
||||||
|
let subscribers = std::rc::Rc::new(std::cell::RefCell::new(Slab::new()));
|
||||||
|
Self {
|
||||||
|
state: Rc::new(RefCell::new(state)),
|
||||||
|
subscribers,
|
||||||
|
update_any: cx.schedule_update_any(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new Selector state from this tracked state
|
||||||
|
pub fn compute<O: PartialEq + 'static>(
|
||||||
|
&self,
|
||||||
|
mut compute: impl FnMut(&I) -> O + 'static,
|
||||||
|
) -> Selector<O, I> {
|
||||||
|
let subscribers = Rc::new(RefCell::new(HashSet::new()));
|
||||||
|
let state = Rc::new(RefCell::new(compute(&self.state.borrow())));
|
||||||
|
let update_any = self.update_any.clone();
|
||||||
|
|
||||||
|
Selector {
|
||||||
|
value: state.clone(),
|
||||||
|
subscribers: subscribers.clone(),
|
||||||
|
_tracker: Rc::new(self.track(move |input_state| {
|
||||||
|
let new = compute(input_state);
|
||||||
|
let different = {
|
||||||
|
let state = state.borrow();
|
||||||
|
*state != new
|
||||||
|
};
|
||||||
|
if different {
|
||||||
|
let mut state = state.borrow_mut();
|
||||||
|
*state = new;
|
||||||
|
for id in subscribers.borrow().iter().copied() {
|
||||||
|
(update_any)(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn track(&self, update: impl FnMut(&I) + 'static) -> Tracker<I> {
|
||||||
|
let mut subscribers = self.subscribers.borrow_mut();
|
||||||
|
let id = subscribers.insert(Box::new(update));
|
||||||
|
Tracker {
|
||||||
|
subscribers: self.subscribers.clone(),
|
||||||
|
id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write to the tracked state
|
||||||
|
pub fn write(&self) -> TrackedMut<'_, I> {
|
||||||
|
TrackedMut {
|
||||||
|
state: self.state.borrow_mut(),
|
||||||
|
subscribers: self.subscribers.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A mutable reference to tracked state
|
||||||
|
pub struct TrackedMut<'a, I> {
|
||||||
|
state: RefMut<'a, I>,
|
||||||
|
subscribers: SubscribedCallbacks<I>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, I> Deref for TrackedMut<'a, I> {
|
||||||
|
type Target = I;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, I> DerefMut for TrackedMut<'a, I> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.state
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, I> Drop for TrackedMut<'a, I> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let state = self.state.deref();
|
||||||
|
for (_, sub) in &mut *self.subscribers.borrow_mut() {
|
||||||
|
sub(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type SubscribedCallbacks<I> = std::rc::Rc<std::cell::RefCell<Slab<Box<dyn FnMut(&I) + 'static>>>>;
|
||||||
|
|
||||||
|
pub(crate) struct Tracker<I> {
|
||||||
|
subscribers: SubscribedCallbacks<I>,
|
||||||
|
id: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I> Drop for Tracker<I> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.subscribers.borrow_mut().remove(self.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn use_selector<I: 'static, O: Clone + PartialEq + 'static>(
|
||||||
|
cx: &ScopeState,
|
||||||
|
tracked: &Tracked<I>,
|
||||||
|
init: impl FnMut(&I) -> O + 'static,
|
||||||
|
) -> O {
|
||||||
|
let selector = cx.use_hook(|| tracked.compute(init));
|
||||||
|
selector.use_state(cx)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Selector state is state that is derived from tracked state
|
||||||
|
///
|
||||||
|
/// Whenever the tracked state changes, the Selector state will be updated and any components reading from it will be rerun
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Selector<T, I> {
|
||||||
|
_tracker: Rc<Tracker<I>>,
|
||||||
|
value: Rc<RefCell<T>>,
|
||||||
|
subscribers: Rc<RefCell<HashSet<ScopeId>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, I> PartialEq for Selector<T, I> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
std::rc::Rc::ptr_eq(&self.value, &other.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Clone + PartialEq, I> Selector<T, I> {
|
||||||
|
/// Read the Selector state and subscribe to updates
|
||||||
|
pub fn use_state(&self, cx: &ScopeState) -> T {
|
||||||
|
cx.use_hook(|| {
|
||||||
|
let id = cx.scope_id();
|
||||||
|
self.subscribers.borrow_mut().insert(id);
|
||||||
|
|
||||||
|
ComputedRead {
|
||||||
|
scope: cx.scope_id(),
|
||||||
|
subscribers: self.subscribers.clone(),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
self.value.borrow().clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ComputedRead {
|
||||||
|
scope: ScopeId,
|
||||||
|
subscribers: std::rc::Rc<std::cell::RefCell<std::collections::HashSet<ScopeId>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for ComputedRead {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.subscribers.borrow_mut().remove(&self.scope);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,9 @@ macro_rules! to_owned {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod computed;
|
||||||
|
pub use computed::*;
|
||||||
|
|
||||||
mod use_on_unmount;
|
mod use_on_unmount;
|
||||||
pub use use_on_unmount::*;
|
pub use use_on_unmount::*;
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user