Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

start the n-back demo paused when the card mounts into a hidden tab

[._.]/ Adam Eivya745eb5d656d
diff --git a/client/src/components/meatspace/post/CognitiveDrillTutorial.jsx b/client/src/components/meatspace/post/CognitiveDrillTutorial.jsx
index c880560054..fae59f4551 100644
--- a/client/src/components/meatspace/post/CognitiveDrillTutorial.jsx
+++ b/client/src/components/meatspace/post/CognitiveDrillTutorial.jsx
@@ -414,7 +414,11 @@ function NBackDemo({ example: { sequence, n } }) {
// A backgrounded tab is the other reason to stop scheduling: PortOS is
// routinely left open on a second tailnet machine, and an unseen animation
// would otherwise re-render this subtree about once a second forever.
- const [hidden, setHidden] = useState(false);
+ // Seeded from the CURRENT state, not `false`: a card mounted into an
+ // already-hidden tab would otherwise animate until the next visibilitychange.
+ const [hidden, setHidden] = useState(
+ () => (typeof document !== 'undefined' && document.visibilityState === 'hidden'),
+ );
useVisibilityEvent(state => setHidden(state === 'hidden'));
const step = reduced ? matchIndex : rawStep;
diff --git a/client/src/components/meatspace/post/CognitiveDrillTutorial.test.jsx b/client/src/components/meatspace/post/CognitiveDrillTutorial.test.jsx
index 5efece67c2..8033d32023 100644
--- a/client/src/components/meatspace/post/CognitiveDrillTutorial.test.jsx
+++ b/client/src/components/meatspace/post/CognitiveDrillTutorial.test.jsx
@@ -209,6 +209,10 @@ describe('n-back demo', () => {
// runs after React commits the current one, so a single large advance would
// only ever move it on by one letter.
const advanceFrom = (step, last) => act(() => { vi.advanceTimersByTime(demoFrameDelay(step, last)); });
+ const setVisibility = (value) => {
+ Object.defineProperty(document, 'visibilityState', { value, configurable: true });
+ act(() => { document.dispatchEvent(new Event('visibilitychange')); });
+ };
const playToHit = (last) => { for (let step = -1; step < last; step += 1) advanceFrom(step, last); };
beforeEach(() => {
@@ -219,6 +223,9 @@ describe('n-back demo', () => {
afterEach(() => {
window.matchMedia = originalMatchMedia;
+ // Unconditional: the hidden-tab case below mutates `visibilityState`, and a
+ // failing assertion there would otherwise leave the whole file hidden.
+ setVisibility('visible');
vi.useRealTimers();
});
@@ -276,16 +283,23 @@ describe('n-back demo', () => {
advanceFrom(-1, last);
expect(stage()).toBe(sequence[0]);
- Object.defineProperty(document, 'visibilityState', { value: 'hidden', configurable: true });
- act(() => { document.dispatchEvent(new Event('visibilitychange')); });
+ setVisibility('hidden');
expect(vi.getTimerCount()).toBe(0);
- Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true });
- act(() => { document.dispatchEvent(new Event('visibilitychange')); });
+ setVisibility('visible');
advanceFrom(0, last);
expect(stage()).toBe(sequence[1]);
});
+ it('never starts when the card mounts into an already-hidden tab', () => {
+ // The subscription only fires on a CHANGE, so seeding `hidden` from the
+ // current state is the only thing standing between a backgrounded tab and a
+ // demo that animates until someone looks at it again.
+ setVisibility('hidden');
+ renderDemo(2);
+ expect(vi.getTimerCount()).toBe(0);
+ });
+
it('marks exactly one chip as the N-back comparison on every frame', () => {
// The sliding window IS the lesson, so a frame with two markers (or none)
// would point the reader at the wrong pair of letters.