state.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. use crate::{AtomId, AtomRoot, Writable};
  2. use dioxus_core::{ScopeId, ScopeState};
  3. use std::{
  4. cell::RefMut,
  5. fmt::{Debug, Display},
  6. ops::{Add, Div, Mul, Not, Sub},
  7. rc::Rc,
  8. };
  9. /// Store state between component renders.
  10. ///
  11. /// ## Dioxus equivalent of AtomState, designed for Rust
  12. ///
  13. /// The Dioxus version of `AtomState` for state management inside components. It allows you to ergonomically store and
  14. /// modify state between component renders. When the state is updated, the component will re-render.
  15. ///
  16. ///
  17. /// ```ignore
  18. /// static COUNT: Atom<u32> = |_| 0;
  19. ///
  20. /// fn Example(cx: Scope) -> Element {
  21. /// let mut count = use_atom_state(cx, &COUNT);
  22. ///
  23. /// cx.render(rsx! {
  24. /// div {
  25. /// h1 { "Count: {count}" }
  26. /// button { onclick: move |_| count += 1, "Increment" }
  27. /// button { onclick: move |_| count -= 1, "Decrement" }
  28. /// }
  29. /// ))
  30. /// }
  31. /// ```
  32. #[must_use]
  33. pub fn use_atom_state<T: 'static>(cx: &ScopeState, f: impl Writable<T>) -> &AtomState<T> {
  34. let root = crate::use_atom_root(cx);
  35. let inner = cx.use_hook(|| AtomState {
  36. value: None,
  37. root: root.clone(),
  38. scope_id: cx.scope_id(),
  39. id: f.unique_id(),
  40. });
  41. inner.value = Some(inner.root.register(f, cx.scope_id()));
  42. inner
  43. }
  44. pub struct AtomState<V: 'static> {
  45. root: Rc<AtomRoot>,
  46. id: AtomId,
  47. scope_id: ScopeId,
  48. value: Option<Rc<V>>,
  49. }
  50. impl<V> Drop for AtomState<V> {
  51. fn drop(&mut self) {
  52. self.root.unsubscribe(self.id, self.scope_id)
  53. }
  54. }
  55. impl<T: 'static> AtomState<T> {
  56. /// Set the state to a new value.
  57. pub fn set(&self, new: T) {
  58. self.root.set(self.id, new)
  59. }
  60. /// Get the current value of the state by cloning its container Rc.
  61. ///
  62. /// This is useful when you are dealing with state in async contexts but need
  63. /// to know the current value. You are not given a reference to the state.
  64. ///
  65. /// # Examples
  66. /// An async context might need to know the current value:
  67. ///
  68. /// ```rust, ignore
  69. /// fn component(cx: Scope) -> Element {
  70. /// let count = use_state(cx, || 0);
  71. /// cx.spawn({
  72. /// let set_count = count.to_owned();
  73. /// async move {
  74. /// let current = set_count.current();
  75. /// }
  76. /// })
  77. /// }
  78. /// ```
  79. #[must_use]
  80. pub fn current(&self) -> Rc<T> {
  81. let atoms = self.root.atoms.borrow();
  82. let slot = atoms.get(&self.id).unwrap();
  83. slot.value.clone().downcast().unwrap()
  84. }
  85. /// Get the `setter` function directly without the `AtomState` wrapper.
  86. ///
  87. /// This is useful for passing the setter function to other components.
  88. ///
  89. /// However, for most cases, calling `to_owned` on the state is the
  90. /// preferred way to get "another" state handle.
  91. ///
  92. ///
  93. /// # Examples
  94. /// A component might require an `Rc<dyn Fn(T)>` as an input to set a value.
  95. ///
  96. /// ```rust, ignore
  97. /// fn component(cx: Scope) -> Element {
  98. /// let value = use_state(cx, || 0);
  99. ///
  100. /// rsx!{
  101. /// Component {
  102. /// handler: value.setter()
  103. /// }
  104. /// }
  105. /// }
  106. /// ```
  107. #[must_use]
  108. pub fn setter(&self) -> Rc<dyn Fn(T)> {
  109. let root = self.root.clone();
  110. let id = self.id;
  111. Rc::new(move |new_val| root.set(id, new_val))
  112. }
  113. /// Set the state to a new value, using the current state value as a reference.
  114. ///
  115. /// This is similar to passing a closure to React's `set_value` function.
  116. ///
  117. /// # Examples
  118. ///
  119. /// Basic usage:
  120. /// ```rust, ignore
  121. /// # use dioxus_core::prelude::*;
  122. /// # use dioxus_hooks::*;
  123. /// fn component(cx: Scope) -> Element {
  124. /// let value = use_state(cx, || 0);
  125. ///
  126. /// // to increment the value
  127. /// value.modify(|v| v + 1);
  128. ///
  129. /// // usage in async
  130. /// cx.spawn({
  131. /// let value = value.to_owned();
  132. /// async move {
  133. /// value.modify(|v| v + 1);
  134. /// }
  135. /// });
  136. ///
  137. /// # todo!()
  138. /// }
  139. /// ```
  140. pub fn modify(&self, f: impl FnOnce(&T) -> T) {
  141. self.root.clone().set(self.id, {
  142. let current = self.value.as_ref().unwrap();
  143. f(current.as_ref())
  144. });
  145. }
  146. /// Get the value of the state when this handle was created.
  147. ///
  148. /// This method is useful when you want an `Rc` around the data to cheaply
  149. /// pass it around your app.
  150. ///
  151. /// ## Warning
  152. ///
  153. /// This will return a stale value if used within async contexts.
  154. ///
  155. /// Try `current` to get the real current value of the state.
  156. ///
  157. /// ## Example
  158. ///
  159. /// ```rust, ignore
  160. /// # use dioxus_core::prelude::*;
  161. /// # use dioxus_hooks::*;
  162. /// fn component(cx: Scope) -> Element {
  163. /// let value = use_state(cx, || 0);
  164. ///
  165. /// let as_rc = value.get();
  166. /// assert_eq!(as_rc.as_ref(), &0);
  167. ///
  168. /// # todo!()
  169. /// }
  170. /// ```
  171. #[must_use]
  172. pub fn get(&self) -> &T {
  173. self.value.as_ref().unwrap()
  174. }
  175. #[must_use]
  176. pub fn get_rc(&self) -> &Rc<T> {
  177. self.value.as_ref().unwrap()
  178. }
  179. /// Mark all consumers of this atom to re-render
  180. ///
  181. /// ```rust, ignore
  182. /// fn component(cx: Scope) -> Element {
  183. /// let count = use_state(cx, || 0);
  184. /// cx.spawn({
  185. /// let count = count.to_owned();
  186. /// async move {
  187. /// // for the component to re-render
  188. /// count.needs_update();
  189. /// }
  190. /// })
  191. /// }
  192. /// ```
  193. pub fn needs_update(&self) {
  194. self.root.force_update(self.id)
  195. }
  196. }
  197. impl<T: Clone> AtomState<T> {
  198. /// Get a mutable handle to the value by calling `ToOwned::to_owned` on the
  199. /// current value.
  200. ///
  201. /// This is essentially cloning the underlying value and then setting it,
  202. /// giving you a mutable handle in the process. This method is intended for
  203. /// types that are cheaply cloneable.
  204. ///
  205. /// If you are comfortable dealing with `RefMut`, then you can use `make_mut` to get
  206. /// the underlying slot. However, be careful with `RefMut` since you might panic
  207. /// if the `RefCell` is left open.
  208. ///
  209. /// # Examples
  210. ///
  211. /// ```ignore
  212. /// let val = use_state(cx, || 0);
  213. ///
  214. /// val.with_mut(|v| *v = 1);
  215. /// ```
  216. pub fn with_mut(&self, apply: impl FnOnce(&mut T)) {
  217. let mut new_val = self.value.as_ref().unwrap().as_ref().to_owned();
  218. apply(&mut new_val);
  219. self.set(new_val);
  220. }
  221. /// Get a mutable handle to the value by calling `ToOwned::to_owned` on the
  222. /// current value.
  223. ///
  224. /// This is essentially cloning the underlying value and then setting it,
  225. /// giving you a mutable handle in the process. This method is intended for
  226. /// types that are cheaply cloneable.
  227. ///
  228. /// # Warning
  229. /// Be careful with `RefMut` since you might panic if the `RefCell` is left open!
  230. ///
  231. /// # Examples
  232. ///
  233. /// ```ignore
  234. /// let val = use_state(cx, || 0);
  235. ///
  236. /// *val.make_mut() += 1;
  237. /// ```
  238. #[must_use]
  239. pub fn make_mut(&self) -> RefMut<T> {
  240. todo!("make mut not support for atom values yet")
  241. // let mut slot = self.value.as_ref().unwrap();
  242. // self.needs_update();
  243. // if Rc::strong_count(&*slot) > 0 {
  244. // *slot = Rc::new(slot.as_ref().to_owned());
  245. // }
  246. // RefMut::map(slot, |rc| Rc::get_mut(rc).expect("the hard count to be 0"))
  247. }
  248. /// Convert this handle to a tuple of the value and the handle itself.
  249. #[must_use]
  250. pub fn split(&self) -> (&T, &Self) {
  251. (self.value.as_ref().unwrap(), self)
  252. }
  253. }
  254. impl<T: 'static> Clone for AtomState<T> {
  255. fn clone(&self) -> Self {
  256. AtomState {
  257. root: self.root.clone(),
  258. id: self.id,
  259. scope_id: self.scope_id,
  260. value: self.value.clone(),
  261. }
  262. }
  263. }
  264. impl<T: 'static + Display> std::fmt::Display for AtomState<T> {
  265. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  266. write!(f, "{}", self.value.as_ref().unwrap())
  267. }
  268. }
  269. impl<T: std::fmt::Binary> std::fmt::Binary for AtomState<T> {
  270. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  271. write!(f, "{:b}", self.value.as_ref().unwrap().as_ref())
  272. }
  273. }
  274. impl<T: PartialEq> PartialEq<T> for AtomState<T> {
  275. fn eq(&self, other: &T) -> bool {
  276. self.value.as_ref().unwrap().as_ref() == other
  277. }
  278. }
  279. // todo: this but for more interesting conrete types
  280. impl PartialEq<bool> for &AtomState<bool> {
  281. fn eq(&self, other: &bool) -> bool {
  282. self.value.as_ref().unwrap().as_ref() == other
  283. }
  284. }
  285. impl<T: PartialEq> PartialEq<AtomState<T>> for AtomState<T> {
  286. fn eq(&self, other: &AtomState<T>) -> bool {
  287. Rc::ptr_eq(self.value.as_ref().unwrap(), other.value.as_ref().unwrap())
  288. }
  289. }
  290. impl<T: Debug> Debug for AtomState<T> {
  291. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  292. write!(f, "{:?}", self.value.as_ref().unwrap())
  293. }
  294. }
  295. impl<T> std::ops::Deref for AtomState<T> {
  296. type Target = T;
  297. fn deref(&self) -> &Self::Target {
  298. self.value.as_ref().unwrap().as_ref()
  299. }
  300. }
  301. impl<T: Not + Copy> std::ops::Not for &AtomState<T> {
  302. type Output = <T as std::ops::Not>::Output;
  303. fn not(self) -> Self::Output {
  304. self.value.as_ref().unwrap().not()
  305. }
  306. }
  307. impl<T: Not + Copy> std::ops::Not for AtomState<T> {
  308. type Output = <T as std::ops::Not>::Output;
  309. fn not(self) -> Self::Output {
  310. self.value.as_ref().unwrap().not()
  311. }
  312. }
  313. impl<T: std::ops::Add + Copy> std::ops::Add<T> for &AtomState<T> {
  314. type Output = <T as std::ops::Add>::Output;
  315. fn add(self, other: T) -> Self::Output {
  316. *self.value.as_ref().unwrap().as_ref() + other
  317. }
  318. }
  319. impl<T: std::ops::Sub + Copy> std::ops::Sub<T> for &AtomState<T> {
  320. type Output = <T as std::ops::Sub>::Output;
  321. fn sub(self, other: T) -> Self::Output {
  322. *self.value.as_ref().unwrap().as_ref() - other
  323. }
  324. }
  325. impl<T: std::ops::Div + Copy> std::ops::Div<T> for &AtomState<T> {
  326. type Output = <T as std::ops::Div>::Output;
  327. fn div(self, other: T) -> Self::Output {
  328. *self.value.as_ref().unwrap().as_ref() / other
  329. }
  330. }
  331. impl<T: std::ops::Mul + Copy> std::ops::Mul<T> for &AtomState<T> {
  332. type Output = <T as std::ops::Mul>::Output;
  333. fn mul(self, other: T) -> Self::Output {
  334. *self.value.as_ref().unwrap().as_ref() * other
  335. }
  336. }
  337. impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for &AtomState<T> {
  338. fn add_assign(&mut self, rhs: T) {
  339. self.set((*self.current()) + rhs);
  340. }
  341. }
  342. impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for &AtomState<T> {
  343. fn sub_assign(&mut self, rhs: T) {
  344. self.set((*self.current()) - rhs);
  345. }
  346. }
  347. impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for &AtomState<T> {
  348. fn mul_assign(&mut self, rhs: T) {
  349. self.set((*self.current()) * rhs);
  350. }
  351. }
  352. impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for &AtomState<T> {
  353. fn div_assign(&mut self, rhs: T) {
  354. self.set((*self.current()) / rhs);
  355. }
  356. }
  357. impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for AtomState<T> {
  358. fn add_assign(&mut self, rhs: T) {
  359. self.set((*self.current()) + rhs);
  360. }
  361. }
  362. impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for AtomState<T> {
  363. fn sub_assign(&mut self, rhs: T) {
  364. self.set((*self.current()) - rhs);
  365. }
  366. }
  367. impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for AtomState<T> {
  368. fn mul_assign(&mut self, rhs: T) {
  369. self.set((*self.current()) * rhs);
  370. }
  371. }
  372. impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for AtomState<T> {
  373. fn div_assign(&mut self, rhs: T) {
  374. self.set((*self.current()) / rhs);
  375. }
  376. }