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

Updated dependencies to yew 0.21 and gloo 0.10 #90

Merged
merged 4 commits into from
Oct 14, 2023
Merged
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## Release 0.9.0

### Breaking Changes

- Compatible with Yew 0.21. Due to the breaking API changes in Yew 0.21 not backwards compatible with Yew 0.20.

### Other Changes

- Updated the Gloo dependency to 0.10

## Release 0.8.0

### Breaking Changes
Expand Down
8 changes: 4 additions & 4 deletions crates/bounce/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ rust-version = "1.64.0"
anymap2 = "0.13.0"
once_cell = "1.18.0"
wasm-bindgen = "0.2.87"
yew = "0.20"
yew = "0.21"
bounce-macros = { path = "../bounce-macros", version = "0.8.0" }
futures = "0.3.28"

async-trait = { version = "0.1.68", optional = true }
gloo = { version = "0.9.0", features = ["futures"], optional = true }
gloo = { version = "0.10.0", features = ["futures"], optional = true }
html-escape = { version = "0.2.13", optional = true }
serde = { version = "1.0.164", features = ["derive"] }
tracing = "0.1"
Expand All @@ -47,8 +47,8 @@ helmet = ["gloo", "web-sys"]

[dev-dependencies]
wasm-bindgen-test = "0.3.37"
gloo = { version = "0.9.0", features = ["futures"] }
yew = { version = "0.20", features = ["csr", "ssr"] }
gloo = { version = "0.10.0", features = ["futures"] }
yew = { version = "0.21", features = ["csr", "ssr"] }
thiserror = "1"

[dev-dependencies.web-sys]
Expand Down
52 changes: 23 additions & 29 deletions crates/bounce/src/helmet/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,13 @@ mod guard {
let guard = use_slice::<HelmetBridgeGuard>();
let root = use_context::<BounceRootState>().expect_throw("No bounce root found.");

use_effect_with_deps(
move |_| {
guard.dispatch(HelmetBridgeGuardAction::Increment);
use_effect_with(root, move |_| {
guard.dispatch(HelmetBridgeGuardAction::Increment);

move || {
guard.dispatch(HelmetBridgeGuardAction::Decrement);
}
},
root,
);
move || {
guard.dispatch(HelmetBridgeGuardAction::Decrement);
}
});
}
}

Expand Down Expand Up @@ -244,24 +241,26 @@ pub fn helmet_bridge(props: &HelmetBridgeProps) -> Html {
}

// Remove pre-rendered tags.
use_effect_with_deps(
|_| {
let pre_rendered = head()
.query_selector_all("[data-bounce-helmet=pre-render]")
.expect_throw("failed to read pre rendered tags");

for i in 0..pre_rendered.length() {
if let Some(m) = pre_rendered.get(i) {
if let Some(parent) = m.parent_node() {
let _ = parent.remove_child(&m);
}
use_effect_with((), |_| {
let pre_rendered = head()
.query_selector_all("[data-bounce-helmet=pre-render]")
.expect_throw("failed to read pre rendered tags");

for i in 0..pre_rendered.length() {
if let Some(m) = pre_rendered.get(i) {
if let Some(parent) = m.parent_node() {
let _ = parent.remove_child(&m);
}
}
},
(),
);
}
});

use_effect_with_deps(
use_effect_with(
(
helmet_states,
props.format_title.clone(),
props.default_title.clone(),
),
move |(helmet_states, format_title, default_title)| {
// Calculate tags to render.
let to_render =
Expand All @@ -272,11 +271,6 @@ pub fn helmet_bridge(props: &HelmetBridgeProps) -> Html {

|| {}
},
(
helmet_states,
props.format_title.clone(),
props.default_title.clone(),
),
);

Html::default()
Expand Down
11 changes: 9 additions & 2 deletions crates/bounce/src/helmet/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ fn collect_str_in_children(tag: &VNode) -> String {
}

fn collect_text_content(tag: &VTag) -> String {
collect_str_in_children(&tag.children().clone().into())
if let Some(children) = tag.children() {
collect_str_in_children(children)
} else {
String::default()
}
}

fn collect_attributes(tag: &VTag) -> BTreeMap<Arc<str>, Arc<str>> {
Expand Down Expand Up @@ -71,8 +75,11 @@ fn assert_empty_node(node: &VNode) {
VNode::VRaw(_) => throw_str("expected nothing, found raw html."),
}
}

fn assert_empty_children(tag: &VTag) {
assert_empty_node(&tag.children().clone().into())
if let Some(children) = tag.children() {
assert_empty_node(children);
}
}

#[derive(Properties, PartialEq, Clone)]
Expand Down
54 changes: 24 additions & 30 deletions crates/bounce/src/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,45 +56,39 @@ pub fn bounce_root(props: &BounceRootProps) -> Html {
#[allow(clippy::redundant_clone)]
{
let root_state = root_state.clone();
use_effect_with_deps(
move |_| {
// We clear all states manually.
move || {
root_state.clear();
}
},
(),
);
use_effect_with((), move |_| {
// We clear all states manually.
move || {
root_state.clear();
}
});
}

#[allow(clippy::unused_unit, clippy::redundant_clone)]
{
let _root_state = root_state.clone();
let _ = use_transitive_state!(
move |_| -> () {
#[cfg(feature = "ssr")]
#[cfg(feature = "helmet")]
{
// Workaround to send helmet states back to static writer
use crate::helmet::StaticWriterState;
let _ = use_transitive_state!((), move |_| -> () {
#[cfg(feature = "ssr")]
#[cfg(feature = "helmet")]
{
// Workaround to send helmet states back to static writer
use crate::helmet::StaticWriterState;

let states = _root_state.states();
let writer_state = states.get_atom_value::<StaticWriterState>();
let states = _root_state.states();
let writer_state = states.get_atom_value::<StaticWriterState>();

if let Some(ref w) = writer_state.writer {
w.send_helmet(
states,
writer_state.format_title.clone(),
writer_state.default_title.clone(),
);
}
if let Some(ref w) = writer_state.writer {
w.send_helmet(
states,
writer_state.format_title.clone(),
writer_state.default_title.clone(),
);
}
}

// We drop the root state on SSR as well.
_root_state.clear();
},
()
);
// We drop the root state on SSR as well.
_root_state.clear();
});
}

html! {
Expand Down
40 changes: 17 additions & 23 deletions crates/bounce/src/query/use_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,38 +225,32 @@ pub fn use_mutation<T>() -> UseMutationHandle<T>
where
T: Mutation + 'static,
{
let id = *use_memo(|_| HandleId::default(), ());
let id = *use_memo((), |_| HandleId::default());
let dispatch_state = use_slice_dispatch::<MutationSlice<T>>();
let run_mutation = use_future_notion_runner::<RunMutation<T>>();
let state = use_input_selector_value::<MutationSelector<T>>(id.into());

{
use_effect_with_deps(
|id| {
let id = *id;
dispatch_state(MutationSliceAction::Create(id));
use_effect_with(id, |id| {
let id = *id;
dispatch_state(MutationSliceAction::Create(id));

move || {
dispatch_state(MutationSliceAction::Destroy(id));
}
},
id,
);
move || {
dispatch_state(MutationSliceAction::Destroy(id));
}
});
}

let state = use_memo(
|state| match state.value.as_ref() {
Some(MutationSliceValue::Idle) | None => MutationState::Idle,
Some(MutationSliceValue::Loading { .. }) => MutationState::Loading,
Some(MutationSliceValue::Completed { result, .. }) => MutationState::Completed {
result: result.clone(),
},
Some(MutationSliceValue::Outdated { result, .. }) => MutationState::Refreshing {
last_result: result.clone(),
},
let state = use_memo(state, |state| match state.value.as_ref() {
Some(MutationSliceValue::Idle) | None => MutationState::Idle,
Some(MutationSliceValue::Loading { .. }) => MutationState::Loading,
Some(MutationSliceValue::Completed { result, .. }) => MutationState::Completed {
result: result.clone(),
},
state,
);
Some(MutationSliceValue::Outdated { result, .. }) => MutationState::Refreshing {
last_result: result.clone(),
},
});

UseMutationHandle {
id,
Expand Down
80 changes: 37 additions & 43 deletions crates/bounce/src/query/use_prepared_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
T::Input: Clone + Serialize + for<'de> Deserialize<'de>,
T::Error: Clone + Serialize + for<'de> Deserialize<'de>,
{
let id = *use_memo(|_| Id::new(), ());
let id = *use_memo((), |_| Id::new());
let value_state = use_input_selector_value::<QuerySelector<T>>(input.clone());
let dispatch_state = use_slice_dispatch::<QuerySlice<T>>();
let run_query = use_future_notion_runner::<RunQuery<T>>();
Expand All @@ -95,8 +95,11 @@ where
let _run_query = run_query.clone();
let _root = use_context::<BounceRootState>().expect_throw("No bounce root found.");

let prepared_value = use_prepared_state!(
async move |input| -> std::result::Result<T, T::Error> {
let prepared_value =
use_prepared_state!((*input).clone(), async move |input| -> std::result::Result<
T,
T::Error,
> {
use std::cell::RefCell;
use std::time::Duration;

Expand Down Expand Up @@ -145,61 +148,53 @@ where
}
}
}
},
(*input).clone()
)?;
})?;

(*use_memo(
|p| p.clone().map(|m| (*m).clone().map(Rc::new)),
prepared_value,
))
(*use_memo(prepared_value, |p| {
p.clone().map(|m| (*m).clone().map(Rc::new))
}))
.clone()
};

let value = use_memo(
|v| match v.value {
Some(QuerySliceValue::Loading { .. }) | None => Err(Suspension::new()),
Some(QuerySliceValue::Completed { id, result: ref m }) => {
Ok((id, Rc::new(QueryState::Completed { result: m.clone() })))
}
Some(QuerySliceValue::Outdated { id, result: ref m }) => Ok((
id,
Rc::new(QueryState::Refreshing {
last_result: m.clone(),
}),
)),
},
value_state.clone(),
);
let value = use_memo(value_state.clone(), |v| match v.value {
Some(QuerySliceValue::Loading { .. }) | None => Err(Suspension::new()),
Some(QuerySliceValue::Completed { id, result: ref m }) => {
Ok((id, Rc::new(QueryState::Completed { result: m.clone() })))
}
Some(QuerySliceValue::Outdated { id, result: ref m }) => Ok((
id,
Rc::new(QueryState::Refreshing {
last_result: m.clone(),
}),
)),
});

{
let input = input.clone();
let run_query = run_query.clone();
let dispatch_state = dispatch_state.clone();

use_memo(
move |_| match prepared_value {
Some(m) => dispatch_state(QuerySliceAction::LoadPrepared {
id,
input,
result: m,
}),
None => run_query(RunQueryInput {
id,
input: input.clone(),
sender: Rc::default(),
is_refresh: false,
}),
},
(),
);
use_memo((), move |_| match prepared_value {
Some(m) => dispatch_state(QuerySliceAction::LoadPrepared {
id,
input,
result: m,
}),
None => run_query(RunQueryInput {
id,
input: input.clone(),
sender: Rc::default(),
is_refresh: false,
}),
});
}

{
let input = input.clone();
let run_query = run_query.clone();

use_effect_with_deps(
use_effect_with(
(id, input, value_state.clone()),
move |(id, input, value_state)| {
if matches!(value_state.value, Some(QuerySliceValue::Outdated { .. })) {
run_query(RunQueryInput {
Expand All @@ -212,7 +207,6 @@ where

|| {}
},
(id, input, value_state.clone()),
);
}

Expand Down
Loading