Skip to content

Commit

Permalink
Users always have to manually construct dependency into Rc.
Browse files Browse the repository at this point in the history
  • Loading branch information
futursolo committed Jul 19, 2022
1 parent f2d2332 commit 9eee1da
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 25 deletions.
Expand Up @@ -40,7 +40,7 @@ async fn decode_base64(_s: &str) -> Result<Vec<u8>, JsValue> {
}

#[doc(hidden)]
pub fn use_prepared_state<T, D>(deps: D) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
pub fn use_prepared_state<T, D>(deps: Rc<D>) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
T: Serialize + DeserializeOwned + 'static,
Expand All @@ -51,7 +51,7 @@ where
T: Serialize + DeserializeOwned + 'static,
{
_marker: PhantomData<T>,
deps: D,
deps: Rc<D>,
}

impl<T, D> Hook for HookProvider<T, D>
Expand Down
Expand Up @@ -14,7 +14,7 @@ use crate::suspense::SuspensionResult;
#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -27,7 +27,7 @@ where
T: Serialize + DeserializeOwned + 'static,
F: FnOnce(Rc<D>) -> T,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand All @@ -53,7 +53,7 @@ where
#[doc(hidden)]
pub fn use_prepared_state_with_suspension<T, D, F, U>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -68,7 +68,7 @@ where
F: FnOnce(Rc<D>) -> U,
U: 'static + Future<Output = T>,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand Down
Expand Up @@ -10,7 +10,7 @@ use crate::suspense::SuspensionResult;

#[doc(hidden)]
#[hook]
pub fn use_prepared_state<T, D>(_deps: D) -> SuspensionResult<Option<Rc<T>>>
pub fn use_prepared_state<T, D>(_deps: Rc<D>) -> SuspensionResult<Option<Rc<T>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
T: Serialize + DeserializeOwned + 'static,
Expand All @@ -20,7 +20,7 @@ where

#[doc(hidden)]
#[hook]
pub fn use_prepared_state_with_suspension<T, D>(_deps: D) -> SuspensionResult<Option<Rc<T>>>
pub fn use_prepared_state_with_suspension<T, D>(_deps: Rc<D>) -> SuspensionResult<Option<Rc<T>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
T: Serialize + DeserializeOwned + 'static,
Expand Down
14 changes: 6 additions & 8 deletions packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs
Expand Up @@ -15,7 +15,7 @@ use crate::suspense::{Suspension, SuspensionResult};
#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -28,7 +28,7 @@ where
T: Serialize + DeserializeOwned + 'static,
F: FnOnce(Rc<D>) -> T,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand All @@ -41,8 +41,7 @@ where
type Output = SuspensionResult<Option<Rc<T>>>;

fn run(self, ctx: &mut HookContext) -> Self::Output {
let f = self.f;
let deps = Rc::new(self.deps);
let Self { f, deps } = self;

let state = {
let deps = deps.clone();
Expand Down Expand Up @@ -70,7 +69,7 @@ where
#[doc(hidden)]
pub fn use_prepared_state_with_suspension<T, D, F, U>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -85,7 +84,7 @@ where
F: FnOnce(Rc<D>) -> U,
U: 'static + Future<Output = T>,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand All @@ -99,8 +98,7 @@ where
type Output = SuspensionResult<Option<Rc<T>>>;

fn run(self, ctx: &mut HookContext) -> Self::Output {
let f = self.f;
let deps = Rc::new(self.deps);
let Self { f, deps } = self;

let result = use_state(|| {
let (s, handle) = Suspension::new();
Expand Down
Expand Up @@ -13,7 +13,7 @@ use crate::suspense::SuspensionResult;
#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -26,7 +26,7 @@ where
T: Serialize + DeserializeOwned + 'static,
F: 'static + FnOnce(Rc<D>) -> T,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand Down
Expand Up @@ -40,7 +40,7 @@ where
#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: D,
deps: Rc<D>,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -53,7 +53,7 @@ where
T: Serialize + DeserializeOwned + 'static,
F: 'static + FnOnce(Rc<D>) -> T,
{
deps: D,
deps: Rc<D>,
f: F,
}

Expand All @@ -66,12 +66,12 @@ where
type Output = SuspensionResult<Option<Rc<T>>>;

fn run(self, ctx: &mut HookContext) -> Self::Output {
let f = self.f;
let Self { f, deps } = self;

ctx.next_prepared_state(move |_re_render, _| -> TransitiveStateBase<T, D, F> {
TransitiveStateBase {
state_fn: Some(f).into(),
deps: self.deps.into(),
deps,
}
});

Expand Down
5 changes: 3 additions & 2 deletions packages/yew/tests/use_prepared_state.rs
Expand Up @@ -18,7 +18,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
async fn use_prepared_state_works() {
#[function_component]
fn Comp() -> HtmlResult {
let ctr = use_prepared_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default();
let ctr = use_prepared_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default();

Ok(html! {
<div>
Expand Down Expand Up @@ -68,7 +68,8 @@ async fn use_prepared_state_works() {
async fn use_prepared_state_with_suspension_works() {
#[function_component]
fn Comp() -> HtmlResult {
let ctr = use_prepared_state!(async move |_| -> u32 { 12345 }, ())?.unwrap_or_default();
let ctr =
use_prepared_state!(async move |_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default();

Ok(html! {
<div>
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/tests/use_transitive_state.rs
Expand Up @@ -17,7 +17,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
async fn use_transitive_state_works() {
#[function_component]
fn Comp() -> HtmlResult {
let ctr = use_transitive_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default();
let ctr = use_transitive_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default();

Ok(html! {
<div>
Expand Down

0 comments on commit 9eee1da

Please sign in to comment.