wip: examples

This commit is contained in:
Jonathan Kelley committed 2021-09-21 12:11:04 -04:00
1 parent adf202eab9
commit f1cff845ce
10 files changed
+80 -26

No files matched your search

+4 -1
View File
@@ -1,2 +1,5 @@
mod usestate;
pub use usestate::use_state;
pub use usestate::{use_state, AsyncUseState, UseState};
mod useref;
pub use useref::*;
+38
View File
@@ -0,0 +1,38 @@
use std::cell::{Ref, RefCell, RefMut};
use dioxus_core::Context;
pub struct UseRef<'a, T> {
inner: &'a RefCell<T>,
}
impl<'a, T> UseRef<'a, T> {
pub fn read(&self) -> Ref<'_, T> {
self.inner.borrow()
}
pub fn read_write(&self) -> (Ref<'_, T>, &Self) {
(self.read(), self)
}
/// Calling "write" will force the component to re-render
pub fn write(&self) -> RefMut<'_, T> {
self.inner.borrow_mut()
}
/// Allows the ability to write the value without forcing a re-render
pub fn write_silent(&self) -> RefMut<'_, T> {
self.inner.borrow_mut()
}
}
impl<T> Clone for UseRef<'_, T> {
fn clone(&self) -> Self {
Self { inner: self.inner }
}
}
impl<T> Copy for UseRef<'_, T> {}
pub fn use_ref<P, T: 'static>(cx: Context<P>, f: impl FnOnce() -> T) -> UseRef<T> {
cx.use_hook(|_| RefCell::new(f()), |f| UseRef { inner: f }, |_| {})
}
+12 -1
View File
@@ -82,6 +82,7 @@ struct UseStateInner<T: 'static> {
pub struct UseState<'a, T: 'static> {
inner: &'a UseStateInner<T>,
}
impl<T> Copy for UseState<'_, T> {}
impl<'a, T> Clone for UseState<'a, T>
where
@@ -115,6 +116,11 @@ impl<'a, T: 'static> UseState<'a, T> {
self.inner.wip.borrow()
}
/// Get the current status of the work-in-progress data
pub fn get_wip_mut(&self) -> RefMut<Option<T>> {
self.inner.wip.borrow_mut()
}
pub fn classic(self) -> (&'a T, &'a Rc<dyn Fn(T)>) {
todo!()
}
@@ -136,7 +142,12 @@ impl<'a, T: 'static> UseState<'a, T> {
}
impl<'a, T: 'static + ToOwned<Owned = T>> UseState<'a, T> {
pub fn get_mut(self) -> RefMut<'a, T> {
/// Gain mutable access to the new value. This method is only available when the value is a `ToOwned` type.
///
/// Mutable access is derived by calling "ToOwned" (IE cloning) on the current value.
///
/// To get a reference to the current value, use `.get()`
pub fn modify(self) -> RefMut<'a, T> {
// make sure we get processed
self.needs_update();