Skip to content

Commit

Permalink
Simplify component state (#1186)
Browse files Browse the repository at this point in the history
* Simplify component state

* Add another test
  • Loading branch information
jstarry authored and mergify[bot] committed May 14, 2020
1 parent 8b231d5 commit 5f33d54
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 232 deletions.
2 changes: 1 addition & 1 deletion yew-functional/src/lib.rs
Expand Up @@ -255,7 +255,7 @@ where
}
use_hook(
move |state: &mut UseEffectState<Dependents, Destructor>, hook_update| {
let mut should_update = !(*state.deps == *deps);
let mut should_update = *state.deps != *deps;

move || {
hook_update(move |state: &mut UseEffectState<Dependents, Destructor>| {
Expand Down
92 changes: 79 additions & 13 deletions yew-functional/tests/lib.rs
Expand Up @@ -151,34 +151,100 @@ fn use_effect_destroys_on_component_drop() {
struct UseEffectFunction {}
struct UseEffectWrapper {}
#[derive(Properties, Clone)]
struct DestroyCalledProps {
struct WrapperProps {
destroy_called: Rc<dyn Fn()>,
}
impl PartialEq for DestroyCalledProps {
impl PartialEq for WrapperProps {
fn eq(&self, _other: &Self) -> bool {
false
}
}
#[derive(Properties, Clone)]
struct FunctionProps {
effect_called: Rc<dyn Fn()>,
destroy_called: Rc<dyn Fn()>,
}
impl PartialEq for FunctionProps {
fn eq(&self, _other: &Self) -> bool {
false
}
}
type UseEffectComponent = FunctionComponent<UseEffectFunction>;
type UseEffectWrapperComponent = FunctionComponent<UseEffectWrapper>;
impl FunctionProvider for UseEffectFunction {
type TProps = DestroyCalledProps;
type TProps = FunctionProps;

fn run(props: &Self::TProps) -> Html {
let effect_called = props.effect_called.clone();
let destroy_called = props.destroy_called.clone();
use_effect_with_deps(
move |_| {
move || {
destroy_called();
}
effect_called();
move || destroy_called()
},
(),
);
return html! {};
}
}
impl FunctionProvider for UseEffectWrapper {
type TProps = DestroyCalledProps;
type TProps = WrapperProps;

fn run(props: &Self::TProps) -> Html {
let (show, set_show) = use_state(|| true);
if *show {
let effect_called: Rc<dyn Fn()> = Rc::new(move || set_show(false));
return html! {
<UseEffectComponent destroy_called=props.destroy_called.clone() effect_called=effect_called />
};
} else {
return html! {
<div>{"EMPTY"}</div>
};
}
}
}
let app: App<UseEffectWrapperComponent> = yew::App::new();
let destroy_counter = Rc::new(std::cell::RefCell::new(0));
let destroy_counter_c = destroy_counter.clone();
app.mount_with_props(
yew::utils::document().get_element_by_id("output").unwrap(),
WrapperProps {
destroy_called: Rc::new(move || *destroy_counter_c.borrow_mut().deref_mut() += 1),
},
);
assert_eq!(1, *destroy_counter.borrow().deref());
}

#[wasm_bindgen_test]
fn use_effect_not_called_if_destroyed_immediately() {
struct UseEffectFunction {}
struct UseEffectWrapper {}
#[derive(Properties, Clone)]
struct EffectCalledProps {
effect_called: Rc<dyn Fn()>,
}
impl PartialEq for EffectCalledProps {
fn eq(&self, _other: &Self) -> bool {
false
}
}
type UseEffectComponent = FunctionComponent<UseEffectFunction>;
type UseEffectWrapperComponent = FunctionComponent<UseEffectWrapper>;
impl FunctionProvider for UseEffectFunction {
type TProps = EffectCalledProps;

fn run(props: &Self::TProps) -> Html {
let effect_called = props.effect_called.clone();
use_effect(move || {
effect_called();
|| {}
});
html! {}
}
}
impl FunctionProvider for UseEffectWrapper {
type TProps = EffectCalledProps;

fn run(props: &Self::TProps) -> Html {
let (show, set_show) = use_state(|| true);
Expand All @@ -192,7 +258,7 @@ fn use_effect_destroys_on_component_drop() {

if *show {
return html! {
<UseEffectComponent destroy_called=props.destroy_called.clone() />
<UseEffectComponent effect_called=props.effect_called.clone() />
};
} else {
return html! {
Expand All @@ -202,15 +268,15 @@ fn use_effect_destroys_on_component_drop() {
}
}
let app: App<UseEffectWrapperComponent> = yew::App::new();
let destroy_counter = Rc::new(std::cell::RefCell::new(0));
let destroy_counter_c = destroy_counter.clone();
let effect_counter = Rc::new(std::cell::RefCell::new(0));
let effect_counter_c = effect_counter.clone();
app.mount_with_props(
yew::utils::document().get_element_by_id("output").unwrap(),
DestroyCalledProps {
destroy_called: Rc::new(move || *destroy_counter_c.borrow_mut().deref_mut() += 1),
EffectCalledProps {
effect_called: Rc::new(move || *effect_counter_c.borrow_mut().deref_mut() += 1),
},
);
assert_eq!(1, *destroy_counter.borrow().deref());
assert_eq!(0, *effect_counter.borrow().deref());
}

#[wasm_bindgen_test]
Expand Down

0 comments on commit 5f33d54

Please sign in to comment.