Skip to main content

glory_web/
lib.rs

1//! Browser-only entry points are target gated; enabling web never changes core.
2#[cfg(target_arch = "wasm32")]
3mod browser;
4#[cfg(any(target_arch = "wasm32", test))]
5mod clipboard_state;
6#[cfg(any(target_arch = "wasm32", test))]
7mod file_reads;
8#[cfg(all(test, not(target_arch = "wasm32")))]
9#[path = "browser/plan_fields.rs"]
10mod plan_fields_tests;
11#[cfg(all(test, not(target_arch = "wasm32")))]
12#[path = "browser/plan_parts.rs"]
13mod plan_parts_tests;
14#[cfg(any(target_arch = "wasm32", test))]
15mod semantic_changes;
16/// Experimental direct HTML template/slot runtime (limited non-editor profile).
17#[cfg(target_arch = "wasm32")]
18pub mod template;
19#[cfg(all(test, not(target_arch = "wasm32")))]
20#[path = "browser/templates.rs"]
21mod template_tests;
22pub mod view;
23#[cfg(target_arch = "wasm32")]
24pub use browser::*;
25#[cfg(feature = "stream")]
26pub const STREAM_SCRIPT: &str = include_str!("stream.js");
27
28/// Read a snapshot without modifying/consuming the data block. A failed
29/// hydration can create a fresh Resources context from the same payload.
30#[cfg(all(target_arch = "wasm32", feature = "resources"))]
31pub fn read_resources(
32    element: &web_sys::Element,
33    limits: glory_resource::Limits,
34) -> Result<glory_resource::Snapshot, glory_resource::Error> {
35    use glory_resource::{Error, Snapshot, VERSION};
36    if element.namespace_uri().as_deref() != Some("http://www.w3.org/1999/xhtml")
37        || element.local_name() != "script"
38        || element.get_attribute("type").as_deref() != Some("application/json")
39    {
40        return Err(Error::Payload(
41            "expected an application/json script element",
42        ));
43    }
44    if element.get_attribute("data-gly-resources").as_deref() != Some(VERSION.to_string().as_str())
45    {
46        return Err(Error::Payload("resource data block version"));
47    }
48    let json = element
49        .text_content()
50        .ok_or(Error::Payload("resource data block has no text"))?;
51    Snapshot::from_json(&json, limits)
52}
53
54/// Restore explicit resources and adopt the same initial factory. Failure
55/// preserves the payload for a fresh attempt; success removes its DOM data
56/// block so large serialized values are not retained alongside live state.
57#[cfg(all(target_arch = "wasm32", feature = "resources", feature = "hydrate"))]
58pub fn hydrate_resources_with<W: glory_ui::Widget>(
59    root: web_sys::Element,
60    data: &web_sys::Element,
61    limits: glory_resource::Limits,
62    factory: impl FnOnce(&glory_ui::Scope, &glory_resource::Resources) -> W,
63) -> Result<(WebApp, glory_resource::Resources), HydrationError> {
64    if root.contains(Some(data.as_ref())) {
65        return Err(HydrationError::Resources(glory_resource::Error::Payload(
66            "resource block must be outside the mount",
67        )));
68    }
69    let snapshot = read_resources(data, limits).map_err(HydrationError::Resources)?;
70    let resources =
71        glory_resource::Resources::restore(snapshot).map_err(HydrationError::Resources)?;
72    let app = hydrate_with(root, |scope| factory(scope, &resources))?;
73    data.remove();
74    Ok((app, resources))
75}