From d23848193686b8ca9fb6b1820ccac0e18b9c0fde Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 2 Mar 2023 11:54:21 -0600 Subject: [PATCH] reduce indirection and remove some unnecessary methods --- packages/hooks/src/use_shared_state.rs | 50 +++++++++----------------- 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/packages/hooks/src/use_shared_state.rs b/packages/hooks/src/use_shared_state.rs index 173bfbb71..8e0e813c8 100644 --- a/packages/hooks/src/use_shared_state.rs +++ b/packages/hooks/src/use_shared_state.rs @@ -9,8 +9,8 @@ use std::{ type ProvidedState = Rc>>; // Tracks all the subscribers to a shared State -pub struct ProvidedStateInner { - value: Rc>, +pub(crate) struct ProvidedStateInner { + value: T, notify_any: Arc, consumers: HashSet, } @@ -21,14 +21,6 @@ impl ProvidedStateInner { (self.notify_any)(*consumer); } } - - pub fn write(&self) -> RefMut { - self.value.borrow_mut() - } - - pub fn read(&self) -> Ref { - self.value.borrow() - } } /// This hook provides some relatively light ergonomics around shared state. @@ -103,8 +95,7 @@ pub fn use_shared_state(cx: &ScopeState) -> Option<&UseSharedState { impl Drop for UseSharedStateOwner { fn drop(&mut self) { // we need to unsubscribe when our component is unmounted - let mut root = self.state.root.borrow_mut(); + let mut root = self.state.inner.borrow_mut(); root.consumers.remove(&self.scope_id); } } pub struct UseSharedState { - pub(crate) value: Rc>, - pub(crate) root: Rc>>, + pub(crate) inner: Rc>>, } impl UseSharedState { - pub fn read(&self) -> Ref<'_, T> { - self.value.borrow() - } - pub fn notify_consumers(&self) { - self.root.borrow_mut().notify_consumers(); + self.inner.borrow_mut().notify_consumers(); } - pub fn read_write(&self) -> (Ref<'_, T>, &Self) { - (self.read(), self) + pub fn read(&self) -> Ref<'_, T> { + Ref::map(self.inner.borrow(), |inner| &inner.value) } /// Calling "write" will force the component to re-render @@ -147,32 +133,30 @@ impl UseSharedState { /// /// TODO: We prevent unncessary notifications only in the hook, but we should figure out some more global lock pub fn write(&self) -> RefMut<'_, T> { - self.notify_consumers(); - self.value.borrow_mut() + let mut value = self.inner.borrow_mut(); + value.notify_consumers(); + RefMut::map(value, |inner| &mut inner.value) } /// Allows the ability to write the value without forcing a re-render pub fn write_silent(&self) -> RefMut<'_, T> { - self.value.borrow_mut() - } - - pub fn inner(&self) -> Rc>> { - self.root.clone() + RefMut::map(self.inner.borrow_mut(), |inner| &mut inner.value) } } impl Clone for UseSharedState { fn clone(&self) -> Self { Self { - value: self.value.clone(), - root: self.root.clone(), + inner: self.inner.clone(), } } } impl PartialEq for UseSharedState { fn eq(&self, other: &Self) -> bool { - self.value == other.value + let first = self.inner.borrow(); + let second = other.inner.borrow(); + first.value == second.value } } @@ -219,7 +203,7 @@ impl PartialEq for UseSharedState { pub fn use_shared_state_provider(cx: &ScopeState, f: impl FnOnce() -> T) { cx.use_hook(|| { let state: ProvidedState = Rc::new(RefCell::new(ProvidedStateInner { - value: Rc::new(RefCell::new(f())), + value: f(), notify_any: cx.schedule_update_any(), consumers: HashSet::new(), }));