add RsxTextIndex

This commit is contained in:
Evan Almloff committed 2022-05-25 11:20:07 -05:00
1 parent c7c84da8ab
commit ca10d89670
7 files changed
+63 -18

No files matched your search

+2 -1
View File
@@ -45,7 +45,7 @@ router = ["dioxus-router"]
tui = ["dioxus-tui"]
liveview = ["dioxus-liveview"]
autofmt = ["dioxus-autofmt"]
hot_reload = ["dioxus-core-macro/hot_reload"]
hot_reload = ["dioxus-core-macro/hot_reload", "dioxus-rsx-interperter"]
[workspace]
members = [
@@ -85,6 +85,7 @@ criterion = "0.3.5"
thiserror = "1.0.30"
env_logger = "0.9.0"
[[bench]]
name = "create"
harness = false
+17 -4
View File
@@ -1,7 +1,5 @@
use proc_macro::TokenStream;
use quote::ToTokens;
use rsx::{BodyNode, Component, ContentField, ElementAttr, IfmtInput};
use std::collections::HashMap;
use quote::{quote, ToTokens};
use syn::parse_macro_input;
mod inlineprops;
@@ -190,7 +188,22 @@ pub fn rsx(s: TokenStream) -> TokenStream {
use dioxus_rsx_interperter::captuered_context::CapturedContextBuilder;
let captured = CapturedContextBuilder::from_call_body(body);
captured.to_token_stream().into()
quote! {
{
let line_num = get_line_num();
LazyNodes::new(|factory|{
let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap();
let read = rsx_text_index.read();
let text = read.get(line_num).unwrap();
interpert_rsx(
factory,
&text,
#captured
)
})
}
}
.into()
}
#[cfg(not(feature = "hot_reload"))]
body.to_token_stream().into()
-1
View File
@@ -49,4 +49,3 @@ backtrace = "0.3"
default = []
serialize = ["serde"]
debug_vdom = []
-2
View File
@@ -85,8 +85,6 @@ pub mod prelude {
fc_to_builder, Attributes, Component, DioxusElement, Element, EventHandler, Fragment,
LazyNodes, NodeFactory, Properties, Scope, ScopeId, ScopeState, VNode, VirtualDom,
};
#[cfg(feature = "hot_reload")]
pub use dioxus_rsx_interperter::captuered_context::{CapturedContext, IfmtArgs};
}
pub mod exports {
@@ -6,10 +6,10 @@ use syn::Expr;
#[derive(Default)]
pub struct CapturedContextBuilder {
attributes: HashMap<String, IfmtInput>,
text: Vec<IfmtInput>,
components: Vec<Component>,
iterators: Vec<Expr>,
pub attributes: HashMap<String, IfmtInput>,
pub text: Vec<IfmtInput>,
pub components: Vec<Component>,
pub iterators: Vec<Expr>,
}
impl CapturedContextBuilder {
@@ -89,7 +89,7 @@ impl ToTokens for CapturedContextBuilder {
tokens.append_all(quote! {
CapturedContext {
captured: IfmtArgs{
named_args: &'static [#((#captured_names, #captured_expr)),*]
named_args: &[#((#captured_names, #captured_expr)),*]
},
components: vec![#(#components),*],
iterators: vec![#(#iterators),*],
@@ -105,11 +105,11 @@ struct CapturedComponentBuilder {
pub struct CapturedContext<'a> {
// map of the attribute name to the formated value
captured: IfmtArgs,
pub captured: IfmtArgs,
// the only thing we can update in component is the children
components: Vec<VNode<'a>>,
pub components: Vec<VNode<'a>>,
// we can't reasonably interpert iterators, so they are staticly inserted
iterators: Vec<VNode<'a>>,
pub iterators: Vec<VNode<'a>>,
}
pub struct IfmtArgs {
+30 -2
View File
@@ -1,6 +1,10 @@
use crate::interperter::build;
use dioxus_core::LazyNodes;
use dioxus_core::{LazyNodes, VNode};
use dioxus_rsx::CallBody;
use std::collections::HashMap;
use std::panic::Location;
use std::rc::Rc;
use std::sync::{RwLock, RwLockReadGuard};
use syn::{parse_str, Result};
mod attributes;
@@ -8,6 +12,30 @@ pub mod captuered_context;
mod elements;
mod interperter;
pub fn rsx_to_html(text: &str, context: &captuered_context::CapturedContext) -> {
pub fn interpert_rsx<'a, 'b>(
factory: dioxus_core::NodeFactory<'a>,
text: &str,
context: captuered_context::CapturedContext,
) -> VNode<'a> {
panic!()
}
#[track_caller]
pub fn get_line_num() -> &'static Location<'static> {
Location::caller()
}
#[derive(Debug, Default, Clone)]
pub struct RsxTextIndex {
hm: Rc<RwLock<HashMap<&'static Location<'static>, String>>>,
}
impl RsxTextIndex {
pub fn insert(&self, loc: &'static Location<'static>, text: String) {
self.hm.write().unwrap().insert(loc, text);
}
pub fn read(&self) -> RwLockReadGuard<HashMap<&'static Location<'static>, String>> {
self.hm.read().unwrap()
}
}
+6
View File
@@ -53,4 +53,10 @@ pub mod prelude {
#[cfg(feature = "fermi")]
pub use fermi::{use_atom_ref, use_init_atom_root, use_read, use_set, Atom, AtomRef};
#[cfg(feature = "hot_reload")]
pub use dioxus_rsx_interperter::{
captuered_context::{CapturedContext, IfmtArgs},
get_line_num, interpert_rsx, RsxTextIndex,
};
}