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.

LR9: holds released as Chrome releases them. A composition holding its frame until an <img>'s onLoad is released: the load is told on the turn after the img is made or its src changes (sdk/raea dom-load.ts), through the host's events, as Chrome fires it once the picture is in (the page holds the frame for the picture itself). A hold released from requestAnimationFrame (two rAFs after a ref lands) is released: while a delayRender hold is open, rAF's callbacks run every 16 ms with the held frame's time (frame-globals.ts), as a browser keeps rendering while a page waits; <R3F>'s own hold doesn't pump. channel-thread, logo-collapse and pulsing-border, which LR9's real holds had stopped, pass again. settle.rs's held flag is set once per turn (no dead initial value)

shinyobjectz37fc50782dd9
diff --git a/runtime/react/src/scene/settle.rs b/runtime/react/src/scene/settle.rs
index 0636d667..94c96a57 100644
--- a/runtime/react/src/scene/settle.rs
+++ b/runtime/react/src/scene/settle.rs
@@ -56,7 +56,7 @@ pub fn settle_with(app: &mut App, want: Option<i64>, secs: f64, answer: &mut cra
// the marker as last seen (frame, done), for the refusal's message
let mut marked = (-2i64, -2i64);
// the frame is held (below): its marker at the frame, not done
- let mut held = false;
+ let mut held;
// the count before the first update: a flush is in the channel before it's counted, so one that landed before this
// read is applied by the update below, and one after it wakes the wait. (A first pass that waited for a flush
// before updating lost the frame's flush whenever React was quicker than this read: the wait then ran its 0.5 s
diff --git a/sdk/raea/src/dom-load.ts b/sdk/raea/src/dom-load.ts
new file mode 100644
index 00000000..c6e2518c
--- /dev/null
+++ b/sdk/raea/src/dom-load.ts
@@ -0,0 +1,56 @@
+// An <img>'s onLoad fires (docs/layout-reads.feature LR9): a component that holds its frame until its image loads
+// (delayRender, then continueRender in onLoad, the common pattern) is released, as it is in Chrome. The page holds
+// every frame until its images are in and fails the render naming a src it can't load, so load is told on the next
+// turn after the <img> is made or its src changes, and the frame still waits for the picture itself. onError never
+// fires: an image that doesn't load fails the render instead.
+// The event goes the way the host's events go: bevy-react's loop awaits __bevyHost.op_next_event, and an elementEvent
+// for the img's id calls its handler, so it runs in React's batch like any other.
+
+type Msg = { t: string; id?: number; event?: string; payload?: unknown };
+type Op = { op: string; id?: number; kind?: string; props?: Record<string, unknown> };
+
+const HOST = (globalThis as any).__bevyHost;
+const told: Msg[] = [];
+let wake: (() => void) | null = null;
+
+/** The img creates and src changes in a batch that listen for load. */
+function sniff(json: string): void {
+ if (!json.includes('"onLoad"')) return;
+ for (const op of JSON.parse(json) as Op[]) {
+ if (op.kind !== "img" || op.id === undefined || op.props?.onLoad !== true) continue;
+ if (op.op !== "create" && !(op.op === "update" && "src" in op.props)) continue;
+ const id = op.id;
+ setTimeout(() => {
+ told.push({ t: "elementEvent", id, event: "load", payload: { type: "load" } });
+ wake?.();
+ }, 0);
+ }
+}
+
+/** Installs it on the host, once, from dom-reads. */
+export function domLoad(): void {
+ if (!HOST?.op_flush || typeof HOST.op_next_event !== "function" || HOST.__domLoad) return;
+ HOST.__domLoad = true;
+ const flush = HOST.op_flush;
+ HOST.op_flush = (json: string, devtools: boolean) => {
+ sniff(json);
+ return flush(json, devtools);
+ };
+ const nextEvent = HOST.op_next_event.bind(HOST);
+ // the host's own next message, kept across calls: one that loses the race to a load is the next call's
+ let pending: Promise<Msg | null> | null = null;
+ // (null is the host shutting down, so only the host's own answer is ever passed on as null)
+ HOST.op_next_event = async (): Promise<Msg | null> => {
+ for (;;) {
+ if (told.length) return told.shift()!;
+ const host: Promise<Msg | null> = (pending ??= nextEvent());
+ const ours = new Promise<null>((r) => (wake = () => r(null)));
+ const won = await Promise.race([host.then((m) => ({ m })), ours]);
+ wake = null;
+ if (won) {
+ pending = null;
+ return won.m;
+ }
+ }
+ };
+}
diff --git a/sdk/raea/src/frame-globals.ts b/sdk/raea/src/frame-globals.ts
index 6bcfa043..fd49d85d 100644
--- a/sdk/raea/src/frame-globals.ts
+++ b/sdk/raea/src/frame-globals.ts
@@ -10,14 +10,39 @@ type FrameCallback = (time: number) => void;
let queue = new Map<number, FrameCallback>();
let next = 1;
let ran = -1;
+/** the frame being drawn's time, for a callback run while it's held */
+let now = 0;
+let pumping = false;
+
+/** A callback asked for while the frame is held (delayRender) runs on a timer with that frame's time, as a browser's
+ * runs while a page waits: a hold released from requestAnimationFrame (two rAFs after a ref lands) would otherwise
+ * wait for a frame that can't start until it's released (layout-reads LR9). */
+function pumpHeld(): void {
+ // only a composition's own holds (delayRender's, named in `open`), not <R3F>'s while its scene loads
+ if (pumping || !((globalThis as { __raeaHold?: { open?: Map<number, string> } }).__raeaHold?.open?.size! > 0)) return;
+ pumping = true;
+ setTimeout(() => {
+ pumping = false;
+ const due = queue;
+ queue = new Map();
+ for (const cb of due.values()) {
+ try {
+ cb(now);
+ } catch (e) {
+ console.error(e);
+ }
+ }
+ }, 16);
+}
/** Frame `f` at `fps` starts: the callbacks asked for before it run, once (a second draw of the same frame runs none). */
export function frameCallbacks(f: number, fps: number): void {
+ const time = (f * 1000) / (fps > 0 ? fps : 30);
+ now = time;
if (f === ran || queue.size === 0) return;
ran = f;
const due = queue;
queue = new Map();
- const time = (f * 1000) / (fps > 0 ? fps : 30);
for (const cb of due.values()) {
try {
cb(time);
@@ -85,6 +110,7 @@ if (typeof g.requestAnimationFrame !== "function") {
g.requestAnimationFrame = (cb: FrameCallback) => {
const id = next++;
queue.set(id, cb);
+ pumpHeld();
return id;
};
g.cancelAnimationFrame = (id: number) => void queue.delete(id);