fix(js): expose runtime API before startup
Assign window.hemx before startup side effects so constrained browser contexts can still inspect the runtime and root state even if bootstrap work reports errors. req: runtime/002
This commit is contained in:
+11
-2
@@ -791,11 +791,20 @@
|
|||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
roots().forEach((root) => {
|
roots().forEach((root) => {
|
||||||
|
try {
|
||||||
bootstrapState(root);
|
bootstrapState(root);
|
||||||
bindRoot(root);
|
bindRoot(root);
|
||||||
bindSse(root);
|
bindSse(root);
|
||||||
|
} catch (error) {
|
||||||
|
emit(root, "hemx:error", String(error));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
try {
|
||||||
history.replaceState(history.state || { hemx: true }, "", location.href);
|
history.replaceState(history.state || { hemx: true }, "", location.href);
|
||||||
|
} catch (error) {
|
||||||
|
const root = roots()[0];
|
||||||
|
if (root) emit(root, "hemx:history-error", String(error));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addEventListener("popstate", () => {
|
addEventListener("popstate", () => {
|
||||||
@@ -803,8 +812,8 @@
|
|||||||
if (root) navigateUrl(location.href, root, "none").catch((error) => emit(root, "hemx:error", String(error)));
|
if (root) navigateUrl(location.href, root, "none").catch((error) => emit(root, "hemx:error", String(error)));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
window.hemx = Object.freeze({ runtimeAbiVersion, roots, rootOf, applyHtml, applyBatch, decodeBatch, atomValue, decodeAtomState });
|
||||||
|
|
||||||
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
|
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
|
||||||
else start();
|
else start();
|
||||||
|
|
||||||
window.hemx = Object.freeze({ runtimeAbiVersion, roots, rootOf, applyHtml, applyBatch, decodeBatch, atomValue, decodeAtomState });
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -1,3 +1,19 @@
|
|||||||
|
#[test]
|
||||||
|
fn runtime_exposes_debug_api_before_startup_side_effects() {
|
||||||
|
// req: runtime/002
|
||||||
|
let source = hemx_js::RUNTIME_JS;
|
||||||
|
|
||||||
|
let api = source
|
||||||
|
.find("window.hemx = Object.freeze")
|
||||||
|
.expect("runtime exposes browser API");
|
||||||
|
let start = source
|
||||||
|
.find("if (document.readyState === \"loading\") document.addEventListener(\"DOMContentLoaded\", start)")
|
||||||
|
.expect("runtime starts after declaration");
|
||||||
|
assert!(api < start);
|
||||||
|
assert!(source.contains("try {\n bootstrapState(root);"));
|
||||||
|
assert!(source.contains("emit(root, \"hemx:error\", String(error));"));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn runtime_posts_urlencoded_forms_by_default() {
|
fn runtime_posts_urlencoded_forms_by_default() {
|
||||||
let source = hemx_js::RUNTIME_JS;
|
let source = hemx_js::RUNTIME_JS;
|
||||||
|
|||||||
Reference in New Issue
Block a user