Prechádzať zdrojové kódy

handle rsx!{cx, ...}

Evan Almloff 3 rokov pred
rodič
commit
2a6d9ed075

+ 10 - 3
packages/core-macro/src/lib.rs

@@ -189,8 +189,8 @@ pub fn rsx(s: TokenStream) -> TokenStream {
                 use dioxus_rsx_interpreter::captuered_context::CapturedContextBuilder;
 
                 match CapturedContextBuilder::from_call_body(body) {
-                    Ok(captured) => quote::quote! {
-                        {
+                    Ok(captured) => {
+                        let lazy = quote::quote! {
                             LazyNodes::new(move |__cx|{
                                 let captured = #captured;
                                 let line_num = get_line_num();
@@ -198,9 +198,16 @@ pub fn rsx(s: TokenStream) -> TokenStream {
 
                                 resolve_scope(line_num, text, captured, __cx)
                             })
+                        };
+                        if let Some(cx) = captured.custom_context {
+                            quote::quote! {
+                                #cx.render(#lazy)
+                            }
+                            .into()
+                        } else {
+                            lazy.into()
                         }
                     }
-                    .into(),
                     Err(err) => err.into_compile_error().into(),
                 }
             }

+ 1 - 1
packages/rsx_interpreter/Cargo.toml

@@ -8,7 +8,7 @@ license = "MIT/Apache-2.0"
 syn = { version = "1.0", features = ["extra-traits"] }
 quote = "1.0"
 serde = { version = "1.0", features = ["derive"] }
-serde_json = { vesion = "1.0" }
+serde_json = "1.0"
 lazy_static = "1.4.0"
 
 dioxus-rsx = { path = "../rsx", default-features = false }

+ 4 - 1
packages/rsx_interpreter/src/captuered_context.rs

@@ -4,7 +4,7 @@ use dioxus_rsx::{
 };
 use quote::{quote, ToTokens, TokenStreamExt};
 use std::collections::HashMap;
-use syn::{Expr, Result};
+use syn::{Expr, Ident, Result};
 
 #[derive(Default)]
 pub struct CapturedContextBuilder {
@@ -14,6 +14,7 @@ pub struct CapturedContextBuilder {
     pub iterators: Vec<BodyNode>,
     pub captured_expressions: Vec<Expr>,
     pub listeners: Vec<ElementAttrNamed>,
+    pub custom_context: Option<Ident>,
 }
 
 impl CapturedContextBuilder {
@@ -28,6 +29,7 @@ impl CapturedContextBuilder {
 
     pub fn from_call_body(body: CallBody) -> Result<Self> {
         let mut new = Self::default();
+        new.custom_context = body.custom_context;
         for node in body.roots {
             new.extend(Self::find_captured(node)?);
         }
@@ -86,6 +88,7 @@ impl ToTokens for CapturedContextBuilder {
             iterators,
             captured_expressions,
             listeners,
+            custom_context: _,
         } = self;
         let listeners_str = listeners
             .iter()