Skip to content

3D engine that AI agents drive

Join the press in flight instead of starting the hero world twice (#2635)

abu-tahir-0e3199694adcc
diff --git a/changes/2635.md b/changes/2635.md
new file mode 100644
index 00000000..2c3e86b5
--- /dev/null
+++ b/changes/2635.md
@@ -0,0 +1 @@
+- A second press on "Play live 3D" while the world loads joins the first press instead of starting the world again (#2635)
diff --git a/src/hero-lab/loop/play-on-demand.js b/src/hero-lab/loop/play-on-demand.js
index c4d80ba3..e75e8175 100644
--- a/src/hero-lab/loop/play-on-demand.js
+++ b/src/hero-lab/loop/play-on-demand.js
@@ -11,6 +11,7 @@ const LOADING_LABEL = 'Loading 3D';
export function createPlayOnDemand({ playButton, beginLive, showError, focusTarget, onLive, wait = setTimeout }) {
const label = playButton.querySelector('.play-label');
+ let inFlight = null;
function showLoading() {
playButton.disabled = true;
@@ -24,7 +25,7 @@ export function createPlayOnDemand({ playButton, beginLive, showError, focusTarg
label.textContent = PLAY_LABEL;
}
- return async function playOnDemand() {
+ async function press() {
showLoading();
try {
await beginLive();
@@ -37,5 +38,11 @@ export function createPlayOnDemand({ playButton, beginLive, showError, focusTarg
focusTarget?.focus({ preventScroll: true });
wait(onLive, PLAY_HANDOFF_MS);
return true;
+ }
+
+ // A second press while the world loads joins the first one instead of starting the world again.
+ return function playOnDemand() {
+ inFlight ??= press().finally(() => { inFlight = null; });
+ return inFlight;
};
}
diff --git a/tests/hero-prefetch.test.js b/tests/hero-prefetch.test.js
index a6af861a..a9df0ada 100644
--- a/tests/hero-prefetch.test.js
+++ b/tests/hero-prefetch.test.js
@@ -163,3 +163,21 @@ test('a failed start shows the error and puts the button back, and the next pres
assert.equal(button.hidden, true);
assert.equal(events.at(-1), 'focus {"preventScroll":true}');
});
+
+test('a second press while the world loads joins the first and starts nothing again (#2635)', async () => {
+ let attempts = 0;
+ let finish;
+ const { events, handoffs, play } = playHarness(() => {
+ attempts++;
+ return new Promise(resolve => { finish = resolve; });
+ });
+ const first = play();
+ const second = play();
+ assert.equal(attempts, 1, 'beginLive runs once');
+ assert.equal(first, second, 'both presses get the same promise');
+ finish();
+ assert.equal(await first, true);
+ assert.equal(await second, true);
+ assert.equal(events.filter(event => event.startsWith('focus')).length, 1);
+ assert.equal(handoffs.length, 1, 'the live layout is scheduled once');
+});