wip: update hooks

This commit is contained in:
Jonathan Kelley committed 2021-12-13 00:14:47 -05:00
1 parent 96b3d56e09
commit 597a0456f5
6 files changed
+30 -21

No files matched your search

+4 -4
View File
@@ -25,16 +25,16 @@ pub(crate) mod innerlude {
} }
pub use crate::innerlude::{ pub use crate::innerlude::{
Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId, EventHandler, AnyContext, Attribute, Component, Context, DioxusElement, DomEdit, Element, ElementId,
EventPriority, IntoVNode, LazyNodes, Listener, Mutations, NodeFactory, Properties, EventHandler, EventPriority, IntoVNode, LazyNodes, Listener, Mutations, NodeFactory,
SchedulerMsg, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom, Properties, SchedulerMsg, Scope, ScopeId, UserEvent, VElement, VFragment, VNode, VirtualDom,
}; };
pub mod prelude { pub mod prelude {
pub use crate::component::{fc_to_builder, Fragment, Properties}; pub use crate::component::{fc_to_builder, Fragment, Properties};
pub use crate::innerlude::Context; pub use crate::innerlude::Context;
pub use crate::innerlude::{ 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::nodes::VNode;
pub use crate::VirtualDom; pub use crate::VirtualDom;
+6 -4
View File
@@ -1,4 +1,4 @@
use dioxus_core::{prelude::Context, ScopeId}; use dioxus_core::{prelude::Context, AnyContext, Scope, ScopeId};
use std::{ use std::{
cell::{Cell, Ref, RefCell, RefMut}, cell::{Cell, Ref, RefCell, RefMut},
collections::HashSet, 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( cx.use_hook(
|_| { |_| {
let scope_id = cx.scope_id(); let scope_id = cx.scope_id();
@@ -110,7 +111,7 @@ impl<T> Drop for SharedStateInner<T> {
} }
pub struct UseSharedState<'a, T: 'static> { 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) value: &'a Rc<RefCell<T>>,
pub(crate) root: &'a Rc<RefCell<ProvidedStateInner<T>>>, pub(crate) root: &'a Rc<RefCell<ProvidedStateInner<T>>>,
pub(crate) needs_notification: &'a Cell<bool>, 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( cx.use_hook(
|_| { |_| {
let state: ProvidedState<T> = RefCell::new(ProvidedStateInner { let state: ProvidedState<T> = RefCell::new(ProvidedStateInner {
+5 -5
View File
@@ -1,4 +1,4 @@
use dioxus_core::Context; use dioxus_core::{AnyContext, Context, Scope};
use futures::Future; use futures::Future;
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
@@ -7,10 +7,10 @@ use std::{
}; };
pub fn use_coroutine<'a, F: Future<Output = ()> + 'static>( pub fn use_coroutine<'a, F: Future<Output = ()> + 'static>(
cx: Context<'a>, cx: &dyn AnyContext<'a>,
mut f: impl FnMut() -> F + 'a, mut f: impl FnMut() -> F + 'a,
) -> CoroutineHandle { ) -> CoroutineHandle<'a> {
// let cx = cx.get_scope();
cx.use_hook( cx.use_hook(
move |_| State { move |_| State {
running: Default::default(), running: Default::default(),
@@ -56,7 +56,7 @@ struct State {
} }
pub struct CoroutineHandle<'a> { pub struct CoroutineHandle<'a> {
cx: Context<'a>, cx: &'a Scope,
inner: &'a State, inner: &'a State,
} }
impl Clone for CoroutineHandle<'_> { impl Clone for CoroutineHandle<'_> {
+9 -4
View File
@@ -4,7 +4,7 @@
//! //!
//! In these cases, we provide `use_model` - a convenient way of abstracting over some state and async functions. //! 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 futures::Future;
use std::{ use std::{
cell::{Cell, Ref, RefCell, RefMut}, cell::{Cell, Ref, RefCell, RefMut},
@@ -13,7 +13,11 @@ use std::{
rc::Rc, 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( cx.use_hook(
|_| UseModelInner { |_| UseModelInner {
update_scheduled: Cell::new(false), update_scheduled: Cell::new(false),
@@ -76,11 +80,12 @@ impl<'a, T: 'static> UseModel<'a, T> {
} }
// keep a coroutine going // keep a coroutine going
pub fn use_model_coroutine<T, F: Future<Output = ()> + 'static>( pub fn use_model_coroutine<'a, T, F: Future<Output = ()> + 'static>(
cx: Context, cx: &dyn AnyContext<'a>,
model: UseModel<T>, model: UseModel<T>,
f: impl FnOnce(AppModels) -> F, f: impl FnOnce(AppModels) -> F,
) -> UseModelCoroutine { ) -> UseModelCoroutine {
let cx = cx.get_scope();
cx.use_hook( cx.use_hook(
|_| { |_| {
// //
+3 -2
View File
@@ -3,9 +3,10 @@ use std::{
rc::Rc, 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( cx.use_hook(
|_| UseRefInner { |_| UseRefInner {
update_scheduled: Cell::new(false), update_scheduled: Cell::new(false),
+3 -2
View File
@@ -1,4 +1,4 @@
use dioxus_core::prelude::Context; use dioxus_core::prelude::*;
use std::{ use std::{
cell::{Cell, Ref, RefCell, RefMut}, cell::{Cell, Ref, RefCell, RefMut},
fmt::{Debug, Display}, fmt::{Debug, Display},
@@ -50,9 +50,10 @@ use std::{
/// } /// }
/// ``` /// ```
pub fn use_state<'a, T: 'static>( pub fn use_state<'a, T: 'static>(
cx: Context<'a>, cx: &dyn AnyContext<'a>,
initial_state_fn: impl FnOnce() -> T, initial_state_fn: impl FnOnce() -> T,
) -> UseState<'a, T> { ) -> UseState<'a, T> {
let cx = cx.get_scope();
cx.use_hook( cx.use_hook(
move |_| { move |_| {
let first_val = initial_state_fn(); let first_val = initial_state_fn();