useref.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. use std::{
  2. cell::{Cell, Ref, RefCell, RefMut},
  3. rc::Rc,
  4. };
  5. use dioxus_core::Context;
  6. pub fn use_ref<T: 'static>(cx: Context, f: impl FnOnce() -> T) -> UseRef<T> {
  7. cx.use_hook(
  8. |_| UseRefInner {
  9. update_scheuled: Cell::new(false),
  10. update_callback: cx.schedule_update(),
  11. value: RefCell::new(f()),
  12. },
  13. |inner| {
  14. inner.update_scheuled.set(false);
  15. UseRef { inner }
  16. },
  17. )
  18. }
  19. pub struct UseRef<'a, T> {
  20. inner: &'a UseRefInner<T>,
  21. }
  22. struct UseRefInner<T> {
  23. update_scheuled: Cell<bool>,
  24. update_callback: Rc<dyn Fn()>,
  25. value: RefCell<T>,
  26. }
  27. impl<'a, T> UseRef<'a, T> {
  28. pub fn read(&self) -> Ref<'_, T> {
  29. self.inner.value.borrow()
  30. }
  31. pub fn read_write(&self) -> (Ref<'_, T>, &Self) {
  32. (self.read(), self)
  33. }
  34. /// Calling "write" will force the component to re-render
  35. pub fn write(&self) -> RefMut<'_, T> {
  36. self.needs_update();
  37. self.inner.value.borrow_mut()
  38. }
  39. /// Allows the ability to write the value without forcing a re-render
  40. pub fn write_silent(&self) -> RefMut<'_, T> {
  41. self.inner.value.borrow_mut()
  42. }
  43. pub fn needs_update(&self) {
  44. if !self.inner.update_scheuled.get() {
  45. self.inner.update_scheuled.set(true);
  46. (self.inner.update_callback)();
  47. }
  48. }
  49. }
  50. impl<T> Clone for UseRef<'_, T> {
  51. fn clone(&self) -> Self {
  52. Self { inner: self.inner }
  53. }
  54. }
  55. impl<T> Copy for UseRef<'_, T> {}