Skip to content

Commit

Permalink
Revert "Users always have to manually construct dependency into Rc."
Browse files Browse the repository at this point in the history
This reverts commit 9eee1da.
  • Loading branch information
futursolo committed Jul 19, 2022
1 parent d6bb90d commit 81f2fda
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
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: Rc<D>) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
pub fn use_prepared_state<T, D>(deps: 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: Rc<D>,
deps: D,
}

impl<T, D> Hook for HookProvider<T, D>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::suspense::SuspensionResult;
#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: Rc<D>,
deps: 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: Rc<D>,
deps: 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: Rc<D>,
deps: 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: Rc<D>,
deps: D,
f: F,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::suspense::SuspensionResult;

#[doc(hidden)]
#[hook]
pub fn use_prepared_state<T, D>(_deps: Rc<D>) -> SuspensionResult<Option<Rc<T>>>
pub fn use_prepared_state<T, D>(_deps: 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: Rc<D>) -> SuspensionResult<Option<Rc<T>>>
pub fn use_prepared_state_with_suspension<T, D>(_deps: D) -> SuspensionResult<Option<Rc<T>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
T: Serialize + DeserializeOwned + 'static,
Expand Down
14 changes: 8 additions & 6 deletions packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::suspense::{Suspension, SuspensionResult};
#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: Rc<D>,
deps: 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: Rc<D>,
deps: D,
f: F,
}

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

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

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

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

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

let result = use_state(|| {
let (s, handle) = Suspension::new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::suspense::SuspensionResult;
#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: Rc<D>,
deps: 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: Rc<D>,
deps: D,
f: F,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ where
#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: Rc<D>,
deps: 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: Rc<D>,
deps: 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 Self { f, deps } = self;
let f = self.f;

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

Expand Down
5 changes: 2 additions & 3 deletions packages/yew/tests/use_prepared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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 }, Rc::new(()))?.unwrap_or_default();
let ctr = use_prepared_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default();

Ok(html! {
<div>
Expand Down Expand Up @@ -69,8 +69,7 @@ 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 }, Rc::new(()))?.unwrap_or_default();
let ctr = use_prepared_state!(async move |_| -> u32 { 12345 }, ())?.unwrap_or_default();

Ok(html! {
<div>
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/tests/use_transitive_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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 }, Rc::new(()))?.unwrap_or_default();
let ctr = use_transitive_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default();

Ok(html! {
<div>
Expand Down

0 comments on commit 81f2fda

Please sign in to comment.