Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(club): stop the page freezing when the 21st tip lands in the live feed

nirholas4a339f567575
diff --git a/pages/club.html b/pages/club.html
index fcb28fdc2..469b13ca6 100644
--- a/pages/club.html
+++ b/pages/club.html
@@ -2287,23 +2287,35 @@
let viewerCount = 1;
let viewerSessionId = Math.random().toString(36).slice(2, 10);
+ // One in-flight beat at a time, each bounded to 10s: a slow backend must
+ // not stack overlapping presence POSTs. Paused while the tab is hidden
+ // (a backgrounded tab is not a viewer), with a fresh beat on return.
+ let heartbeatInflight = false;
async function heartbeat() {
+ if (heartbeatInflight || document.hidden) return;
+ heartbeatInflight = true;
try {
const res = await fetch('/api/club/presence', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ session: viewerSessionId }),
+ signal: AbortSignal.timeout(10000),
});
if (res.ok) {
const data = await res.json();
viewerCount = data.count || 1;
if (viewerEl) viewerEl.textContent = String(viewerCount);
}
- } catch {}
+ } catch {} finally {
+ heartbeatInflight = false;
+ }
}
heartbeat();
setInterval(heartbeat, 15000);
+ document.addEventListener('visibilitychange', () => {
+ if (!document.hidden) heartbeat();
+ });
if (reactCanvas) {
document.getElementById('club-reactions')?.addEventListener('click', (e) => {
@@ -2317,10 +2329,15 @@
el.className = 'club-float-emoji';
el.textContent = emoji;
el.style.left = (30 + Math.random() * 40) + '%';
- el.style.animationDuration = (2 + Math.random()) + 's';
+ const durationSec = 2 + Math.random();
+ el.style.animationDuration = durationSec + 's';
el.style.animationDelay = (i * 0.15) + 's';
reactCanvas.appendChild(el);
- el.addEventListener('animationend', () => el.remove());
+ el.addEventListener('animationend', () => el.remove(), { once: true });
+ // Suspended animations (hidden tab, reduced motion) never fire
+ // animationend; without a wall-clock fallback a reaction spam
+ // session grows the DOM without bound.
+ setTimeout(() => el.remove(), (durationSec + 1) * 1000);
}
});
}
diff --git a/src/club-entrance.js b/src/club-entrance.js
index cbcd7849f..64da9ea11 100644
--- a/src/club-entrance.js
+++ b/src/club-entrance.js
@@ -52,7 +52,7 @@ import {
SMAAEffect,
ToneMappingMode,
} from 'postprocessing';
-import { gltfLoader } from './loaders/gltf.js';
+import { gltfLoader, disposeGltfLoader } from './loaders/gltf.js';
import { AnimationManager } from './animation-manager.js';
import { ClubCrowd } from './club-crowd.js';
import { detectProfile, PROFILES, createFrameWatchdog } from './club-perf.js';
@@ -1113,6 +1113,13 @@ async function start(canvasEl) {
disposeObject(scene);
composer.dispose();
renderer.dispose();
+ // dispose() alone does not release the WebGL context; it lingers until GC,
+ // holding its framebuffers. The club stage runs its own context for the
+ // rest of the session, and mobile Safari budgets ~8-16 live contexts, so
+ // force the loss now. The loader's DRACO/KTX2 worker pools are keyed to
+ // this renderer and die with it.
+ try { renderer.forceContextLoss(); } catch {}
+ try { disposeGltfLoader(renderer); } catch {}
try { canvasEl.remove(); } catch {}
showHint(false); showJoystick(false); showPrompt(false); showJourney(false); showMinimap(false); showAgentSwitch(false);
}
diff --git a/src/club.js b/src/club.js
index 2b53c7cae..81fa1d9cd 100644
--- a/src/club.js
+++ b/src/club.js
@@ -220,13 +220,19 @@ function renderTipRow(rowLike, { live = false, prepend = true } = {}) {
tipFeedEl.appendChild(row);
}
- // Max 20 visible entries; oldest fade out.
- while (tipFeedEl.children.length > 20) {
- const oldest = tipFeedEl.lastElementChild;
- if (oldest) {
- oldest.classList.add('is-fading');
- oldest.addEventListener('animationend', () => oldest.remove(), { once: true });
- }
+ // Max 20 visible entries; the overflow fades out (tipFadeOut, 0.3s). Trim
+ // decisions must never depend on the animation actually running: fading rows
+ // still count as children until they detach, and CSS animations are suspended
+ // in hidden tabs and under prefers-reduced-motion. So walk the overflow once
+ // per render (no loop over a length that only shrinks asynchronously), mark
+ // each row exactly once, and back the animationend removal with a wall-clock
+ // timeout so a suspended animation can never strand rows in the DOM.
+ for (const el of Array.from(tipFeedEl.children).slice(20)) {
+ if (el.dataset.fading) continue;
+ el.dataset.fading = '1';
+ el.classList.add('is-fading');
+ el.addEventListener('animationend', () => el.remove(), { once: true });
+ setTimeout(() => el.remove(), 600);
}
// If this tip is for the currently-viewed pole, flash its spotlight + card.
@@ -312,6 +318,7 @@ function subscribeTipStream() {
let es = null;
let consecutiveFailures = 0;
let reconnectTimer = 0;
+ let helloResetTimer = 0;
let closedByUser = false;
// First open is a cold start (initial history already loaded separately);
// only reconnections trigger a gap-filling backfill.
@@ -327,7 +334,13 @@ function subscribeTipStream() {
return;
}
es.addEventListener('hello', () => {
- consecutiveFailures = 0;
+ // Clear the failure counter only after the connection SURVIVES a while.
+ // Resetting on hello alone defeats the backoff when connections open
+ // fine but die young (an LB idle timeout under the first heartbeat,
+ // proxy buffering): each short-lived success would re-arm a ~1s
+ // reconnect loop with a full history fetch per second, forever.
+ clearTimeout(helloResetTimer);
+ helloResetTimer = setTimeout(() => { consecutiveFailures = 0; }, 20_000);
setFeedStatus(null);
// Pull anything missed while the socket was down. Dedup makes this safe.
if (reconnecting) {
@@ -351,6 +364,9 @@ function subscribeTipStream() {
try { es?.close(); } catch {}
es = null;
reconnecting = true;
+ // A connection that died before the survival window must not later
+ // zero the counter mid-outage.
+ clearTimeout(helloResetTimer);
consecutiveFailures += 1;
if (consecutiveFailures >= 3) {
setFeedStatus('Live updates paused', 'paused');
@@ -373,12 +389,14 @@ function subscribeTipStream() {
window.addEventListener('beforeunload', () => {
closedByUser = true;
clearTimeout(reconnectTimer);
+ clearTimeout(helloResetTimer);
try { es?.close(); } catch {}
});
return {
close() {
closedByUser = true;
clearTimeout(reconnectTimer);
+ clearTimeout(helloResetTimer);
try { es?.close(); } catch {}
},
};
@@ -1755,6 +1773,9 @@ function setAutoFollow(on) {
// Track per-pole card elements for status updates.
const poleCardEls = new Map();
+// Per-pole cached progress-bar elements + last written percent, so the render
+// loop never queries the DOM and only writes styles on a visible change.
+const poleProgressEls = new Map();
function updatePoleCardStatus(poleId, status) {
const cardEl = poleCardEls.get(poleId);
@@ -1790,13 +1811,19 @@ function updatePoleCardStatus(poleId, status) {
if (avatarBtn) avatarBtn.disabled = status === 'performing';
}
+// Clear the flash on a wall-clock timer matched to the poleFlash animation
+// (0.6s) rather than animationend: suspended animations (hidden tab,
+// prefers-reduced-motion) never fire the event, which stranded the class and
+// piled one orphaned once-listener onto the card per tip.
+const poleFlashTimers = new Map();
function flashPoleCard(poleId) {
const cardEl = poleCardEls.get(poleId);
if (!cardEl) return;
cardEl.classList.remove('is-flash');
void cardEl.offsetWidth; // force reflow for re-triggering animation
cardEl.classList.add('is-flash');
- cardEl.addEventListener('animationend', () => cardEl.classList.remove('is-flash'), { once: true });
+ clearTimeout(poleFlashTimers.get(poleId));
+ poleFlashTimers.set(poleId, setTimeout(() => cardEl.classList.remove('is-flash'), 700));
}
// ── Avatar selector ──────────────────────────────────────────────────────
@@ -2133,6 +2160,14 @@ function renderPoles() {
`;
polesPanel.appendChild(card);
poleCardEls.set(pole.id, card);
+ // Resolve the progress elements once: the render loop reads this map per
+ // frame per performing dancer, and a querySelector pair at 60fps per
+ // station is sustained layout/AT churn the frame budget can't spare.
+ poleProgressEls.set(pole.id, {
+ bar: card.querySelector('.club-pole-progress-bar'),
+ progress: card.querySelector('.club-pole-progress'),
+ lastPct: -1,
+ });
}
// Tip buttons stay disabled until the x402 payment widget is live, so a tap
@@ -2362,17 +2397,18 @@ function animate(frameNow) {
+ (prefersReducedMotion ? 0 : Math.sin(t * 1.5 + station.idx) * 0.01);
}
- // Update progress bar for performing stations.
+ // Update progress bar for performing stations. Elements are cached at
+ // renderPoles() time and writes are gated on a whole-percent change, so
+ // a routine costs ~100 style writes total instead of per-frame churn.
if (station.performing && station.activeUntil > 0) {
const total = (station.activeTicket?.durationSec || 12) * 1000;
const elapsed = total - (station.activeUntil - Date.now());
- const pct = Math.min(100, Math.max(0, (elapsed / total) * 100));
- const cardEl = poleCardEls.get(station.id);
- if (cardEl) {
- const bar = cardEl.querySelector('.club-pole-progress-bar');
- const progressEl = cardEl.querySelector('.club-pole-progress');
- if (bar) bar.style.width = `${pct}%`;
- if (progressEl) progressEl.setAttribute('aria-valuenow', String(Math.round(pct)));
+ const pct = Math.round(Math.min(100, Math.max(0, (elapsed / total) * 100)));
+ const els = poleProgressEls.get(station.id);
+ if (els && els.lastPct !== pct) {
+ els.lastPct = pct;
+ if (els.bar) els.bar.style.width = `${pct}%`;
+ if (els.progress) els.progress.setAttribute('aria-valuenow', String(pct));
}
}
}