* fix(interactive): keep-alive iframe pool so interactive scenes don't reload on remount (#619) Interactive scene iframes reloaded on every remount — Pro mode toggle, scene switch and back, any PlaybackChromeRoot remount — because the <iframe srcDoc> was rendered inline in the scene subtree and React's unmount (or a DOM re-parent) makes the browser drop and re-run the doc. Persist the iframes in a stable host at the Stage root, outside the mode-swap/scene subtree, keyed by sceneId: - lib/store/interactive-iframe-pool: per-scene entries (srcDoc/src, content hash, tracked rect, visibility), LRU-bounded (CAP=3). Re-mounting with an unchanged hash keeps the existing content reference, so the iframe is never re-set; only a content-hash change rebuilds it. - InteractiveIframeHost: mounted once in Stage, portals one persisted <iframe> per entry to document.body, positioned over each scene's rect. Hidden (visibility, never display:none) in edit mode and whenever the placeholder is gone. Owns the widget-iframe postMessage registration. - InteractiveRenderer: now a placeholder that registers content, marks the scene active/visible, reports its rect via a rAF loop, and hides (not evicts) on unmount. Mode toggle, back-and-forth scene switching, and re-renders now hit the cache with zero reload and zero flash. widget-iframe messaging API is unchanged; no change to the playback engine / SSE / edit-lock. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * test(e2e): interactive iframe keep-alive across Pro toggle + scene switch (#619) Seeds an interactive scene whose widget holds click-counter state, then drives the two remount triggers (Pro mode toggle, scene switch and back). A MutationObserver scoped to the keep-alive iframe's title asserts the element is never removed or recreated across either trigger, and the in-iframe counter state is preserved — i.e. zero reload. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(interactive): address review — widget callback timing, fullscreen, content compare, rect gate (#619) Code-review round-1 fixes for the keep-alive iframe pool: - PlaybackChromeRoot: resolve the widget postMessage callback lazily at send time instead of capturing it once at engine construction. The iframe (and its registration) now lives in the keep-alive host, which registers a commit after the engine is built — so eager capture got null on a scene's first visit and silently dropped every widget action. - InteractiveIframeHost: portal target follows document.fullscreenElement so the iframe stays inside the presentation-fullscreen subtree (requestFullscreen runs on the stage, not body) instead of vanishing. Known tradeoff: entering/exiting fullscreen re-parents the iframe and reloads it once — accepted over an invisible widget during presentation. - InteractiveIframeHost: reset the pool when the host unmounts (classroom switch) so a new classroom doesn't retain the previous one's resident iframes. - InteractiveIframeHost: only show the iframe once its rect has a real measured size (width>0 && height>0), avoiding a 0x0 flash at the viewport origin before first layout. - pool: drop the djb2 hash; compare srcDoc/src by value (string === is value equality, so an equal-but-new remount string still hits the keep-alive path). Removes the hash-collision-skips-rebuild hole and simplifies the store. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * fix(interactive): owner-token visibility so stale cross-fade cleanup can't blank the iframe (#619) cosarah reproduced: after toggling Edit mode on an interactive scene and returning to playback, the scene flashed its preserved state then went permanently blank. Root cause is a visibility race, not a reload — the document stayed alive. Visibility was keyed only by sceneId, so during Stage's AnimatePresence mode cross-fade the outgoing PlaybackChromeRoot's placeholder cleanup (hide) ran after the incoming placeholder had already shown the same scene, hiding the live iframe. Give each placeholder mount a visibility-ownership token (useId). The pool records the owner on claim(); release() only clears visibility when the caller still owns it, so a stale cleanup no-ops instead of blanking a newer instance. The host now shows iff entry.owner !== null. - store: replace visible boolean + show/hide with owner + claim/release. - interactive-renderer: owner = useId(); claim on mount, release on unmount. - e2e: after returning from Pro mode, wait out the cross-fade and re-assert the iframe stays visible with its state (guards the blank-on-return regression). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> Co-authored-by: 杨慎 <117187635+cosarah@users.noreply.github.com>
129 lines
4.8 KiB
TypeScript
129 lines
4.8 KiB
TypeScript
import { beforeEach, describe, expect, test } from 'vitest';
|
|
import { IFRAME_POOL_CAP, useInteractiveIframePool } from '@/lib/store/interactive-iframe-pool';
|
|
|
|
function reset() {
|
|
useInteractiveIframePool.getState().reset();
|
|
}
|
|
|
|
const pool = () => useInteractiveIframePool.getState();
|
|
|
|
beforeEach(reset);
|
|
|
|
describe('mount', () => {
|
|
test('creates an entry with the given content', () => {
|
|
pool().mount('s1', { srcDoc: '<p>1</p>' });
|
|
const e = pool().entries['s1'];
|
|
expect(e).toBeDefined();
|
|
expect(e.srcDoc).toBe('<p>1</p>');
|
|
});
|
|
|
|
test('re-mounting with equal content keeps the entry (no reload)', () => {
|
|
pool().mount('s1', { srcDoc: '<p>1</p>' });
|
|
const first = pool().entries['s1'].srcDoc;
|
|
// A remount recomputes an equal-but-new string; value equality must still
|
|
// hit the keep-alive fast path so the host never re-sets srcDoc.
|
|
pool().mount('s1', { srcDoc: ['<p>', '1', '</p>'].join('') });
|
|
expect(pool().entries['s1'].srcDoc).toBe(first);
|
|
expect(Object.keys(pool().entries)).toHaveLength(1);
|
|
});
|
|
|
|
test('re-mounting with changed content replaces it (intended reload)', () => {
|
|
pool().mount('s1', { srcDoc: '<p>1</p>' });
|
|
pool().mount('s1', { srcDoc: '<p>2</p>' });
|
|
expect(pool().entries['s1'].srcDoc).toBe('<p>2</p>');
|
|
});
|
|
|
|
test('supports url-backed (src) entries', () => {
|
|
pool().mount('s1', { src: 'https://x/widget' });
|
|
expect(pool().entries['s1'].src).toBe('https://x/widget');
|
|
expect(pool().entries['s1'].srcDoc).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('visibility ownership + rect + active', () => {
|
|
test('release keeps the entry alive but clears its owner', () => {
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().claim('s1', 'ownerA');
|
|
expect(pool().entries['s1'].owner).toBe('ownerA');
|
|
pool().release('s1', 'ownerA');
|
|
expect(pool().entries['s1']).toBeDefined(); // keep-alive
|
|
expect(pool().entries['s1'].owner).toBeNull();
|
|
});
|
|
|
|
test('a stale release (wrong owner) does NOT clear a re-claimed entry', () => {
|
|
// Models the mode cross-fade: a newer placeholder re-claims the scene, then
|
|
// the outgoing placeholder's unmount cleanup fires with its old owner id.
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().claim('s1', 'old');
|
|
pool().claim('s1', 'new'); // newer placeholder takes over
|
|
pool().release('s1', 'old'); // stale cleanup must not hide it
|
|
expect(pool().entries['s1'].owner).toBe('new');
|
|
});
|
|
|
|
test('setRect updates the tracked rect', () => {
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().setRect('s1', { left: 1, top: 2, width: 3, height: 4 });
|
|
expect(pool().entries['s1'].rect).toEqual({ left: 1, top: 2, width: 3, height: 4 });
|
|
});
|
|
|
|
test('setActive records the active scene', () => {
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().setActive('s1');
|
|
expect(pool().activeSceneId).toBe('s1');
|
|
});
|
|
|
|
test('setRect / claim / release on an unknown scene are no-ops', () => {
|
|
pool().setRect('ghost', { left: 0, top: 0, width: 0, height: 0 });
|
|
pool().claim('ghost', 'o');
|
|
pool().release('ghost', 'o');
|
|
expect(pool().entries['ghost']).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('LRU eviction', () => {
|
|
test(`evicts the least-recently-mounted entry beyond CAP (${IFRAME_POOL_CAP})`, () => {
|
|
for (let i = 0; i < IFRAME_POOL_CAP + 1; i++) {
|
|
pool().mount(`s${i}`, { srcDoc: `x${i}` });
|
|
}
|
|
expect(Object.keys(pool().entries)).toHaveLength(IFRAME_POOL_CAP);
|
|
expect(pool().entries['s0']).toBeUndefined(); // oldest evicted
|
|
expect(pool().entries[`s${IFRAME_POOL_CAP}`]).toBeDefined(); // newest kept
|
|
});
|
|
|
|
test('never evicts the active scene even if it is the oldest', () => {
|
|
pool().mount('s0', { srcDoc: 'x0' });
|
|
pool().setActive('s0');
|
|
for (let i = 1; i < IFRAME_POOL_CAP + 1; i++) {
|
|
pool().mount(`s${i}`, { srcDoc: `x${i}` });
|
|
}
|
|
expect(pool().entries['s0']).toBeDefined(); // active survives
|
|
expect(Object.keys(pool().entries)).toHaveLength(IFRAME_POOL_CAP);
|
|
});
|
|
|
|
test('re-mounting an existing entry refreshes its recency', () => {
|
|
pool().mount('s0', { srcDoc: 'x0' });
|
|
pool().mount('s1', { srcDoc: 'x1' });
|
|
pool().mount('s2', { srcDoc: 'x2' });
|
|
pool().mount('s0', { srcDoc: 'x0' }); // touch s0 -> s1 now oldest
|
|
pool().mount('s3', { srcDoc: 'x3' });
|
|
expect(pool().entries['s1']).toBeUndefined();
|
|
expect(pool().entries['s0']).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('evict / reset', () => {
|
|
test('evict removes an entry explicitly', () => {
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().evict('s1');
|
|
expect(pool().entries['s1']).toBeUndefined();
|
|
});
|
|
|
|
test('reset clears all entries and active scene', () => {
|
|
pool().mount('s1', { srcDoc: 'x' });
|
|
pool().setActive('s1');
|
|
pool().reset();
|
|
expect(Object.keys(pool().entries)).toHaveLength(0);
|
|
expect(pool().activeSceneId).toBeNull();
|
|
});
|
|
});
|