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.

The desktop app keeps its localStorage across launches, and a page reads it before its code runs (Shane, 11-09: the open workspace and Getting started's Hide were lost on every quit).

shinyobjectzebd147a076e5
diff --git a/io/net/calls/src/lib.rs b/io/net/calls/src/lib.rs
index 96e8ae28..40511daf 100644
--- a/io/net/calls/src/lib.rs
+++ b/io/net/calls/src/lib.rs
@@ -11,6 +11,7 @@
//! net.ws.close {id, code, reason}
//! net.storage.get {area} -> {k: v} localStorage kept across runs, in $BEVY_TSX_STORAGE/<area>.json; without
//! net.storage.set {area, change} that variable these are refused and storage stays in the page's memory
+//! (net.storage.get is a synchronous call too: storage_get, below)
//!
//! fetch and the sockets (raea-io-net's http and ws) block on threads of their own, never the caller's frame.
@@ -90,6 +91,13 @@ impl<K: Copy + Eq + Hash + Send + 'static> Net<K> {
const NO_STORE: &str = "no storage (BEVY_TSX_STORAGE isn't set)";
+/// localStorage's kept values for `{area}`, as `net.storage.get` answers them, for a host to register as a synchronous
+/// call too: sdk/ui's storage.ts reads them before the page's own code runs, as a browser has them (raea-runtime-js
+/// net.rs). Read only at load, so a page that reads its settings then saves them doesn't save its defaults over them.
+pub fn storage_get(args: &Value) -> Result<Value, String> {
+ get(args)
+}
+
fn get(args: &Value) -> Result<Value, String> {
let p = store(args).ok_or(NO_STORE)?;
Ok(std::fs::read_to_string(p).ok().and_then(|t| serde_json::from_str(&t).ok()).unwrap_or(json!({})))
diff --git a/runtime/js/src/net.rs b/runtime/js/src/net.rs
index bbea8a45..f7f7b4f9 100644
--- a/runtime/js/src/net.rs
+++ b/runtime/js/src/net.rs
@@ -28,4 +28,25 @@ pub fn install(calls: &mut HostCalls, push: Sender<(Entity, Value)>) {
}));
});
}
+ // localStorage's kept values, synchronously too: sdk/ui's storage.ts has them before the page's code reads them
+ crate::calls::on_sync("net.storage.get", raea_io_net_calls::storage_get);
+}
+
+#[cfg(test)]
+mod tests {
+ use serde_json::Value;
+
+ /// A page reads what an earlier run kept at once (raea.app's open workspace and dismissed hints, 11-09).
+ #[test]
+ fn kept_storage_answers_synchronously() {
+ let dir = std::env::temp_dir().join(format!("raea-js-storage-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ std::fs::write(dir.join("local.json"), r#"{"raea.settings":"{\"workspace\":\"raea\"}"}"#).unwrap();
+ // SAFETY: nothing else in this crate's tests reads BEVY_TSX_STORAGE
+ unsafe { std::env::set_var("BEVY_TSX_STORAGE", &dir) };
+ let _calls = crate::calls::HostCalls::default();
+ let got: Value = serde_json::from_str(&crate::calls::sync("net.storage.get", r#"{"area":"local"}"#)).unwrap();
+ let _ = std::fs::remove_dir_all(&dir);
+ assert_eq!(got["ok"]["raea.settings"], r#"{"workspace":"raea"}"#, "{got}");
+ }
}
diff --git a/sdk/ui/src/platform/storage.ts b/sdk/ui/src/platform/storage.ts
index c4e32d2f..36aac5c8 100644
--- a/sdk/ui/src/platform/storage.ts
+++ b/sdk/ui/src/platform/storage.ts
@@ -1,5 +1,5 @@
// Storage, location and history (in memory).
-import { call } from "../wire";
+import { call, callSync } from "../wire";
import { DOMEx, URL_, event, g, named, put, structuredClone_, tag } from "./all";
// ---------------------------------------------------------------- Storage
@@ -77,12 +77,24 @@ put("localStorage", new Storage_(local, "local"));
put("sessionStorage", new Storage_(new Map(), null));
-// what an earlier run kept, as soon as the host answers (a bundle reading it at load sees what's been set since)
-call<Record<string, string>>("net.storage.get", { area: "local" })
- .then((kept) => {
- for (const k in kept ?? {}) if (!local.has(k)) local.set(k, kept[k]);
- })
- .catch(() => {});
+// what an earlier run kept, there before the page's own code reads it, as a browser's is: at once from a host that
+// answers net.storage.get synchronously (raea-runtime-js's net.rs). An app reading its settings at load would otherwise
+// see none, then save its defaults over what was kept (raea.app's workspace and dismissed hints, 11-09). A host
+// without the synchronous call answers later, and a bundle reading at load sees only what's been set since.
+const keep = (kept: Record<string, string> | null | undefined) => {
+ for (const k in kept ?? {}) if (!local.has(k)) local.set(k, String(kept![k]));
+};
+let kept: Record<string, string> | undefined;
+try {
+ kept = callSync<Record<string, string>>("net.storage.get", { area: "local" });
+} catch {
+ // no synchronous storage here, or nothing kept (no BEVY_TSX_STORAGE): the async answer below
+}
+if (kept !== undefined) keep(kept);
+else
+ call<Record<string, string>>("net.storage.get", { area: "local" })
+ .then(keep)
+ .catch(() => {});
// ---------------------------------------------------------------- location and history (in memory)