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

Set ssr true at the start #1437

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions fastn-js/js/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2198,6 +2198,7 @@ class Node2 {
};
}
addEventHandler(event, func) {
found_external_js = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are using camelCase of snake_case? Rename this variable to camelCase.

if (event === fastn_dom.Event.Click) {
let onclickEvents = this.mergeFnCalls(this.#node.onclick, func);
if (fastn_utils.isNull(this.#node.onclick)) this.attachCss("cursor", "pointer");
Expand Down
6 changes: 6 additions & 0 deletions fastn-js/js/fastn.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ fastn.closureWithoutExecute = function (func) {
}

fastn.formula = function (deps, func) {
found_external_js = false;

let closure = fastn.closure(func);
let mutable = new Mutable(closure.get());
for (let idx in deps) {
Expand All @@ -297,6 +299,10 @@ fastn.formula = function (deps, func) {
}));
}

if (found_external_js) {
mutables_to_execute_after_hydrate.push([mutable, closure]);
}

return mutable;
}

Expand Down
12 changes: 10 additions & 2 deletions fastn-js/js/virtual.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
let fastn_virtual = {}

let id_counter = 0;
let hydrating = false;
let hydrating = true;
let ssr = false;
let rerender = false;
let found_external_js = false;
let mutables_to_execute_after_hydrate = [];

class ClassList {
#classes = [];
Expand Down Expand Up @@ -153,7 +155,8 @@ fastn_virtual.hydrate = function(main) {
let current_device = ftd.get_device();
let found_device = ftd.device.get();
if (current_device !== found_device) {
rerender = true
hydrating = false;
rerender = true;
ftd.device = fastn.mutable(current_device);
let styles = document.getElementById("styles");
styles.innerText = "";
Expand All @@ -176,6 +179,11 @@ fastn_virtual.hydrate = function(main) {
main(body);
id_counter = 0;
hydrating = false;
for (mutable_with_closure of mutables_to_execute_after_hydrate) {
let [mutable, closure] = mutable_with_closure;
closure.update();
mutable.set(closure.get());
}
}

fastn_virtual.ssr = function(main) {
Expand Down
6 changes: 3 additions & 3 deletions fastn-js/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use to_js::to_js;
pub use udf::{udf_with_arguments, UDF};
pub use udf_statement::UDFStatement;

pub fn all_js_without_test_and_ftd_langugage_js() -> String {
pub fn all_js_without_test_and_ftd_language_js() -> String {
let markdown_js = fastn_js::markdown_js();
let fastn_js = include_str!("../js/fastn.js");
let dom_js = include_str!("../js/dom.js");
Expand All @@ -54,14 +54,14 @@ pub fn all_js_without_test_and_ftd_langugage_js() -> String {
}

pub fn all_js_without_test() -> String {
let fastn_js = all_js_without_test_and_ftd_langugage_js();
let fastn_js = all_js_without_test_and_ftd_language_js();
let ftd_language_js = include_str!("../js/ftd-language.js");
format!("{ftd_language_js}{fastn_js}\nwindow.ftd = ftd;\n")
}

pub fn all_js_with_test() -> String {
let test_js = include_str!("../js/test.js");
let all_js = all_js_without_test_and_ftd_langugage_js();
let all_js = all_js_without_test_and_ftd_language_js();
format!("{all_js}{test_js}")
}

Expand Down
5 changes: 2 additions & 3 deletions fastn-js/src/ssr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ pub fn ssr(ast: &[fastn_js::Ast]) -> String {
parenti0.setProperty(fastn_dom.PropertyKind.Height, fastn_dom.Resizing.FillContainer, inherited);
main(parenti0);
}};
fastn_virtual.ssr(main_wrapper);", fastn_js::to_js(ast,
"foo"));
fastn_virtual.ssr(main_wrapper);", fastn_js::to_js(ast,"foo"));
ssr_str(&js)
}

pub fn ssr_with_js_string(package_name: &str, js: &str) -> String {
let js = format!("
let __fastn_package_name__ = \"{}\";\n{}
let __fastn_package_name__ = \"{}\";\nssr = true; hydrating = false;\n{}
let main_wrapper = function(parent) {{
let parenti0 = fastn_dom.createKernel(parent, fastn_dom.ElementKind.Column);
parenti0.setProperty(fastn_dom.PropertyKind.Width, fastn_dom.Resizing.FillContainer, inherited);
Expand Down
16 changes: 11 additions & 5 deletions fastn-js/src/to_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,19 +467,25 @@ fn func(
"__fastn_package_name__ = \"{}\";",
package_name
)))
.append(if add_catch_statement {
text("found_external_js = true;").append(pretty::RcDoc::softline_())
} else {
pretty::RcDoc::nil()
})
.append(pretty::RcDoc::softline_())
.append(text("try {"))
.append(pretty::RcDoc::softline_())
.append(if add_catch_statement {
text("if (!ssr && !hydrating) {").append(pretty::RcDoc::softline_())
} else {
pretty::RcDoc::nil()
})
.append(body.nest(4))
.append(pretty::RcDoc::softline_())
.append(text(
format!(
"}} {} finally {{ __fastn_package_name__ = __fastn_super_package_name__;}}",
if add_catch_statement {
"catch (e) {if(!ssr){throw e;}}"
} else {
""
}
if add_catch_statement { "}" } else { "" }
)
.as_str(),
))
Expand Down
2 changes: 2 additions & 0 deletions ftd/src/js/ftd_test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ fn p(s: &str, t: &str, fix: bool, manual: bool, script: bool, file_location: &st
<script>
{dummy_package_data}
{all_js}
ssr = true;
hydrating = false;
{js_ftd_script}
{js_document_script}
fastn_virtual.ssr(main);
Expand Down
4 changes: 4 additions & 0 deletions ftd/t/assets/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ function show(a) {
function greeting(){
return "Namaste!";
}

function number() {
return 12;
}
33 changes: 15 additions & 18 deletions ftd/t/js/63-external-js.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions ftd/t/js/70-external-js.ftd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- ftd.text: $greetingfn()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

greeting_fn()


-- integer num: $numberfn()

-- ftd.integer: $num

-- ftd.text: Hello if >= 10
js: ../../t/assets/test.js
if: { num > 10 }

-- ftd.text: Hello



-- string greetingfn():
js: ../../t/assets/test.js

greeting()


-- integer numberfn():
js: ../../t/assets/test.js

number()
129 changes: 129 additions & 0 deletions ftd/t/js/70-external-js.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading