Skip to main content

topcoat_runtime/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2
3mod bind_attribute;
4mod event_handler;
5mod expr;
6#[cfg(feature = "router")]
7mod procedure;
8#[cfg(feature = "router")]
9mod reactive_scope;
10#[cfg(feature = "router")]
11mod shard;
12mod signal;
13mod surrogate;
14
15pub use bind_attribute::*;
16pub use event_handler::*;
17pub use expr::*;
18#[cfg(feature = "router")]
19pub use procedure::*;
20#[cfg(feature = "router")]
21pub use reactive_scope::*;
22#[cfg(feature = "router")]
23pub use shard::*;
24pub use signal::*;
25pub use surrogate::*;
26use topcoat_asset::{Asset, asset};
27
28pub const SCRIPT: Asset = asset!("browser/dist/index.js", rename: "topcoat");
29
30/// Macro helpers to shorten the generated source code.
31#[doc(hidden)]
32pub mod internal {
33    use topcoat_view::{HtmlContext, PartsWriter, internal::in_context};
34
35    #[inline]
36    pub fn __js(parts: &mut PartsWriter<'_>, js: impl Into<std::borrow::Cow<'static, str>>) {
37        // JavaScript source renders inside comment markers and double-quoted
38        // attributes; the comment context escapes the union of what both
39        // positions need.
40        in_context(parts, HtmlContext::Comment, |parts| {
41            match js.into() {
42                std::borrow::Cow::Borrowed(js) => parts.push_static_str(js),
43                std::borrow::Cow::Owned(js) => parts.push_string(js),
44            };
45        });
46    }
47
48    #[inline]
49    pub fn __js_unescaped(parts: &mut PartsWriter<'_>, s: &'static str) {
50        parts.push_static_str_unescaped(s);
51    }
52
53    #[inline]
54    pub fn __surrogate(parts: &mut PartsWriter<'_>, value: &(impl serde::Serialize + ?Sized)) {
55        in_context(parts, HtmlContext::Comment, |parts| {
56            parts.push_promoted_str_unescaped(&"cx.hydrate(");
57            let json = serde_json::to_string(value).expect("failed to serialize surrogate value");
58            parts.push_string(json);
59            parts.push_promoted_str_unescaped(&")");
60        });
61    }
62}