Skip to content

Commit

Permalink
feat: complete the component update case
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Mar 17, 2024
1 parent 8a5651b commit 50ab6f2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
28 changes: 25 additions & 3 deletions rustle/src/compiler/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Code {
components: Vec<(String, String)>,
create: Vec<String>,
update: Vec<String>,
props_set: Vec<String>,
destroy: Vec<String>,
}

Expand All @@ -22,6 +23,7 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String {
components: Vec::new(),
create: Vec::new(),
update: Vec::new(),
props_set: Vec::new(),
destroy: Vec::new(),
};

Expand Down Expand Up @@ -65,6 +67,9 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String {
update(changed) {{
{}
}},
$set(changedSet) {{
{}
}},
destroy() {{
{}
}},
Expand All @@ -86,6 +91,7 @@ pub fn generate(ast: RustleAst, analysis: AnalysisResult) -> String {
.join("\n"),
code.create.join("\n"),
code.update.join("\n"),
code.props_set.join("\n"),
code.destroy.join("\n")
)
}
Expand Down Expand Up @@ -176,11 +182,11 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m
r#"
const {}_changes = {{}};
if (changed.includes('{}')) {{
{}_changes.{} = {};
{}_changes['{}'] = {};
}}
variable_name.$set({}_changes);
{}.$set({}_changes);
"#,
variable_name, value, variable_name, attr.name, value, variable_name
variable_name, value, variable_name, attr.name, value, variable_name, variable_name
));
},
_ => {
Expand Down Expand Up @@ -246,6 +252,14 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m
"#,
expression_name, variable_name, expression_name
));
code.props_set.push(format!(
r#"
if ('{}' in changedSet) {{
{}.data = changedSet['{}'];
}}
"#,
expression_name, variable_name, expression_name
));
} else {
for change in analysis.will_change.iter() {
if expression_name.contains(change) {
Expand All @@ -257,6 +271,14 @@ fn traverse(node: &Fragment, parent: String, analysis: &AnalysisResult, code: &m
"#,
change, variable_name, expression_name
));
code.props_set.push(format!(
r#"
if ('{}' in changedSet) {{
{}.data = changedSet['{}'];
}}
"#,
expression_name, variable_name, expression_name
));
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions rustle/tests/component_update/Nested.rustle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<script>
export let q;
let q=0;
const minus_q = () => q--;
</script>

<p>...don't affect this element {q}</p>
<div>
<button on:click={minus_q}>-</button>
<p>...don't affect this element q={q}</p>
</div>
11 changes: 6 additions & 5 deletions rustle/tests/component_update/app.rustle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script>
let url = '/tutorial/image.gif';
const update_url = () => url--;
import Nested from './Nested.js';

const Nested = require('./Nested.rustle');
<script>
let out_q = 108;
const handleClick = () => out_q++
</script>

<Nested q={url} />
<button on:click={handleClick}>+</button>
<Nested q={out_q} />
15 changes: 15 additions & 0 deletions rustle/tests/component_update/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script type="module">
import App from "./app.js";
App().create(document.body);
</script>
</body>
</html>
12 changes: 11 additions & 1 deletion rustle/tests/test_parsing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,15 @@ fn test_parsing_nested() {
#[test]
fn test_tag_update() { test_parsing("tag_update".to_owned()) }

// in browser passed
#[test]
fn test_component_update() { test_parsing("component_update".to_owned()) }
fn test_component_update() {
test_parsing("component_update".to_owned());

let source = fs::read_to_string("tests/component_update/Nested.rustle").unwrap();
let ast = Parser::new(&source).parse();
let analysis = analyse(&ast);
let generated = generate(ast, analysis);

fs::write("tests/component_update/Nested.js", generated).unwrap();
}

0 comments on commit 50ab6f2

Please sign in to comment.