Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useful methods to UseStateHandle & UseReducerHandle #3422

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/yew/src/functional/hooks/use_reducer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl<T> UseReducerHandle<T>
where
T: Reducible,
{
/// Returns the inner value of the handle in an `Rc`.
pub fn get(&self) -> Rc<T> {
self.value.clone()
}

/// Dispatch the given action to the reducer.
pub fn dispatch(&self, value: T::Action) {
(self.dispatch)(value)
Expand All @@ -50,6 +55,18 @@ where
dispatch: self.dispatch.clone(),
}
}

/// Destrctures the handle into its 2 parts:
/// 0: the current data associated with the reducer;
/// 1: the dispatcher responsible for applying an action to the value.
pub fn into_inner(self) -> (Rc<T>, UseReducerDispatcher<T>) {
(
self.value,
UseReducerDispatcher {
dispatch: self.dispatch,
},
)
}
}

impl<T> Deref for UseReducerHandle<T>
Expand Down
17 changes: 17 additions & 0 deletions packages/yew/src/functional/hooks/use_state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::fmt;
use std::mem::transmute;
use std::ops::Deref;
use std::rc::Rc;

use super::{use_reducer, use_reducer_eq, Reducible, UseReducerDispatcher, UseReducerHandle};
use crate::functional::hook;

#[repr(transparent)]
struct UseStateReducer<T> {
value: T,
}
Expand Down Expand Up @@ -111,6 +113,12 @@ impl<T: fmt::Debug> fmt::Debug for UseStateHandle<T> {
}

impl<T> UseStateHandle<T> {
/// Returns the inner value of the handle.
pub fn get(&self) -> Rc<T> {
// Safety: `UseStateReducer<T>` is `repr(transparent)` and only contains `T`
unsafe { transmute(self.inner.get()) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the transmute somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, this transmute converts between Rc<UseStateReducer<T>> and Rc<T>, there's not much you can do with a value inside an Rc

Copy link
Contributor

@kirillsemyonkin kirillsemyonkin Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would need to convert Rc<UseStateReducer<T>> -> Rc<T> (assuming T is not guaranteed to be Clone) some other way then (maybe via Any?). I think using repr(transparent) was a brilliant idea for Rcs and now I hope Rust makes it like a safe trait / Rc function.

}

/// Replaces the value
pub fn set(&self, value: T) {
self.inner.dispatch(value)
Expand All @@ -122,6 +130,15 @@ impl<T> UseStateHandle<T> {
inner: self.inner.dispatcher(),
}
}

/// Destructures the handle into its 2 parts:
/// 0: The current associated state;
/// 1: The setter responsible for changing the state on demand.
pub fn into_inner(self) -> (Rc<T>, UseStateSetter<T>) {
let (data, inner) = self.inner.into_inner();
// Safety: check the `get` method above
(unsafe { transmute(data) }, UseStateSetter { inner })
}
}

impl<T> Deref for UseStateHandle<T> {
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/suspense/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ where

use_memo_base(
move |deps| {
let self_id = latest_id.get().wrapping_add(1);
let self_id = (*latest_id).get().wrapping_add(1);
// As long as less than 2**32 futures are in flight wrapping_add is fine
(*latest_id).set(self_id);
let deps = Rc::new(deps);
let task = f(deps.clone());
let suspension = Suspension::from_future(async move {
let result = task.await;
if latest_id.get() == self_id {
if (*latest_id).get() == self_id {
output.set(Some(result));
}
});
Expand Down
1 change: 1 addition & 0 deletions packages/yew/tests/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn use_state_works() {
#[function_component(UseComponent)]
fn use_state_comp() -> Html {
let counter = use_state(|| 0);
assert_eq!(*counter.get(), *counter);
if *counter < 5 {
counter.set(*counter + 1)
}
Expand Down
Loading