Browse Source

remove once, restore use_hook

Evan Almloff 1 year ago
parent
commit
01726d6656

+ 1 - 1
packages/core/src/error_boundary.rs

@@ -15,7 +15,7 @@ use std::{
 
 /// Provide an error boundary to catch errors from child components
 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

+ 2 - 6
packages/core/src/global_context.rs

@@ -92,15 +92,11 @@ pub fn remove_future(id: Task) {
 ///
 /// // prints a greeting on the initial render
 /// 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 {
-    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

+ 1 - 1
packages/core/tests/lifecycle.rs

@@ -81,7 +81,7 @@ fn events_generate() {
 // #[test]
 // fn components_generate() {
 //     fn app() -> Element {
-//         let render_phase = once(|| 0);
+//         let render_phase = use_hook(|| 0);
 //         *render_phase += 1;
 
 //         match *render_phase {

+ 4 - 4
packages/core/tests/miri_simple.rs

@@ -16,10 +16,10 @@ fn app_drops() {
 #[test]
 fn hooks_drop() {
     fn app() -> Element {
-        once(|| String::from("asd"));
-        once(|| String::from("asd"));
-        once(|| String::from("asd"));
-        once(|| String::from("asd"));
+        use_hook(|| String::from("asd"));
+        use_hook(|| String::from("asd"));
+        use_hook(|| String::from("asd"));
+        use_hook(|| String::from("asd"));
 
         render! { div {} }
     }

+ 5 - 5
packages/core/tests/miri_stress.rs

@@ -18,7 +18,7 @@ fn test_memory_leak() {
             return render!({});
         }
 
-        let mut name = once(|| String::from("numbers: "));
+        let mut name = use_hook(|| String::from("numbers: "));
 
         name.push_str("123 ");
 
@@ -76,7 +76,7 @@ fn memo_works_properly() {
             return None;
         }
 
-        let name = once(|| String::from("asd"));
+        let name = use_hook(|| String::from("asd"));
 
         render!(
             div { "Hello, world! {name}" }
@@ -117,7 +117,7 @@ fn free_works_on_root_hooks() {
     }
 
     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() })
     }
 
@@ -146,14 +146,14 @@ fn supports_async() {
         let colors = use_signal(|| vec!["green", "blue", "red"]);
         let padding = use_signal(|| 10);
 
-        once(|| {
+        use_hook(|| {
             spawn(async move {
                 sleep(Duration::from_millis(1000)).await;
                 colors.with_mut(|colors| colors.reverse());
             })
         });
 
-        once(|| {
+        use_hook(|| {
             spawn(async move {
                 sleep(Duration::from_millis(10)).await;
                 padding.with_mut(|padding| {

+ 1 - 1
packages/core/tests/task.rs

@@ -9,7 +9,7 @@ async fn it_works() {
     static POLL_COUNT: AtomicUsize = AtomicUsize::new(0);
 
     fn app() -> Element {
-        once(|| {
+        use_hook(|| {
             spawn(async {
                 for x in 0..10 {
                     tokio::time::sleep(Duration::from_micros(50)).await;

+ 2 - 2
packages/signals/src/effect.rs

@@ -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.
 /// 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) {
-    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.
@@ -51,7 +51,7 @@ pub fn use_effect_with_dependencies<D: Dependency>(
     D::Out: 'static,
 {
     let dependencies_signal = use_signal(|| dependencies.out());
-    once(|| {
+    use_hook(|| {
         Effect::new(move || {
             let deref = &*dependencies_signal.read();
             callback(deref.clone());

+ 2 - 2
packages/signals/src/selector.rs

@@ -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"]
 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
@@ -51,7 +51,7 @@ where
     D::Out: 'static,
 {
     let dependencies_signal = use_signal(|| dependencies.out());
-    let selector = once(|| {
+    let selector = use_hook(|| {
         selector(move || {
             let deref = &*dependencies_signal.read();
             f(deref.clone())

+ 1 - 1
packages/signals/src/signal.rs

@@ -52,7 +52,7 @@ pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T> {
     #[cfg(debug_assertions)]
     let caller = std::panic::Location::caller();
 
-    once(|| {
+    use_hook(|| {
         Signal::new_with_caller(
             f(),
             #[cfg(debug_assertions)]