callback.rs 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. use std::rc::Rc;
  2. use crate::Atom;
  3. pub struct RecoilApi {}
  4. impl RecoilApi {
  5. /// Get the value of an atom. Returns a reference to the underlying data.
  6. pub fn get<T: PartialEq>(&self, t: &'static Atom<T>) -> Rc<T> {
  7. todo!()
  8. }
  9. /// Replace an existing value with a new value
  10. ///
  11. /// This does not replace the value instantly.
  12. /// All calls to "get" will return the old value until the component is rendered.
  13. pub fn set<T: PartialEq>(&self, t: &'static Atom<T>, new: T) {
  14. self.modify(t, move |old| *old = new);
  15. }
  16. /// Modify lets you modify the value in place. However, because there's no previous value around to compare
  17. /// the new one with, we are unable to memoize the change. As such, all downsteam users of this Atom will
  18. /// be updated, causing all subsrcibed components to re-render.
  19. ///
  20. /// This is fine for most values, but might not be performant when dealing with collections. For collections,
  21. /// use the "Family" variants as these will stay memoized for inserts, removals, and modifications.
  22. ///
  23. /// Note - like "set" this won't propogate instantly. Once all "gets" are dropped, only then will the update occur
  24. pub fn modify<T: PartialEq, O>(&self, t: &'static Atom<T>, f: impl FnOnce(&mut T) -> O) -> O {
  25. todo!()
  26. }
  27. }