From 597a0456f59872bd5dc60d382acdec76a98b1db2 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 13 Dec 2021 00:14:47 -0500 Subject: [PATCH] wip: update hooks --- packages/core/src/lib.rs | 8 ++++---- packages/hooks/src/use_shared_state.rs | 10 ++++++---- packages/hooks/src/usecoroutine.rs | 10 +++++----- packages/hooks/src/usemodel.rs | 13 +++++++++---- packages/hooks/src/useref.rs | 5 +++-- packages/hooks/src/usestate.rs | 5 +++-- 6 files changed, 30 insertions(+), 21 deletions(-) diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index eb40b4c7a..ba8d71e6e 100644 --- a/packages/core/src/lib.rs +++ b/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; diff --git a/packages/hooks/src/use_shared_state.rs b/packages/hooks/src/use_shared_state.rs index 1a52217de..ee4fba134 100644 --- a/packages/hooks/src/use_shared_state.rs +++ b/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 ProvidedStateInner { /// /// /// -pub fn use_shared_state<'a, T: 'static>(cx: Context<'a>) -> Option> { +pub fn use_shared_state<'a, T: 'static>(cx: &dyn AnyContext<'a>) -> Option> { + let cx = cx.get_scope(); cx.use_hook( |_| { let scope_id = cx.scope_id(); @@ -110,7 +111,7 @@ impl Drop for SharedStateInner { } pub struct UseSharedState<'a, T: 'static> { - pub(crate) cx: Context<'a>, + pub(crate) cx: &'a Scope, pub(crate) value: &'a Rc>, pub(crate) root: &'a Rc>>, pub(crate) needs_notification: &'a Cell, @@ -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 = RefCell::new(ProvidedStateInner { diff --git a/packages/hooks/src/usecoroutine.rs b/packages/hooks/src/usecoroutine.rs index 380d06284..b51172a1c 100644 --- a/packages/hooks/src/usecoroutine.rs +++ b/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 + '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<'_> { diff --git a/packages/hooks/src/usemodel.rs b/packages/hooks/src/usemodel.rs index de991df68..003fa357c 100644 --- a/packages/hooks/src/usemodel.rs +++ b/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(cx: Context, f: impl FnOnce() -> T) -> UseModel { +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 + 'static>( - cx: Context, +pub fn use_model_coroutine<'a, T, F: Future + 'static>( + cx: &dyn AnyContext<'a>, model: UseModel, f: impl FnOnce(AppModels) -> F, ) -> UseModelCoroutine { + let cx = cx.get_scope(); cx.use_hook( |_| { // diff --git a/packages/hooks/src/useref.rs b/packages/hooks/src/useref.rs index c1362f4da..243508660 100644 --- a/packages/hooks/src/useref.rs +++ b/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(cx: Context, f: impl FnOnce() -> T) -> UseRef { +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), diff --git a/packages/hooks/src/usestate.rs b/packages/hooks/src/usestate.rs index c1156f4a3..1629cdb60 100644 --- a/packages/hooks/src/usestate.rs +++ b/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();