Merge branch 'master' into fix-event-bubbling

This commit is contained in:
Evan Almloff committed 2023-09-17 20:00:22 -05:00
1 parent c8fca7b321
commit 98335bd9a6
368 files changed
+1877 -13498

No files matched your search

+2 -1
View File
@@ -17,6 +17,7 @@ use std::{
/// ```rust
/// use dioxus::prelude::*;
///
/// #[component]
/// fn Parent(cx: Scope) -> Element {
/// let count = use_tracked_state(cx, || 0);
///
@@ -27,7 +28,7 @@ use std::{
/// }
/// }
///
/// #[inline_props]
/// #[component]
/// fn Child(cx: Scope, count: Tracked<usize>) -> Element {
/// let less_than_five = use_selector(cx, count, |count| *count < 5);
///
+14 -7
View File
@@ -81,10 +81,12 @@ pub(crate) struct ProvidedStateInner<T> {
value: T,
notify_any: Arc<dyn Fn(ScopeId)>,
consumers: HashSet<ScopeId>,
gen: usize,
}
impl<T> ProvidedStateInner<T> {
pub(crate) fn notify_consumers(&mut self) {
self.gen += 1;
for consumer in self.consumers.iter() {
(self.notify_any)(*consumer);
}
@@ -157,7 +159,7 @@ impl<T> ProvidedStateInner<T> {
///
/// Right now, there is not a distinction between read-only and write-only, so every consumer will be notified.
pub fn use_shared_state<T: 'static>(cx: &ScopeState) -> Option<&UseSharedState<T>> {
let state: &Option<UseSharedStateOwner<T>> = &*cx.use_hook(move || {
let state_owner: &mut Option<UseSharedStateOwner<T>> = &mut *cx.use_hook(move || {
let scope_id = cx.scope_id();
let root = cx.consume_context::<ProvidedState<T>>()?;
@@ -167,7 +169,10 @@ pub fn use_shared_state<T: 'static>(cx: &ScopeState) -> Option<&UseSharedState<T
let owner = UseSharedStateOwner { state, scope_id };
Some(owner)
});
state.as_ref().map(|s| &s.state)
state_owner.as_mut().map(|s| {
s.state.gen = s.state.inner.borrow().gen;
&s.state
})
}
/// This wrapper detects when the hook is dropped and will unsubscribe when the component is unmounted
@@ -187,11 +192,13 @@ impl<T> Drop for UseSharedStateOwner<T> {
/// State that is shared between components through the context system
pub struct UseSharedState<T> {
pub(crate) inner: Rc<RefCell<ProvidedStateInner<T>>>,
gen: usize,
}
impl<T> UseSharedState<T> {
fn new(inner: Rc<RefCell<ProvidedStateInner<T>>>) -> Self {
Self { inner }
let gen = inner.borrow().gen;
Self { inner, gen }
}
/// Notify all consumers of the state that it has changed. (This is called automatically when you call "write")
@@ -302,15 +309,14 @@ impl<T> Clone for UseSharedState<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
gen: self.gen,
}
}
}
impl<T: PartialEq> PartialEq for UseSharedState<T> {
impl<T> PartialEq for UseSharedState<T> {
fn eq(&self, other: &Self) -> bool {
let first = self.inner.borrow();
let second = other.inner.borrow();
first.value == second.value
self.gen == other.gen
}
}
@@ -360,6 +366,7 @@ pub fn use_shared_state_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -
value: f(),
notify_any: cx.schedule_update_any(),
consumers: HashSet::new(),
gen: 0,
}));
cx.provide_context(state);
+4 -2
View File
@@ -14,7 +14,8 @@ use crate::UseFutureDep;
/// ## Examples
///
/// ```rust, no_run
/// #[inline_props]
/// # use dioxus::prelude::*;
/// #[component]
/// fn Profile(cx: Scope, id: usize) -> Element {
/// let name = use_state(cx, || None);
///
@@ -34,7 +35,8 @@ use crate::UseFutureDep;
/// )
/// }
///
/// fn app(cx: Scope) -> Element {
/// #[component]
/// fn App(cx: Scope) -> Element {
/// render!(Profile { id: 0 })
/// }
/// ```
+1 -1
View File
@@ -14,7 +14,7 @@ use crate::{use_state, UseState};
///
/// Whenever the hooks dependencies change, the future will be re-evaluated.
/// If a future is pending when the dependencies change, the previous future
/// will be allowed to continue
/// will be canceled before the new one is started.
///
/// - dependencies: a tuple of references to values that are PartialEq + Clone
pub fn use_future<T, F, D>(
+5 -2
View File
@@ -10,8 +10,9 @@ use crate::UseFutureDep;
/// ## Examples
///
/// ```rust, no_run
/// # use dioxus::prelude::*;
///
/// #[inline_props]
/// #[component]
/// fn Calculator(cx: Scope, number: usize) -> Element {
/// let bigger_number = use_memo(cx, (number,), |(number,)| {
/// // This will only be calculated when `number` has changed.
@@ -21,7 +22,9 @@ use crate::UseFutureDep;
/// p { "{bigger_number}" }
/// )
/// }
/// fn app(cx: Scope) -> Element {
///
/// #[component]
/// fn App(cx: Scope) -> Element {
/// render!(Calculator { number: 0 })
/// }
/// ```