Preserve JSROOT object prototypes when cloning cached canvases

The JSON deep-clone for clean/original/reset redraws stripped the JSROOT
prototype methods (TestBit et al.) that jsroot.draw depends on, breaking
renders with 'this[#pad].TestBit is not a function' even on desktop. Clone
now prefers JSROOT's own clone(), which keeps the object prototypes intact,
and only falls back to the JSON copy when clone() is unavailable.
This commit is contained in:
salman committed 2026-09-03 21:53:56 +02:00
1 parent 4efe67eab9
commit 8d38d54f52
2 files changed
+22 -12

No files matched your search

+18 -10
View File
@@ -571,16 +571,24 @@ function musrfitRootRenderIsStale(container, generation, canvas) {
return !canvas || !canvas.isConnected;
}
// A decoded musrview canvas is a plain tree of ROOT field objects. Reusing the
// decoded object across clean/original/reset redraws avoids a network round-trip
// per toggle, which is the slow, flaky path on mobile. A JSON round-trip is a
// JSROOT-version-independent deep clone; JSROOT draws by field name, so the copy
// paints identically. Each clone is mutated in place by the clean view pipeline,
// leaving the cached pristine copy untouched for the "original" view.
function musrfitRootCloneCanvas(canvasObject) {
// A decoded musrview canvas is a JSROOT tree; the individual objects carry
// prototype methods (e.g. TestBit) that jsroot.draw relies on. Reusing the
// decoded object across clean/original/reset redraws avoids a network
// round-trip per toggle, the slow and flaky path on mobile. Every render draws
// its own clone so the clean-view mutation never corrupts the cached pristine
// copy. Only JSROOT's own clone preserves the object prototypes; JSON cloning
// is a degraded fallback for environments without it.
function musrfitRootCloneCanvas(canvasObject, jsroot) {
if (canvasObject && typeof canvasObject !== "object") {
return canvasObject;
}
if (jsroot && typeof jsroot.clone === "function") {
try {
return jsroot.clone(canvasObject);
} catch (error) {
// fall through to the structural copy
}
}
try {
return JSON.parse(JSON.stringify(canvasObject));
} catch (error) {
@@ -696,7 +704,7 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM
if (decodeCache) {
let cached = decodeCache.get(cacheKey);
if (cached) {
drawObject = musrfitRootCloneCanvas(cached);
drawObject = musrfitRootCloneCanvas(cached, jsroot);
} else {
let fileObject = await withMusrfitRootTimeout(
file.readObject(plotSpec.item),
@@ -704,7 +712,7 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM
if (musrfitRootRenderIsStale(container, renderToken, canvas)) {
return;
}
drawObject = musrfitRootCloneCanvas(fileObject);
drawObject = musrfitRootCloneCanvas(fileObject, jsroot);
decodeCache.set(cacheKey, fileObject);
}
} else {
@@ -714,7 +722,7 @@ async function renderMusrfitRootPlots(container, plotSpecs, renderVersion, viewM
if (musrfitRootRenderIsStale(container, renderToken, canvas)) {
return;
}
drawObject = musrfitRootCloneCanvas(drawObject);
drawObject = musrfitRootCloneCanvas(drawObject, jsroot);
}
if (viewMode === "clean") {
+4 -2
View File
@@ -386,10 +386,12 @@ function testDecodeCache() {
const code = fs.readFileSync(path.join(repoRoot, "muSRFitRoot.js"), "utf8");
// renderMusrfitRootPlots accepts a decode cache; clean view draws a clone of
// the cached pristine copy.
// the cached pristine copy, preferring JSROOT's own clone to preserve the
// object prototypes jsroot.draw relies on (TestBit et al).
assert.match(code, /viewMode = "clean", decodeCache = null/);
assert.match(code, /drawObject = musrfitRootCloneCanvas\(cached\)/);
assert.match(code, /drawObject = musrfitRootCloneCanvas\(cached, jsroot\)/);
assert.match(code, /decodeCache\.set\(cacheKey, fileObject\)/);
assert.match(code, /if \(jsroot && typeof jsroot\.clone === "function"\)/);
// Spectrum renders reuse the window cache; FFT renders never do.
assert.match(code, /renderMusrfitRootPlots\(plotContainer, currentSpecs, resetVersion, rootViewMode, rootDecodeCache\)/);
assert.match(code, /renderMusrfitRootPlots\(plotContainer, currentSpecs, fftVersion, rootViewMode\)/);