Merge branch 'master' into intigrate-collect-assets
This commit is contained in:
1 parent
ffb5de91d1
commit
f976e08f71
379 files changed
+1962
-13517
No files matched your search
@@ -16,7 +16,7 @@ nightly-features = []
|
||||
[dependencies]
|
||||
dioxus-core = { workspace = true }
|
||||
futures-channel = { workspace = true }
|
||||
log = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
slab = { workspace = true }
|
||||
dioxus-debug-cell = "0.1.1"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
# Dioxus Hooks
|
||||
|
||||
|
||||
[![Crates.io][crates-badge]][crates-url]
|
||||
[![MIT licensed][mit-badge]][mit-url]
|
||||
[![Build Status][actions-badge]][actions-url]
|
||||
@@ -8,22 +7,18 @@
|
||||
|
||||
[crates-badge]: https://img.shields.io/crates/v/dioxus-hooks.svg
|
||||
[crates-url]: https://crates.io/crates/dioxus-hooks
|
||||
|
||||
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
|
||||
[mit-url]: https://github.com/dioxuslabs/dioxus/blob/master/LICENSE
|
||||
|
||||
[actions-badge]: https://github.com/dioxuslabs/dioxus/actions/workflows/main.yml/badge.svg
|
||||
[actions-url]: https://github.com/dioxuslabs/dioxus/actions?query=workflow%3ACI+branch%3Amaster
|
||||
|
||||
[discord-badge]: https://img.shields.io/discord/899851952891002890.svg?logo=discord&style=flat-square
|
||||
[discord-url]: https://discord.gg/XgGxMSkvUM
|
||||
|
||||
[Website](https://dioxuslabs.com) |
|
||||
[Guides](https://dioxuslabs.com/docs/0.3/guide/en/) |
|
||||
[Guides](https://dioxuslabs.com/learn/0.4/) |
|
||||
[API Docs](https://docs.rs/dioxus-hooks/latest/dioxus_hooks) |
|
||||
[Chat](https://discord.gg/XgGxMSkvUM)
|
||||
|
||||
|
||||
## Overview
|
||||
|
||||
`dioxus-hooks` includes some basic useful hooks for dioxus:
|
||||
@@ -42,13 +37,13 @@ This crate also provides a few helpful macros to get around some Rust lifetime m
|
||||
- `use_future!()`
|
||||
- `use_callback!()`
|
||||
|
||||
|
||||
## Contributing
|
||||
|
||||
- Report issues on our [issue tracker](https://github.com/dioxuslabs/dioxus/issues).
|
||||
- Join the discord and ask questions!
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the [MIT license].
|
||||
|
||||
[mit license]: https://github.com/DioxusLabs/dioxus/blob/master/LICENSE-MIT
|
||||
|
||||
@@ -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);
|
||||
///
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 })
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
@@ -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>(
|
||||
|
||||
@@ -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 })
|
||||
/// }
|
||||
/// ```
|
||||
|
||||
Reference in new issue
Block a user