From 8d38d54f52eb0f80b4eac8db254595bf4f933b01 Mon Sep 17 00:00:00 2001 From: salman Date: Thu, 3 Sep 2026 21:53:56 +0200 Subject: [PATCH] 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. --- muSRFitRoot.js | 28 ++++++++++++++++++---------- tests/root-plot-clean.test.js | 6 ++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/muSRFitRoot.js b/muSRFitRoot.js index 72584ff..77d9a12 100644 --- a/muSRFitRoot.js +++ b/muSRFitRoot.js @@ -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") { diff --git a/tests/root-plot-clean.test.js b/tests/root-plot-clean.test.js index 9cad7d5..15e6248 100644 --- a/tests/root-plot-clean.test.js +++ b/tests/root-plot-clean.test.js @@ -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\)/);