Skip to content

3D engine that AI agents drive

Follow the newest visibility entry and release the hero loop (#2634)

abu-tahir-03d8b998325d5
diff --git a/changes/2634.md b/changes/2634.md
new file mode 100644
index 00000000..c7f0af72
--- /dev/null
+++ b/changes/2634.md
@@ -0,0 +1 @@
+- The hero keeps drawing after a quick scroll out and back, and `startLoop` now returns a `stop()` that releases its observer, listener and pending frame (#2634)
diff --git a/src/hero-lab/stage.js b/src/hero-lab/stage.js
index 819c6db3..498f92cb 100644
--- a/src/hero-lab/stage.js
+++ b/src/hero-lab/stage.js
@@ -58,6 +58,7 @@ export function startLoop(stage, draw) {
let lastTimestamp = 0;
let visible = true;
let firstDrawn = false;
+ let stopped = false;
const step = timestamp => {
frameRequest = 0;
@@ -80,14 +81,25 @@ export function startLoop(stage, draw) {
};
function wake() {
- if (!frameRequest && visible && !document.hidden) frameRequest = requestAnimationFrame(step);
+ if (!stopped && !frameRequest && visible && !document.hidden) frameRequest = requestAnimationFrame(step);
}
- new IntersectionObserver(entries => {
- visible = entries[0].isIntersecting;
+ const observer = new IntersectionObserver(entries => {
+ // An observer can batch a scroll out and back in one callback, oldest first, so the newest entry is the one that counts.
+ visible = entries[entries.length - 1].isIntersecting;
wake();
- }).observe(stage);
+ });
+ observer.observe(stage);
document.addEventListener('visibilitychange', wake);
wake();
- return { wake };
+ return {
+ wake,
+ stop() {
+ stopped = true;
+ observer.disconnect();
+ document.removeEventListener('visibilitychange', wake);
+ cancelAnimationFrame(frameRequest);
+ frameRequest = 0;
+ },
+ };
}
diff --git a/tests/hero-stage-loop.test.js b/tests/hero-stage-loop.test.js
new file mode 100644
index 00000000..c15e329a
--- /dev/null
+++ b/tests/hero-stage-loop.test.js
@@ -0,0 +1,65 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+
+const listeners = new Set();
+const frames = new Map();
+const observers = [];
+let nextFrame = 0;
+let scheduled = 0;
+
+globalThis.window = { matchMedia: () => ({ matches: false }) };
+globalThis.document = {
+ hidden: false,
+ addEventListener: (type, listener) => { if (type === 'visibilitychange') listeners.add(listener); },
+ removeEventListener: (type, listener) => listeners.delete(listener),
+};
+globalThis.requestAnimationFrame = callback => {
+ scheduled++;
+ const id = ++nextFrame;
+ frames.set(id, callback);
+ return id;
+};
+globalThis.cancelAnimationFrame = id => frames.delete(id);
+globalThis.IntersectionObserver = class {
+ constructor(callback) {
+ this.callback = callback;
+ this.disconnected = false;
+ observers.push(this);
+ }
+ observe() {}
+ disconnect() { this.disconnected = true; }
+};
+
+const { startLoop } = await import('../src/hero-lab/stage.js');
+
+function flushFrame(timestamp = 16) {
+ // One animation frame runs every callback scheduled before it; a callback that schedules another frame waits for the next.
+ const pending = [...frames.values()];
+ frames.clear();
+ for (const callback of pending) callback(timestamp);
+}
+
+test('a stage that scrolled out and back in keeps drawing (#2634)', () => {
+ let draws = 0;
+ const loop = startLoop({ dataset: {} }, () => { draws++; return true; });
+ flushFrame();
+ assert.equal(draws, 1);
+ observers.at(-1).callback([{ isIntersecting: false }, { isIntersecting: true }]);
+ flushFrame(32);
+ assert.equal(draws, 2, 'the newest entry says the stage is visible');
+ loop.stop();
+});
+
+test('stop releases the observer, the listener and the frame, and wake schedules nothing after (#2634)', () => {
+ const loop = startLoop({ dataset: {} }, () => true);
+ assert.equal(frames.size, 1);
+ assert.equal(listeners.size, 1);
+ const observer = observers.at(-1);
+ const scheduledBefore = scheduled;
+ loop.stop();
+ assert.equal(observer.disconnected, true);
+ assert.equal(listeners.size, 0);
+ assert.equal(frames.size, 0);
+ loop.wake();
+ assert.equal(scheduled, scheduledBefore, 'wake schedules no frame after stop');
+});