From 4bbd9137ed484cb3d8a898ef6f090970d7b4830c Mon Sep 17 00:00:00 2001 From: Noah Date: Tue, 12 Dec 2023 00:45:03 -0800 Subject: [PATCH] Rename with_cx -> new --- crates/yewdux-input/src/lib.rs | 4 +- crates/yewdux-utils/src/lib.rs | 4 +- crates/yewdux/src/dispatch.rs | 72 ++++++++++++++++----------------- crates/yewdux/src/functional.rs | 42 ++++++++----------- crates/yewdux/src/listener.rs | 8 ++-- crates/yewdux/src/mrc.rs | 6 +-- crates/yewdux/src/storage.rs | 2 +- crates/yewdux/src/subscriber.rs | 19 +++++---- 8 files changed, 74 insertions(+), 83 deletions(-) diff --git a/crates/yewdux-input/src/lib.rs b/crates/yewdux-input/src/lib.rs index 8699ad5..5641619 100644 --- a/crates/yewdux-input/src/lib.rs +++ b/crates/yewdux-input/src/lib.rs @@ -56,7 +56,7 @@ pub trait InputDispatch { E: AsRef + JsCast + 'static, { let cx = self.context(); - Dispatch::::with_cx(cx).reduce_callback_with(move |s, e| { + Dispatch::::new(cx).reduce_callback_with(move |s, e| { if let Some(value) = input_value(e) { f(s, value) } else { @@ -73,7 +73,7 @@ pub trait InputDispatch { E: AsRef + JsCast + 'static, { let cx = self.context(); - Dispatch::::with_cx(cx).reduce_mut_callback_with(move |s, e| { + Dispatch::::new(cx).reduce_mut_callback_with(move |s, e| { if let Some(value) = input_value(e) { f(s, value); } diff --git a/crates/yewdux-utils/src/lib.rs b/crates/yewdux-utils/src/lib.rs index 21a3d1a..8487298 100644 --- a/crates/yewdux-utils/src/lib.rs +++ b/crates/yewdux-utils/src/lib.rs @@ -25,7 +25,7 @@ impl Listener for HistoryListener { type Store = T; fn on_change(&mut self, cx: &Context, state: Rc) { - Dispatch::>::with_cx(cx).apply(HistoryChangeMessage::(state)) + Dispatch::>::new(cx).apply(HistoryChangeMessage::(state)) } } @@ -76,7 +76,7 @@ impl HistoryStore { impl Store for HistoryStore { fn new(cx: &Context) -> Self { - let dispatch = Dispatch::::with_cx(cx); + let dispatch = Dispatch::::new(cx); let s1 = dispatch.get(); Self { vector: vec![s1], diff --git a/crates/yewdux/src/dispatch.rs b/crates/yewdux/src/dispatch.rs index ec94479..0d8b33c 100644 --- a/crates/yewdux/src/dispatch.rs +++ b/crates/yewdux/src/dispatch.rs @@ -56,11 +56,11 @@ impl Dispatch { /// This is only available for wasm32 targets. For SSR, see the YewduxRoot pattern. #[cfg(target_arch = "wasm32")] pub fn global() -> Self { - Self::with_cx(&Context::global()) + Self::new(&Context::global()) } /// Create a new dispatch with access to the given context. - pub fn with_cx(cx: &Context) -> Self { + pub fn new(cx: &Context) -> Self { Self { _subscriber_id: Default::default(), cx: cx.clone(), @@ -860,12 +860,12 @@ mod tests { #[test] fn apply_no_clone() { - Dispatch::with_cx(&Context::new()).reduce(|_| TestStateNoClone(1).into()); + Dispatch::new(&Context::new()).reduce(|_| TestStateNoClone(1).into()); } #[test] fn reduce_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); @@ -880,7 +880,7 @@ mod tests { #[async_std::test] async fn reduce_future_changes_value() { let cx = Context::new(); - let dispatch = Dispatch::::with_cx(&cx); + let dispatch = Dispatch::::new(&cx); let old = dispatch.get(); dispatch @@ -898,7 +898,7 @@ mod tests { use std::time::Duration; let cx = Context::new(); - let dispatch = Dispatch::::with_cx(&cx); + let dispatch = Dispatch::::new(&cx); dispatch .reduce_future(|state| async move { @@ -912,7 +912,7 @@ mod tests { #[test] fn reduce_mut_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.reduce_mut(|state| *state = TestState(1)); @@ -925,7 +925,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn reduce_mut_future_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch @@ -940,19 +940,19 @@ mod tests { #[test] fn reduce_does_not_require_static() { let val = "1".to_string(); - Dispatch::with_cx(&Context::new()).reduce(|_| TestState(val.parse().unwrap()).into()); + Dispatch::new(&Context::new()).reduce(|_| TestState(val.parse().unwrap()).into()); } #[test] fn reduce_mut_does_not_require_static() { let val = "1".to_string(); - Dispatch::with_cx(&Context::new()) + Dispatch::new(&Context::new()) .reduce_mut(|state: &mut TestState| state.0 = val.parse().unwrap()); } #[test] fn set_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); @@ -965,7 +965,7 @@ mod tests { #[test] fn apply_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.apply(Msg); @@ -977,7 +977,7 @@ mod tests { #[test] fn dispatch_set_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.set(TestState(1)); @@ -987,7 +987,7 @@ mod tests { #[test] fn dispatch_set_callback_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.set_callback(|_| TestState(1)); @@ -998,7 +998,7 @@ mod tests { #[test] fn dispatch_reduce_mut_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.reduce_mut(|state| state.0 += 1); @@ -1009,7 +1009,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_mut_future_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch @@ -1021,7 +1021,7 @@ mod tests { #[test] fn dispatch_reduce_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.reduce(|_| TestState(1).into()); @@ -1032,7 +1032,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_future_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch @@ -1044,7 +1044,7 @@ mod tests { #[test] fn dispatch_reduce_callback_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.reduce_callback(|_| TestState(1).into()); @@ -1056,7 +1056,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_future_callback_compiles() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let _ = dispatch.reduce_future_callback::<_, _, ()>(|state| async move { TestState(state.0 + 1).into() @@ -1065,7 +1065,7 @@ mod tests { #[test] fn dispatch_reduce_mut_callback_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.reduce_mut_callback(|state| state.0 += 1); @@ -1077,7 +1077,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_mut_future_callback_compiles() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let _ = dispatch.reduce_mut_future_callback::<_, _, ()>(|state| { Box::pin(async move { @@ -1089,7 +1089,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_future_callback_with_compiles() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let _ = dispatch.reduce_future_callback_with(|state, e: u32| async move { TestState(state.0 + e).into() @@ -1098,7 +1098,7 @@ mod tests { #[test] fn dispatch_reduce_callback_with_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.reduce_callback_with(|_, _| TestState(1).into()); @@ -1109,7 +1109,7 @@ mod tests { #[test] fn dispatch_reduce_mut_callback_with_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.reduce_mut_callback_with(|state, val| state.0 += val); @@ -1121,7 +1121,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn dispatch_reduce_mut_future_callback_with_compiles() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let _ = dispatch.reduce_mut_future_callback_with::<_, _, u32>(|state, e| { Box::pin(async move { @@ -1132,7 +1132,7 @@ mod tests { #[test] fn dispatch_apply_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.apply(Msg); @@ -1143,7 +1143,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn apply_future_changes_value() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); dispatch.apply_future(Msg).await; @@ -1155,7 +1155,7 @@ mod tests { #[test] fn dispatch_apply_callback_works() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); let old = dispatch.get(); let cb = dispatch.apply_callback(|_| Msg); @@ -1167,7 +1167,7 @@ mod tests { #[cfg(feature = "future")] #[async_std::test] async fn apply_future_callback_compiles() { - let dispatch = Dispatch::::with_cx(&Context::new()); + let dispatch = Dispatch::::new(&Context::new()); dispatch.apply_future_callback(|_: ()| Msg); } @@ -1179,13 +1179,13 @@ mod tests { let _id = { let flag = flag.clone(); - Dispatch::::with_cx(&cx) + Dispatch::::new(&cx) .subscribe(move |_| flag.clone().with_mut(|flag| *flag = true)) }; *flag.borrow_mut() = false; - Dispatch::::with_cx(&cx).reduce_mut(|state| state.0 += 1); + Dispatch::::new(&cx).reduce_mut(|state| state.0 += 1); assert!(*flag.borrow()); } @@ -1194,14 +1194,14 @@ mod tests { fn subscriber_is_not_notified_when_state_is_same() { let cx = Context::new(); let flag = Mrc::new(false); - let dispatch = Dispatch::::with_cx(&cx); + let dispatch = Dispatch::::new(&cx); // TestState(1) dispatch.reduce_mut(|_| {}); let _id = { let flag = flag.clone(); - Dispatch::::with_cx(&cx) + Dispatch::::new(&cx) .subscribe(move |_| flag.clone().with_mut(|flag| *flag = true)) }; @@ -1220,7 +1220,7 @@ mod tests { assert!(entry.store.borrow().borrow().0.is_empty()); - let dispatch = Dispatch::::with_cx(&cx).subscribe(|_| ()); + let dispatch = Dispatch::::new(&cx).subscribe(|_| ()); assert!(!entry.store.borrow().borrow().0.is_empty()); @@ -1236,7 +1236,7 @@ mod tests { assert!(entry.store.borrow().borrow().0.is_empty()); - let dispatch = Dispatch::::with_cx(&cx).subscribe(|_| ()); + let dispatch = Dispatch::::new(&cx).subscribe(|_| ()); let dispatch_clone = dispatch.clone(); assert!(!entry.store.borrow().borrow().0.is_empty()); diff --git a/crates/yewdux/src/functional.rs b/crates/yewdux/src/functional.rs index 92e17f4..9a8d3fc 100644 --- a/crates/yewdux/src/functional.rs +++ b/crates/yewdux/src/functional.rs @@ -12,7 +12,7 @@ fn use_cx() -> Context { use_context::().unwrap_or_else(crate::context::Context::global) } #[cfg(not(target_arch = "wasm32"))] - { + { use_context::().expect("YewduxRoot not found") } } @@ -45,7 +45,7 @@ fn use_cx() -> Context { #[hook] pub fn use_store() -> (Rc, Dispatch) { let cx = use_cx(); - let dispatch = Dispatch::::with_cx(&cx); + let dispatch = Dispatch::::new(&cx); let state: UseStateHandle> = use_state(|| dispatch.get()); let dispatch = { let state = state.clone(); @@ -55,8 +55,6 @@ pub fn use_store() -> (Rc, Dispatch) { (Rc::clone(&state), dispatch.deref().clone()) } - - /// Simliar to ['use_store'], but only provides the state. #[hook] pub fn use_store_value() -> Rc { @@ -173,7 +171,7 @@ where let cx = use_cx(); // Given to user, this is what we update to force a re-render. let selected = { - let state = Dispatch::with_cx(&cx).get(); + let state = Dispatch::new(&cx).get(); let value = selector(&state, &deps); use_state(|| Rc::new(value)) @@ -186,26 +184,20 @@ where let _dispatch = { let selected = selected.clone(); - use_memo( - deps, - move |deps| { - let deps = deps.clone(); - Dispatch::with_cx(&cx).subscribe( - move |val: Rc| { - let value = selector(&val, &deps); - - if !eq(¤t.borrow(), &value) { - let value = Rc::new(value); - // Update value for user. - selected.set(Rc::clone(&value)); - // Make sure to update our tracking value too. - *current.borrow_mut() = Rc::clone(&value); - } - }, - ) - }, - - ) + use_memo(deps, move |deps| { + let deps = deps.clone(); + Dispatch::new(&cx).subscribe(move |val: Rc| { + let value = selector(&val, &deps); + + if !eq(¤t.borrow(), &value) { + let value = Rc::new(value); + // Update value for user. + selected.set(Rc::clone(&value)); + // Make sure to update our tracking value too. + *current.borrow_mut() = Rc::clone(&value); + } + }) + }) }; Rc::clone(&selected) diff --git a/crates/yewdux/src/listener.rs b/crates/yewdux/src/listener.rs index bb60626..87dd8a3 100644 --- a/crates/yewdux/src/listener.rs +++ b/crates/yewdux/src/listener.rs @@ -26,7 +26,7 @@ pub fn init_listener(listener: L, cx: &Context) { let dispatch = { let listener = Mrc::new(listener); let cxo = cx.clone(); - Dispatch::with_cx(cx) + Dispatch::new(cx) .subscribe_silent(move |state| listener.borrow_mut().on_change(&cxo, state)) }; @@ -100,7 +100,7 @@ mod tests { init_listener(listener.clone(), &cx); - Dispatch::with_cx(&cx).reduce_mut(|state: &mut TestState| state.0 = 1); + Dispatch::new(&cx).reduce_mut(|state: &mut TestState| state.0 = 1); assert_eq!(listener.0.get(), 1) } @@ -113,13 +113,13 @@ mod tests { init_listener(listener1.clone(), &cx); - Dispatch::with_cx(&cx).reduce_mut(|state: &mut TestState| state.0 = 1); + Dispatch::new(&cx).reduce_mut(|state: &mut TestState| state.0 = 1); assert_eq!(listener1.0.get(), 1); init_listener(listener2.clone(), &cx); - Dispatch::with_cx(&cx).reduce_mut(|state: &mut TestState| state.0 = 2); + Dispatch::new(&cx).reduce_mut(|state: &mut TestState| state.0 = 2); assert_eq!(listener1.0.get(), 1); assert_eq!(listener2.0.get(), 2); diff --git a/crates/yewdux/src/mrc.rs b/crates/yewdux/src/mrc.rs index de9a5c2..2c2e3d0 100644 --- a/crates/yewdux/src/mrc.rs +++ b/crates/yewdux/src/mrc.rs @@ -156,7 +156,7 @@ mod tests { let dispatch = { let flag = flag.clone(); - Dispatch::::with_cx(&cx) + Dispatch::::new(&cx) .subscribe(move |_| flag.clone().with_mut(|flag| *flag = true)) }; @@ -176,7 +176,7 @@ mod tests { let dispatch = { let flag = flag.clone(); - Dispatch::::with_cx(&cx) + Dispatch::::new(&cx) .subscribe(move |_| flag.clone().with_mut(|flag| *flag = true)) }; @@ -190,7 +190,7 @@ mod tests { #[test] fn can_wrap_store_with_mrc() { let cx = Context::new(); - let dispatch = Dispatch::>::with_cx(&cx); + let dispatch = Dispatch::>::new(&cx); assert!(*dispatch.get().borrow().0.borrow() == 0) } } diff --git a/crates/yewdux/src/storage.rs b/crates/yewdux/src/storage.rs index c797b75..ac18a44 100644 --- a/crates/yewdux/src/storage.rs +++ b/crates/yewdux/src/storage.rs @@ -144,7 +144,7 @@ pub fn init_tab_sync( let cx = cx.clone(); let closure = Closure::wrap(Box::new(move |_: &Event| match load(area) { Ok(Some(state)) => { - Dispatch::::with_cx(&cx).set(state); + Dispatch::::new(&cx).set(state); } Err(e) => { crate::log::error!("Unable to load state: {:?}", e); diff --git a/crates/yewdux/src/subscriber.rs b/crates/yewdux/src/subscriber.rs index 1202837..c544078 100644 --- a/crates/yewdux/src/subscriber.rs +++ b/crates/yewdux/src/subscriber.rs @@ -130,7 +130,7 @@ mod tests { assert!(entry.store.borrow().borrow().0.is_empty()); - let _id = Dispatch::with_cx(&cx).subscribe(|_: Rc| ()); + let _id = Dispatch::new(&cx).subscribe(|_: Rc| ()); assert!(!entry.store.borrow().borrow().0.is_empty()); } @@ -142,7 +142,7 @@ mod tests { assert!(entry.store.borrow().borrow().0.is_empty()); - let id = Dispatch::with_cx(&cx).subscribe(|_: Rc| ()); + let id = Dispatch::new(&cx).subscribe(|_: Rc| ()); assert!(!entry.store.borrow().borrow().0.is_empty()); @@ -158,7 +158,7 @@ mod tests { assert!(entry.store.borrow().borrow().0.is_empty()); - let id = Dispatch::::with_cx(&cx).subscribe(|_| {}); + let id = Dispatch::::new(&cx).subscribe(|_| {}); assert!(!entry.store.borrow().borrow().0.is_empty()); @@ -174,7 +174,7 @@ mod tests { let _id = { let flag = flag.clone(); - Dispatch::::with_cx(&cx) + Dispatch::::new(&cx) .subscribe(move |_| flag.clone().with_mut(|flag| *flag = true)) }; @@ -204,12 +204,11 @@ mod tests { fn can_modify_state_inside_on_changed() { let cx = Context::new(); let cxo = cx.clone(); - let dispatch = - Dispatch::::with_cx(&cx).subscribe(move |state: Rc| { - if state.0 == 0 { - Dispatch::with_cx(&cxo).reduce_mut(|state: &mut TestState| state.0 += 1); - } - }); + let dispatch = Dispatch::::new(&cx).subscribe(move |state: Rc| { + if state.0 == 0 { + Dispatch::new(&cxo).reduce_mut(|state: &mut TestState| state.0 += 1); + } + }); assert_eq!(dispatch.get().0, 1) }