Skip to content

Commit

Permalink
added (UseReducerHandle/UseStateHandle)::(get/into_inner)
Browse files Browse the repository at this point in the history
  • Loading branch information
its-the-shrimp committed Sep 28, 2023
1 parent 189a729 commit 2c54ff7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
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()) }
}

/// 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
2 changes: 2 additions & 0 deletions packages/yew/tests/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod common;

use std::rc::Rc;
use std::time::Duration;

use common::obtain_result;
Expand All @@ -16,6 +17,7 @@ async fn use_state_works() {
#[function_component(UseComponent)]
fn use_state_comp() -> Html {
let counter = use_state(|| 0);
assert_eq!(counter.get(), Rc::new(0));
if *counter < 5 {
counter.set(*counter + 1)
}
Expand Down

0 comments on commit 2c54ff7

Please sign in to comment.