recoil.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use dioxus_core::context::Context;
  2. pub struct RecoilContext<T: 'static> {
  3. _inner: T,
  4. }
  5. impl<T: 'static> RecoilContext<T> {
  6. /// Get the value of an atom. Returns a reference to the underlying data.
  7. pub fn get(&self) {}
  8. /// Replace an existing value with a new value
  9. ///
  10. /// This does not replace the value instantly, and all calls to "get" within the current scope will return
  11. pub fn set(&self) {}
  12. // Modify lets you modify the value in place. However, because there's no previous value around to compare
  13. // the new one with, we are unable to memoize the change. As such, all downsteam users of this Atom will
  14. // be updated, causing all subsrcibed components to re-render.
  15. //
  16. // This is fine for most values, but might not be performant when dealing with collections. For collections,
  17. // use the "Family" variants as these will stay memoized for inserts, removals, and modifications.
  18. //
  19. // Note - like "set" this won't propogate instantly. Once all "gets" are dropped, only then will we run the
  20. pub fn modify(&self) {}
  21. }
  22. pub fn use_callback<'a, G>(c: &Context<'a>, f: impl Fn() -> G) -> &'a RecoilContext<G> {
  23. todo!()
  24. }
  25. pub fn use_atom<T: PartialEq, O>(c: &Context, t: &'static Atom<T>) -> O {
  26. todo!()
  27. }
  28. pub fn use_batom<T: PartialEq, O>(c: &Context, t: impl Readable) -> O {
  29. todo!()
  30. }
  31. pub trait Readable {}
  32. impl<T: PartialEq> Readable for &'static Atom<T> {}
  33. impl<K: PartialEq, V: PartialEq> Readable for &'static AtomFamily<K, V> {}
  34. pub fn use_atom_family<'a, K: PartialEq, V: PartialEq>(
  35. c: &Context<'a>,
  36. t: &'static AtomFamily<K, V>,
  37. g: K,
  38. ) -> &'a V {
  39. todo!()
  40. }
  41. pub use atoms::{atom, Atom};
  42. pub use atoms::{atom_family, AtomFamily};
  43. mod atoms {
  44. use super::*;
  45. pub struct AtomBuilder<T: PartialEq> {
  46. pub key: String,
  47. pub manual_init: Option<Box<dyn Fn() -> T>>,
  48. _never: std::marker::PhantomData<T>,
  49. }
  50. impl<T: PartialEq> AtomBuilder<T> {
  51. pub fn new() -> Self {
  52. Self {
  53. key: uuid::Uuid::new_v4().to_string(),
  54. manual_init: None,
  55. _never: std::marker::PhantomData {},
  56. }
  57. }
  58. pub fn init<A: Fn() -> T + 'static>(&mut self, f: A) {
  59. self.manual_init = Some(Box::new(f));
  60. }
  61. pub fn set_key(&mut self, _key: &'static str) {}
  62. }
  63. pub struct atom<T: PartialEq>(pub fn(&mut AtomBuilder<T>) -> T);
  64. pub type Atom<T: PartialEq> = atom<T>;
  65. pub struct AtomFamilyBuilder<K, V> {
  66. _never: std::marker::PhantomData<(K, V)>,
  67. }
  68. pub struct atom_family<K: PartialEq, V: PartialEq>(pub fn(&mut AtomFamilyBuilder<K, V>));
  69. pub type AtomFamily<K: PartialEq, V: PartialEq> = atom_family<K, V>;
  70. }
  71. pub use selectors::selector;
  72. mod selectors {
  73. pub struct SelectorBuilder<Out, const Built: bool> {
  74. _p: std::marker::PhantomData<Out>,
  75. }
  76. impl<O> SelectorBuilder<O, false> {
  77. pub fn getter(self, f: impl Fn(()) -> O) -> SelectorBuilder<O, true> {
  78. todo!()
  79. // std::rc::Rc::pin(value)
  80. // todo!()
  81. }
  82. }
  83. pub struct selector<O>(pub fn(SelectorBuilder<O, false>) -> SelectorBuilder<O, true>);
  84. }