From 6b7d707192af87ec52f7025a48a1d88ae67b2457 Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 14 Oct 2021 12:57:55 -0700 Subject: [PATCH 1/2] Prevent binding from panicking when not in wasm --- .../sycamore-macro/src/template/attributes.rs | 15 +++++++++++++++ packages/sycamore/tests/ssr/main.rs | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/packages/sycamore-macro/src/template/attributes.rs b/packages/sycamore-macro/src/template/attributes.rs index 0b67fe7d3..75c2e80c8 100644 --- a/packages/sycamore-macro/src/template/attributes.rs +++ b/packages/sycamore-macro/src/template/attributes.rs @@ -278,6 +278,7 @@ impl ToTokens for Attribute { JsPropertyType::String => quote! { ::std::string::String }, }; + #[cfg(target = "wasm32-unknown-unknown")] let convert_into_jsvalue_fn = match property_ty { JsPropertyType::Bool => { quote! { ::sycamore::rt::JsValue::from_bool(*signal.get()) } @@ -307,6 +308,7 @@ impl ToTokens for Attribute { }, }; + #[cfg(target = "wasm32-unknown-unknown")] tokens.extend(quote_spanned! { expr_span=> { let signal: ::sycamore::reactive::Signal<#value_ty> = #expr; @@ -330,6 +332,19 @@ impl ToTokens for Attribute { }), ) }}); + + #[cfg(not(target = "wasm32-unknown-unknown"))] + tokens.extend(quote_spanned! { expr_span=> { + let signal: ::sycamore::reactive::Signal<#value_ty> = #expr; + + ::sycamore::generic_node::GenericNode::event( + &__el, + #event_name, + ::std::boxed::Box::new(move |event: ::sycamore::rt::Event| { + signal.set(#convert_from_jsvalue_fn); + }), + ) + }}); } AttributeType::Ref => { tokens.extend(quote_spanned! { expr_span=>{ diff --git a/packages/sycamore/tests/ssr/main.rs b/packages/sycamore/tests/ssr/main.rs index 49d55a47a..fcf52d1de 100644 --- a/packages/sycamore/tests/ssr/main.rs +++ b/packages/sycamore/tests/ssr/main.rs @@ -98,3 +98,15 @@ fn indexed() { let actual = sycamore::render_to_string(|| node.clone()); assert_eq!(actual, ""); } + +#[test] +fn bind() { + let signal = Signal::new(String::new()); + + let node = template! { + input(bind:value=signal) + }; + + let actual = sycamore::render_to_string(|| node); + assert_eq!(actual, ""); +} From 24e9e8743a5d733f5523f45594b7d4eecf99ad80 Mon Sep 17 00:00:00 2001 From: Zoey Date: Thu, 14 Oct 2021 17:30:43 -0700 Subject: [PATCH 2/2] Fix cfg attributes --- .../sycamore-macro/src/template/attributes.rs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/packages/sycamore-macro/src/template/attributes.rs b/packages/sycamore-macro/src/template/attributes.rs index 75c2e80c8..235324799 100644 --- a/packages/sycamore-macro/src/template/attributes.rs +++ b/packages/sycamore-macro/src/template/attributes.rs @@ -278,7 +278,6 @@ impl ToTokens for Attribute { JsPropertyType::String => quote! { ::std::string::String }, }; - #[cfg(target = "wasm32-unknown-unknown")] let convert_into_jsvalue_fn = match property_ty { JsPropertyType::Bool => { quote! { ::sycamore::rt::JsValue::from_bool(*signal.get()) } @@ -308,10 +307,10 @@ impl ToTokens for Attribute { }, }; - #[cfg(target = "wasm32-unknown-unknown")] tokens.extend(quote_spanned! { expr_span=> { let signal: ::sycamore::reactive::Signal<#value_ty> = #expr; + #[cfg(target_arch = "wasm32")] ::sycamore::reactive::create_effect({ let signal = ::std::clone::Clone::clone(&signal); let __el = ::std::clone::Clone::clone(&__el); @@ -332,19 +331,6 @@ impl ToTokens for Attribute { }), ) }}); - - #[cfg(not(target = "wasm32-unknown-unknown"))] - tokens.extend(quote_spanned! { expr_span=> { - let signal: ::sycamore::reactive::Signal<#value_ty> = #expr; - - ::sycamore::generic_node::GenericNode::event( - &__el, - #event_name, - ::std::boxed::Box::new(move |event: ::sycamore::rt::Event| { - signal.set(#convert_from_jsvalue_fn); - }), - ) - }}); } AttributeType::Ref => { tokens.extend(quote_spanned! { expr_span=>{