remove once, restore use_hook

This commit is contained in:
Evan Almloff committed 2024-01-15 09:05:46 -06:00
1 parent 36e9eb1160
commit 01726d6656
9 files changed
+19 -23

No files matched your search

+1 -1
View File
@@ -15,7 +15,7 @@ use std::{
/// Provide an error boundary to catch errors from child components /// Provide an error boundary to catch errors from child components
pub fn use_error_boundary() -> ErrorBoundary { pub fn use_error_boundary() -> ErrorBoundary {
once(|| provide_context(ErrorBoundary::new())) use_hook(|| provide_context(ErrorBoundary::new()))
} }
/// A boundary that will capture any errors from child components /// A boundary that will capture any errors from child components
+2 -6
View File
@@ -92,15 +92,11 @@ pub fn remove_future(id: Task) {
/// ///
/// // prints a greeting on the initial render /// // prints a greeting on the initial render
/// pub fn use_hello_world() { /// pub fn use_hello_world() {
/// once(|| println!("Hello, world!")); /// use_hook(|| println!("Hello, world!"));
/// } /// }
/// ``` /// ```
pub fn once<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State {
with_current_scope(|cx| cx.use_hook(initializer)).expect("to be in a dioxus runtime")
}
pub fn use_hook<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State { pub fn use_hook<State: Clone + 'static>(initializer: impl FnOnce() -> State) -> State {
once(initializer) with_current_scope(|cx| cx.use_hook(initializer)).expect("to be in a dioxus runtime")
} }
/// Get the current render since the inception of this component /// Get the current render since the inception of this component
+1 -1
View File
@@ -81,7 +81,7 @@ fn events_generate() {
// #[test] // #[test]
// fn components_generate() { // fn components_generate() {
// fn app() -> Element { // fn app() -> Element {
// let render_phase = once(|| 0); // let render_phase = use_hook(|| 0);
// *render_phase += 1; // *render_phase += 1;
// match *render_phase { // match *render_phase {
+4 -4
View File
@@ -16,10 +16,10 @@ fn app_drops() {
#[test] #[test]
fn hooks_drop() { fn hooks_drop() {
fn app() -> Element { fn app() -> Element {
once(|| String::from("asd")); use_hook(|| String::from("asd"));
once(|| String::from("asd")); use_hook(|| String::from("asd"));
once(|| String::from("asd")); use_hook(|| String::from("asd"));
once(|| String::from("asd")); use_hook(|| String::from("asd"));
render! { div {} } render! { div {} }
} }
+5 -5
View File
@@ -18,7 +18,7 @@ fn test_memory_leak() {
return render!({}); return render!({});
} }
let mut name = once(|| String::from("numbers: ")); let mut name = use_hook(|| String::from("numbers: "));
name.push_str("123 "); name.push_str("123 ");
@@ -76,7 +76,7 @@ fn memo_works_properly() {
return None; return None;
} }
let name = once(|| String::from("asd")); let name = use_hook(|| String::from("asd"));
render!( render!(
div { "Hello, world! {name}" } div { "Hello, world! {name}" }
@@ -117,7 +117,7 @@ fn free_works_on_root_hooks() {
} }
fn app(cx: AppProps) -> Element { fn app(cx: AppProps) -> Element {
let name: AppProps = once(|| cx.clone()); let name: AppProps = use_hook(|| cx.clone());
render!(child_component { inner: name.inner.clone() }) render!(child_component { inner: name.inner.clone() })
} }
@@ -146,14 +146,14 @@ fn supports_async() {
let colors = use_signal(|| vec!["green", "blue", "red"]); let colors = use_signal(|| vec!["green", "blue", "red"]);
let padding = use_signal(|| 10); let padding = use_signal(|| 10);
once(|| { use_hook(|| {
spawn(async move { spawn(async move {
sleep(Duration::from_millis(1000)).await; sleep(Duration::from_millis(1000)).await;
colors.with_mut(|colors| colors.reverse()); colors.with_mut(|colors| colors.reverse());
}) })
}); });
once(|| { use_hook(|| {
spawn(async move { spawn(async move {
sleep(Duration::from_millis(10)).await; sleep(Duration::from_millis(10)).await;
padding.with_mut(|padding| { padding.with_mut(|padding| {
+1 -1
View File
@@ -9,7 +9,7 @@ async fn it_works() {
static POLL_COUNT: AtomicUsize = AtomicUsize::new(0); static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
fn app() -> Element { fn app() -> Element {
once(|| { use_hook(|| {
spawn(async { spawn(async {
for x in 0..10 { for x in 0..10 {
tokio::time::sleep(Duration::from_micros(50)).await; tokio::time::sleep(Duration::from_micros(50)).await;
+2 -2
View File
@@ -39,7 +39,7 @@ pub(crate) fn get_effect_stack() -> EffectStack {
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes. /// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
/// The signal will be owned by the current component and will be dropped when the component is dropped. /// The signal will be owned by the current component and will be dropped when the component is dropped.
pub fn use_effect(callback: impl FnMut() + 'static) { pub fn use_effect(callback: impl FnMut() + 'static) {
once(|| Effect::new(callback)); use_hook(|| Effect::new(callback));
} }
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes. /// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
@@ -51,7 +51,7 @@ pub fn use_effect_with_dependencies<D: Dependency>(
D::Out: 'static, D::Out: 'static,
{ {
let dependencies_signal = use_signal(|| dependencies.out()); let dependencies_signal = use_signal(|| dependencies.out());
once(|| { use_hook(|| {
Effect::new(move || { Effect::new(move || {
let deref = &*dependencies_signal.read(); let deref = &*dependencies_signal.read();
callback(deref.clone()); callback(deref.clone());
+2 -2
View File
@@ -23,7 +23,7 @@ use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySig
/// ``` /// ```
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"] #[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
pub fn use_selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> { pub fn use_selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
once(|| selector(f)) use_hook(|| selector(f))
} }
/// Creates a new Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes /// Creates a new Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
@@ -51,7 +51,7 @@ where
D::Out: 'static, D::Out: 'static,
{ {
let dependencies_signal = use_signal(|| dependencies.out()); let dependencies_signal = use_signal(|| dependencies.out());
let selector = once(|| { let selector = use_hook(|| {
selector(move || { selector(move || {
let deref = &*dependencies_signal.read(); let deref = &*dependencies_signal.read();
f(deref.clone()) f(deref.clone())
+1 -1
View File
@@ -52,7 +52,7 @@ pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T> {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
let caller = std::panic::Location::caller(); let caller = std::panic::Location::caller();
once(|| { use_hook(|| {
Signal::new_with_caller( Signal::new_with_caller(
f(), f(),
#[cfg(debug_assertions)] #[cfg(debug_assertions)]