Bläddra i källkod

wip: update hooks

Jonathan Kelley 3 år sedan
förälder
incheckning
597a045

+ 4 - 4
packages/core/src/lib.rs

@@ -25,16 +25,16 @@ pub(crate) mod innerlude {
 }
 
 pub use crate::innerlude::{
-    Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId, EventHandler,
-    EventPriority, IntoVNode, LazyNodes, Listener, Mutations, NodeFactory, Properties,
-    SchedulerMsg, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom,
+    AnyContext, Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId,
+    EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, Mutations, NodeFactory,
+    Properties, SchedulerMsg, Scope, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom,
 };
 
 pub mod prelude {
     pub use crate::component::{fc_to_builder, Fragment, Properties};
     pub use crate::innerlude::Context;
     pub use crate::innerlude::{
-        Component, DioxusElement, Element, EventHandler, LazyNodes, NodeFactory, Scope,
+        AnyContext, Component, DioxusElement, Element, EventHandler, LazyNodes, NodeFactory, Scope,
     };
     pub use crate::nodes::VNode;
     pub use crate::VirtualDom;

+ 6 - 4
packages/hooks/src/use_shared_state.rs

@@ -1,4 +1,4 @@
-use dioxus_core::{prelude::Context, ScopeId};
+use dioxus_core::{prelude::Context, AnyContext, Scope, ScopeId};
 use std::{
     cell::{Cell, Ref, RefCell, RefMut},
     collections::HashSet,
@@ -59,7 +59,8 @@ impl<T> ProvidedStateInner<T> {
 ///
 ///
 ///
-pub fn use_shared_state<'a, T: 'static>(cx: Context<'a>) -> Option<UseSharedState<'a, T>> {
+pub fn use_shared_state<'a, T: 'static>(cx: &dyn AnyContext<'a>) -> Option<UseSharedState<'a, T>> {
+    let cx = cx.get_scope();
     cx.use_hook(
         |_| {
             let scope_id = cx.scope_id();
@@ -110,7 +111,7 @@ impl<T> Drop for SharedStateInner<T> {
 }
 
 pub struct UseSharedState<'a, T: 'static> {
-    pub(crate) cx: Context<'a>,
+    pub(crate) cx: &'a Scope,
     pub(crate) value: &'a Rc<RefCell<T>>,
     pub(crate) root: &'a Rc<RefCell<ProvidedStateInner<T>>>,
     pub(crate) needs_notification: &'a Cell<bool>,
@@ -175,7 +176,8 @@ where
 ///
 ///
 ///
-pub fn use_provide_state<'a, T: 'static>(cx: Context<'a>, f: impl FnOnce() -> T) {
+pub fn use_provide_state<'a, T: 'static>(cx: &dyn AnyContext<'a>, f: impl FnOnce() -> T) {
+    let cx = cx.get_scope();
     cx.use_hook(
         |_| {
             let state: ProvidedState<T> = RefCell::new(ProvidedStateInner {

+ 5 - 5
packages/hooks/src/usecoroutine.rs

@@ -1,4 +1,4 @@
-use dioxus_core::Context;
+use dioxus_core::{AnyContext, Context, Scope};
 use futures::Future;
 use std::{
     cell::{Cell, RefCell},
@@ -7,10 +7,10 @@ use std::{
 };
 
 pub fn use_coroutine<'a, F: Future<Output = ()> + 'static>(
-    cx: Context<'a>,
+    cx: &dyn AnyContext<'a>,
     mut f: impl FnMut() -> F + 'a,
-) -> CoroutineHandle {
-    //
+) -> CoroutineHandle<'a> {
+    let cx = cx.get_scope();
     cx.use_hook(
         move |_| State {
             running: Default::default(),
@@ -56,7 +56,7 @@ struct State {
 }
 
 pub struct CoroutineHandle<'a> {
-    cx: Context<'a>,
+    cx: &'a Scope,
     inner: &'a State,
 }
 impl Clone for CoroutineHandle<'_> {

+ 9 - 4
packages/hooks/src/usemodel.rs

@@ -4,7 +4,7 @@
 //!
 //! In these cases, we provide `use_model` - a convenient way of abstracting over some state and async functions.
 
-use dioxus_core::prelude::Context;
+use dioxus_core::prelude::{AnyContext, Context};
 use futures::Future;
 use std::{
     cell::{Cell, Ref, RefCell, RefMut},
@@ -13,7 +13,11 @@ use std::{
     rc::Rc,
 };
 
-pub fn use_model<T: 'static>(cx: Context, f: impl FnOnce() -> T) -> UseModel<T> {
+pub fn use_model<'a, T: 'static>(
+    cx: &dyn AnyContext<'a>,
+    f: impl FnOnce() -> T,
+) -> UseModel<'a, T> {
+    let cx = cx.get_scope();
     cx.use_hook(
         |_| UseModelInner {
             update_scheduled: Cell::new(false),
@@ -76,11 +80,12 @@ impl<'a, T: 'static> UseModel<'a, T> {
 }
 
 // keep a coroutine going
-pub fn use_model_coroutine<T, F: Future<Output = ()> + 'static>(
-    cx: Context,
+pub fn use_model_coroutine<'a, T, F: Future<Output = ()> + 'static>(
+    cx: &dyn AnyContext<'a>,
     model: UseModel<T>,
     f: impl FnOnce(AppModels) -> F,
 ) -> UseModelCoroutine {
+    let cx = cx.get_scope();
     cx.use_hook(
         |_| {
             //

+ 3 - 2
packages/hooks/src/useref.rs

@@ -3,9 +3,10 @@ use std::{
     rc::Rc,
 };
 
-use dioxus_core::Context;
+use dioxus_core::AnyContext;
 
-pub fn use_ref<T: 'static>(cx: Context, f: impl FnOnce() -> T) -> UseRef<T> {
+pub fn use_ref<'a, T: 'static>(cx: &dyn AnyContext<'a>, f: impl FnOnce() -> T) -> UseRef<'a, T> {
+    let cx = cx.get_scope();
     cx.use_hook(
         |_| UseRefInner {
             update_scheduled: Cell::new(false),

+ 3 - 2
packages/hooks/src/usestate.rs

@@ -1,4 +1,4 @@
-use dioxus_core::prelude::Context;
+use dioxus_core::prelude::*;
 use std::{
     cell::{Cell, Ref, RefCell, RefMut},
     fmt::{Debug, Display},
@@ -50,9 +50,10 @@ use std::{
 /// }
 /// ```
 pub fn use_state<'a, T: 'static>(
-    cx: Context<'a>,
+    cx: &dyn AnyContext<'a>,
     initial_state_fn: impl FnOnce() -> T,
 ) -> UseState<'a, T> {
+    let cx = cx.get_scope();
     cx.use_hook(
         move |_| {
             let first_val = initial_state_fn();