dioxus/packages/hooks/src/usestate.rs

316 lines
9.2 KiB
Rust
Raw Normal View History

2021-12-13 05:14:47 +00:00
use dioxus_core::prelude::*;
2021-07-09 15:54:07 +00:00
use std::{
cell::{Cell, Ref, RefCell, RefMut},
2021-11-29 16:10:40 +00:00
fmt::{Debug, Display},
2021-10-11 19:35:20 +00:00
ops::Not,
2021-07-09 15:54:07 +00:00
rc::Rc,
};
2021-12-15 02:46:19 +00:00
pub trait UseStateA<'a, T> {
fn use_state(&self, initial_state_fn: impl FnOnce() -> T) -> UseState<'a, T>;
}
impl<'a, P, T> UseStateA<'a, T> for Scope<'a, P> {
fn use_state(&self, initial_state_fn: impl FnOnce() -> T) -> UseState<'a, T> {
use_state(self.scope, initial_state_fn)
}
}
2021-07-09 15:54:07 +00:00
/// Store state between component renders!
///
2021-12-10 02:19:31 +00:00
/// ## Dioxus equivalent of useState, designed for Rust
2021-07-09 15:54:07 +00:00
///
2021-12-15 02:46:19 +00:00
/// The Dioxus version of `useState` for state management inside components. It allows you to ergonomically store and
2021-07-09 15:54:07 +00:00
/// modify state between component renders. When the state is updated, the component will re-render.
///
/// Dioxus' use_state basically wraps a RefCell with helper methods and integrates it with the VirtualDOM update system.
///
/// [`use_state`] exposes a few helper methods to modify the underlying state:
/// - `.set(new)` allows you to override the "work in progress" value with a new value
/// - `.get_mut()` allows you to modify the WIP value
/// - `.get_wip()` allows you to access the WIP value
/// - `.deref()` provides the previous value (often done implicitly, though a manual dereference with `*` might be required)
///
/// Additionally, a ton of std::ops traits are implemented for the `UseState` wrapper, meaning any mutative type operations
/// will automatically be called on the WIP value.
///
/// ## Combinators
///
/// On top of the methods to set/get state, `use_state` also supports fancy combinators to extend its functionality:
/// - `.classic()` and `.split()` convert the hook into the classic React-style hook
/// ```rust
/// let (state, set_state) = use_state(&cx, || 10).split()
/// ```
///
2021-07-09 15:54:07 +00:00
///
/// Usage:
2021-12-15 02:46:19 +00:00
///
2021-07-09 15:54:07 +00:00
/// ```ignore
2021-12-15 02:46:19 +00:00
/// const Example: Component<()> = |cx| {
/// let counter = use_state(&cx, || 0);
///
/// cx.render(rsx! {
/// div {
/// h1 { "Counter: {counter}" }
/// button { onclick: move |_| counter += 1, "Increment" }
/// button { onclick: move |_| counter -= 1, "Decrement" }
/// }
/// ))
2021-07-09 15:54:07 +00:00
/// }
/// ```
2021-09-25 00:11:30 +00:00
pub fn use_state<'a, T: 'static>(
cx: &'a ScopeState,
2021-09-24 04:05:56 +00:00
initial_state_fn: impl FnOnce() -> T,
2021-07-27 04:27:07 +00:00
) -> UseState<'a, T> {
2021-07-09 15:54:07 +00:00
cx.use_hook(
2021-12-13 00:47:13 +00:00
move |_| {
let first_val = initial_state_fn();
UseStateInner {
current_val: Rc::new(first_val),
update_callback: cx.schedule_update(),
wip: Rc::new(RefCell::new(None)),
update_scheuled: Cell::new(false),
}
2021-07-09 15:54:07 +00:00
},
move |hook| {
hook.update_scheuled.set(false);
2021-12-10 02:19:31 +00:00
2021-07-09 15:54:07 +00:00
let mut new_val = hook.wip.borrow_mut();
if new_val.is_some() {
2021-12-13 00:47:13 +00:00
// if there's only one reference (weak or otherwise), we can just swap the values
if let Some(val) = Rc::get_mut(&mut hook.current_val) {
*val = new_val.take().unwrap();
} else {
hook.current_val = Rc::new(new_val.take().unwrap());
}
2021-07-09 15:54:07 +00:00
}
UseState { inner: &*hook }
},
)
}
struct UseStateInner<T: 'static> {
2021-12-13 00:47:13 +00:00
current_val: Rc<T>,
2021-07-09 15:54:07 +00:00
update_scheuled: Cell<bool>,
update_callback: Rc<dyn Fn()>,
wip: Rc<RefCell<Option<T>>>,
2021-07-09 15:54:07 +00:00
}
pub struct UseState<'a, T: 'static> {
inner: &'a UseStateInner<T>,
}
2021-09-21 16:11:04 +00:00
2021-07-09 15:54:07 +00:00
impl<T> Copy for UseState<'_, T> {}
impl<'a, T> Clone for UseState<'a, T>
where
T: 'static,
{
fn clone(&self) -> Self {
UseState { inner: self.inner }
}
}
2021-11-29 16:10:40 +00:00
impl<T: Debug> Debug for UseState<'_, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.inner.current_val)
}
}
2021-07-09 15:54:07 +00:00
impl<'a, T: 'static> UseState<'a, T> {
/// Tell the Dioxus Scheduler that we need to be processed
pub fn needs_update(&self) {
if !self.inner.update_scheuled.get() {
self.inner.update_scheuled.set(true);
(self.inner.update_callback)();
2021-07-09 15:54:07 +00:00
}
}
2021-11-03 19:13:50 +00:00
pub fn set(&self, new_val: T) {
2021-07-09 15:54:07 +00:00
*self.inner.wip.borrow_mut() = Some(new_val);
self.needs_update();
2021-07-09 15:54:07 +00:00
}
2021-07-20 23:03:49 +00:00
pub fn get(&self) -> &'a T {
2021-07-09 15:54:07 +00:00
&self.inner.current_val
}
2021-12-13 00:47:13 +00:00
pub fn get_rc(&self) -> &'a Rc<T> {
&self.inner.current_val
}
2021-07-09 15:54:07 +00:00
/// Get the current status of the work-in-progress data
pub fn get_wip(&self) -> Ref<Option<T>> {
self.inner.wip.borrow()
}
2021-09-21 16:11:04 +00:00
/// Get the current status of the work-in-progress data
pub fn get_wip_mut(&self) -> RefMut<Option<T>> {
self.inner.wip.borrow_mut()
}
2021-10-11 02:27:08 +00:00
pub fn classic(self) -> (&'a T, Rc<dyn Fn(T)>) {
(&self.inner.current_val, self.setter())
2021-07-09 15:54:07 +00:00
}
pub fn setter(&self) -> Rc<dyn Fn(T)> {
let slot = self.inner.wip.clone();
2021-11-16 06:25:38 +00:00
Rc::new(move |new| {
*slot.borrow_mut() = Some(new);
})
}
2021-07-16 20:11:25 +00:00
pub fn for_async(&self) -> AsyncUseState<T> {
AsyncUseState {
2021-12-10 02:19:31 +00:00
re_render: self.inner.update_callback.clone(),
2021-07-16 20:11:25 +00:00
wip: self.inner.wip.clone(),
2021-12-13 00:47:13 +00:00
inner: self.inner.current_val.clone(),
2021-07-16 20:11:25 +00:00
}
}
2021-07-21 21:05:48 +00:00
pub fn split_for_async(&'a self) -> (&'a Self, AsyncUseState<T>) {
(self, self.for_async())
}
2021-07-09 15:54:07 +00:00
}
2021-07-16 20:11:25 +00:00
2021-07-09 15:54:07 +00:00
impl<'a, T: 'static + ToOwned<Owned = T>> UseState<'a, T> {
/// Gain mutable access to the new value via RefMut.
///
/// If `modify` is called, then the component will re-render.
///
/// This method is only available when the value is a `ToOwned` type.
2021-09-21 16:11:04 +00:00
///
/// 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> {
2021-07-09 15:54:07 +00:00
// make sure we get processed
self.needs_update();
// Bring out the new value, cloning if it we need to
// "get_mut" is locked behind "ToOwned" to make it explicit that cloning occurs to use this
RefMut::map(self.inner.wip.borrow_mut(), |slot| {
if slot.is_none() {
2021-12-13 00:47:13 +00:00
*slot = Some(self.inner.current_val.as_ref().to_owned());
2021-07-09 15:54:07 +00:00
}
slot.as_mut().unwrap()
})
}
2021-12-10 02:19:31 +00:00
pub fn inner(self) -> T {
2021-12-13 00:47:13 +00:00
self.inner.current_val.as_ref().to_owned()
2021-12-10 02:19:31 +00:00
}
2021-07-09 15:54:07 +00:00
}
2021-07-21 21:05:48 +00:00
impl<'a, T> std::ops::Deref for UseState<'a, T> {
2021-07-09 15:54:07 +00:00
type Target = T;
fn deref(&self) -> &Self::Target {
2021-07-21 21:05:48 +00:00
self.get()
2021-07-09 15:54:07 +00:00
}
}
2021-07-21 21:05:48 +00:00
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
2021-07-16 04:27:06 +00:00
2021-07-09 15:54:07 +00:00
impl<'a, T: Copy + Add<T, Output = T>> Add<T> for UseState<'a, T> {
type Output = T;
fn add(self, rhs: T) -> Self::Output {
self.inner.current_val.add(rhs)
}
}
impl<'a, T: Copy + Add<T, Output = T>> AddAssign<T> for UseState<'a, T> {
fn add_assign(&mut self, rhs: T) {
self.set(self.inner.current_val.add(rhs));
}
}
impl<'a, T: Copy + Sub<T, Output = T>> Sub<T> for UseState<'a, T> {
type Output = T;
fn sub(self, rhs: T) -> Self::Output {
self.inner.current_val.sub(rhs)
}
}
impl<'a, T: Copy + Sub<T, Output = T>> SubAssign<T> for UseState<'a, T> {
fn sub_assign(&mut self, rhs: T) {
self.set(self.inner.current_val.sub(rhs));
}
}
2021-07-16 20:11:25 +00:00
/// MUL
impl<'a, T: Copy + Mul<T, Output = T>> Mul<T> for UseState<'a, T> {
type Output = T;
fn mul(self, rhs: T) -> Self::Output {
self.inner.current_val.mul(rhs)
}
}
impl<'a, T: Copy + Mul<T, Output = T>> MulAssign<T> for UseState<'a, T> {
fn mul_assign(&mut self, rhs: T) {
self.set(self.inner.current_val.mul(rhs));
}
}
/// DIV
impl<'a, T: Copy + Div<T, Output = T>> Div<T> for UseState<'a, T> {
type Output = T;
fn div(self, rhs: T) -> Self::Output {
self.inner.current_val.div(rhs)
}
}
impl<'a, T: Copy + Div<T, Output = T>> DivAssign<T> for UseState<'a, T> {
fn div_assign(&mut self, rhs: T) {
self.set(self.inner.current_val.div(rhs));
}
}
impl<'a, V, T: PartialEq<V>> PartialEq<V> for UseState<'a, T> {
fn eq(&self, other: &V) -> bool {
2021-07-21 21:05:48 +00:00
self.get() == other
}
}
impl<'a, O, T: Not<Output = O> + Copy> Not for UseState<'a, T> {
type Output = O;
fn not(self) -> Self::Output {
!*self.get()
}
}
2021-07-16 20:11:25 +00:00
2021-07-09 15:54:07 +00:00
// enable displaty for the handle
impl<'a, T: 'static + Display> std::fmt::Display for UseState<'a, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.inner.current_val)
}
}
2021-07-16 20:11:25 +00:00
/// A less ergonmic but still capable form of use_state that's valid for `static lifetime
pub struct AsyncUseState<T: 'static> {
2021-12-13 00:47:13 +00:00
inner: Rc<T>,
2021-12-10 02:19:31 +00:00
re_render: Rc<dyn Fn()>,
2021-07-16 20:11:25 +00:00
wip: Rc<RefCell<Option<T>>>,
}
impl<T: ToOwned> AsyncUseState<T> {
pub fn get_mut<'a>(&'a self) -> RefMut<'a, T> {
// make sure we get processed
// self.needs_update();
// Bring out the new value, cloning if it we need to
// "get_mut" is locked behind "ToOwned" to make it explicit that cloning occurs to use this
RefMut::map(self.wip.borrow_mut(), |slot| {
//
slot.as_mut().unwrap()
})
}
2021-12-10 02:19:31 +00:00
}
impl<T> AsyncUseState<T> {
2021-07-21 21:05:48 +00:00
pub fn set(&mut self, val: T) {
2021-12-10 02:19:31 +00:00
(self.re_render)();
2021-07-21 21:05:48 +00:00
*self.wip.borrow_mut() = Some(val);
}
2021-12-13 00:47:13 +00:00
pub fn get(&self) -> &T {
self.inner.as_ref()
}
pub fn get_rc(&self) -> &Rc<T> {
&self.inner
}
2021-07-16 20:11:25 +00:00
}