Spaces:
Running on Zero
Keep the Studio preview matched to the selected input
I fixed a race in the Perturbation Studio where switching inputs before a preview resolved could leave an older molecule or gene-network readout on screen. To reproduce it, I delayed the first HTTP response until the second preview appeared in Chrome. The stale readout appeared in all eight orders tested across the published molecule and gene presets. After Dasatinib showed 33 atoms and 488 Da, a late Panobinostat preview replaced that label with 26 atoms and 349.4 Da while Dasatinib stayed selected.
The fix checks the active mode and selected input before applying data or showing an error. The correct readout stayed visible in all eight replays.
I ran a separate Node 22 regression test for delayed JSON bodies and late HTTP, network, and JSON parsing failures on both preview endpoints. It fails on main and passes with this patch.
The browser replay used controlled delays over local HTTP.
Save the regression as preview-race.cjs and run node preview-race.cjs static/app.js from the Studio checkout.
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const labels = new Map();
const document = {
readyState: 'loading',
addEventListener() {},
getElementById(id) {
if (!labels.has(id)) labels.set(id, { classList: { add() {}, remove() {} } });
return labels.get(id);
},
};
const replies = [];
const window = {};
const source = fs.readFileSync(process.argv[2] || 'static/app.js', 'utf8');
vm.runInNewContext(source.replace(/\}\)\(\);\s*$/, `
window.select = (mode, value) => {
appState.mode = mode;
if (mode === "drug") return fetchMolecule3D(appState.smiles = value);
return fetchGeneGraph3D(appState.gene = value);
};
window.preview = () => appState.inputViz;
})();`), { window, document, performance, fetch: () => replies.shift() });
(async () => {
const checks = [];
const error = document.getElementById('error-banner');
for (const [mode, oldInput, newInput, view] of [
['drug', 'C', 'CC', { num_atoms: 2, mol_weight: 30.1 }],
['crispr', 'MYC', 'DNMT1', { total_edges: 20 }],
]) {
for (const delayed of ['body', 'http-error', 'network-error', 'json-error']) {
const gate = Promise.withResolvers();
const oldView = { num_atoms: 1, mol_weight: 16, total_edges: 10 };
replies.push(delayed === 'body' || delayed === 'json-error'
? { ok: true, json: () => gate.promise } : gate.promise);
const older = window.select(mode, oldInput);
await Promise.resolve();
replies.push({ ok: true, json: async () => view });
await window.select(mode, newInput);
if (delayed === 'body') gate.resolve(oldView);
if (delayed === 'http-error') gate.resolve({ ok: false });
if (delayed.endsWith('-error') && delayed !== 'http-error') gate.reject(new Error(delayed));
await older;
checks.push([mode, delayed, window.preview() === view, error.textContent]);
}
replies.push({ ok: false });
await window.select(mode, newInput);
assert.match(error.textContent, /Invalid SMILES|Unknown human gene/);
}
assert.deepEqual(checks, checks.map(([mode, delayed]) => [mode, delayed, true, '']));
})().catch((error) => {
console.error(error);
process.exitCode = 1;
});
🤖 Generated with Claude Code