Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

H12: a held JS clock and an app screen's bundle

shinyobjectz4f354e97fcc8
diff --git a/runtime/bundle/lib/src/app.rs b/runtime/bundle/lib/src/app.rs
index f9d116e7..2a44fbc7 100644
--- a/runtime/bundle/lib/src/app.rs
+++ b/runtime/bundle/lib/src/app.rs
@@ -5,10 +5,10 @@
use std::path::Path;
/// The entry every app is mounted by: the DOM installed before the app's module runs, its default export mounted at
-/// the root (an app that mounts itself with createRoot needs no default export)
-fn entry(app: &Path) -> String {
+/// the root with `props` (JS; an app's none) (an app that mounts itself with createRoot needs no default export)
+fn entry(app: &Path, props: &str) -> String {
format!(
- "import \"@raea/ui/dom-globals\";\nimport {{ createElement }} from \"react\";\nimport {{ mount }} from \"@raea/ui/react\";\nimport * as M from {:?};\nconst App = (M as any).default;\nif (typeof App === \"function\") mount(createElement(App), 0, {{ dom: true }});\n",
+ "import \"@raea/ui/dom-globals\";\nimport {{ createElement }} from \"react\";\nimport {{ mount }} from \"@raea/ui/react\";\nimport * as M from {:?};\nconst App = (M as any).default;\nif (typeof App === \"function\") mount(createElement(App, {props}), 0, {{ dom: true }});\n",
app.display().to_string()
)
}
@@ -38,6 +38,16 @@ fn ui(home: &Path) -> serde_json::Value {
/// `app` bundled against `home`: its script and the CSS its modules import
pub fn bundle(app: &Path, home: &Path) -> Result<(String, String), String> {
+ bundle_with(app, home, "null")
+}
+
+/// An app screen's module (`<Screen app="kit.tsx">`, docs/blitz-bevy.feature H12) bundled as an app is: its default
+/// export given `{width, height}`, the screen's size (its document's, so one bundle serves a screen of any size)
+pub fn bundle_screen(app: &Path, home: &Path) -> Result<(String, String), String> {
+ bundle_with(app, home, "{ width: innerWidth, height: innerHeight }")
+}
+
+fn bundle_with(app: &Path, home: &Path, props: &str) -> Result<(String, String), String> {
// canonical, and on Windows without canonicalize()'s verbatim \\?\ prefix: Rolldown can't resolve an entry that
// imports a path written with it (T1f: the Windows release check, 11-07)
let app = raea_io_home::plain(app.canonicalize().map_err(|e| format!("{}: {e}", app.display()))?);
@@ -49,7 +59,7 @@ pub fn bundle(app: &Path, home: &Path) -> Result<(String, String), String> {
let _ = std::fs::remove_dir_all(&work);
std::fs::create_dir_all(&work).map_err(|e| format!("{}: {e}", work.display()))?;
let entry_file = work.join("entry.tsx");
- let wrote = std::fs::write(&entry_file, entry(&app)).and_then(|_| std::fs::write(work.join("tsconfig.json"), tsconfig(home)));
+ let wrote = std::fs::write(&entry_file, entry(&app, props)).and_then(|_| std::fs::write(work.join("tsconfig.json"), tsconfig(home)));
let r = wrote.map_err(|e| e.to_string()).and_then(|_| {
let opts = serde_json::json!({
"script": {"js": home.join("sdk/react/js"), "motion": home.join("sdk/raea/src"), "shell": home.join("sdk/shell/src/index.ts"), "aliases": ui(home), "stubs": []},
diff --git a/runtime/bundle/src/fresh.rs b/runtime/bundle/src/fresh.rs
index 8fe4c109..59b320a8 100644
--- a/runtime/bundle/src/fresh.rs
+++ b/runtime/bundle/src/fresh.rs
@@ -25,28 +25,6 @@ fn spec(p: &Path) -> String {
dunce::simplified(p).display().to_string()
}
-/// A react-dom page's bundle (a surface): the module at `src` (its default
-/// export a React component) rendered by react-dom into #root with the page's width and height, again on each resize,
-/// bundled with its packages (the project's node_modules, raea-io-npm's) into `out`. The bundle's text. A legacy-app panel's
-/// and a `<Screen app>`'s.
-pub fn bundle_surface(src: &Path, out: &Path) -> R<String> {
- let entry = format!(
- r#"// raea writes this (motion.rs bundle_surface): {src} on react-dom, at the surface's size, again on each resize.
-import {{ createElement }} from "react";
-import {{ createRoot }} from "react-dom/client";
-import Page from {src:?};
-
-const root = createRoot(document.getElementById("root")!);
-const draw = () => root.render(createElement(Page as any, {{ width: innerWidth, height: innerHeight }}));
-addEventListener("resize", draw);
-draw();
-"#,
- src = spec(src)
- );
- bundle_entry(&entry, out, &[])?;
- std::fs::read_to_string(out).map_err(|e| format!("{}: {e}", out.display()))
-}
-
/// `entry` (TypeScript source) bundled into `out` by raea-bundle's library (engine/bundle/src/script.rs's resolution); `extra`
/// are bundle.ts's flags (--define K=V, --alias SPEC=PATH, --node-stub a,b).
pub fn bundle_entry(entry: &str, out: &Path, extra: &[&str]) -> R<()> {
diff --git a/runtime/jsengine/src/lib.rs b/runtime/jsengine/src/lib.rs
index ce74bf36..94bfc11d 100644
--- a/runtime/jsengine/src/lib.rs
+++ b/runtime/jsengine/src/lib.rs
@@ -36,11 +36,21 @@ pub type Native = Box<dyn Fn(&[String]) -> Result<Ret, String>>;
thread_local! {
static T0: std::time::Instant = std::time::Instant::now();
+ static HELD: std::cell::Cell<Option<f64>> = const { std::cell::Cell::new(None) };
}
-/// ms since this thread first asked: the clock a host's JS timers run on.
+/// ms since this thread first asked: the clock a host's JS timers run on (or the time `at_clock` holds it at).
pub fn now_ms() -> f64 {
- T0.with(|t| t.elapsed().as_secs_f64() * 1e3)
+ HELD.with(|h| h.get()).unwrap_or_else(|| T0.with(|t| t.elapsed().as_secs_f64() * 1e3))
+}
+
+/// `f` run with this thread's JS clock held at `ms`: a root stepped on a composition's frames (an app screen, frame
+/// over fps) sees the same timers and performance.now in a render and its play, so the render repeats.
+pub fn at_clock<T>(ms: f64, f: impl FnOnce() -> T) -> T {
+ let was = HELD.with(|h| h.replace(Some(ms)));
+ let out = f();
+ HELD.with(|h| h.set(was));
+ out
}
/// A native answering `now_ms()`, a host's `now` for its JS.