From 5dcd168a28837ab1a60bb25b3d8dea761408219e Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Wed, 17 Jun 2026 14:55:41 -0700 Subject: [PATCH] perf(sdk): cache draft element in applyDraft, drop HTMLElement casts (R1 #1490a) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit applyDraft runs at 60fps during a drag but re-ran doc.querySelector on every call — the _draftEl/_draftId fields were only consumed by commit/cancel, never to skip the query. Reuse the tracked element when the id matches and the node is still connected; re-query only on id change or detach (iframe reload). Retypes _draftEl to HTMLElement | null (only ever set from querySelector), which removes the `as HTMLElement` casts in commitPreview / _clearDraft. Test asserts a repeated same-id drag queries once. Co-Authored-By: Claude Opus 4.8 (1M context) --- packages/sdk/src/adapters/iframe.test.ts | 21 +++++++++++++++++++++ packages/sdk/src/adapters/iframe.ts | 24 ++++++++++++++---------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/packages/sdk/src/adapters/iframe.test.ts b/packages/sdk/src/adapters/iframe.test.ts index 879a96186f..c018619004 100644 --- a/packages/sdk/src/adapters/iframe.test.ts +++ b/packages/sdk/src/adapters/iframe.test.ts @@ -212,6 +212,7 @@ interface FakeDomEl { "data-x": string | null; "data-y": string | null; style: FakeStyle; + isConnected: boolean; getAttribute(name: string): string | null; querySelector(sel: string): FakeDomEl | null; } @@ -234,6 +235,7 @@ function fakeDomEl(id: string, dataX: string | null, dataY: string | null): Fake "data-x": dataX, "data-y": dataY, style, + isConnected: true, getAttribute(name) { if (name === "data-x") return this["data-x"]; if (name === "data-y") return this["data-y"]; @@ -304,6 +306,25 @@ describe("IframePreviewAdapter draft / commit / cancel", () => { }); }); + it("applyDraft reuses the cached element across repeated calls (no re-query)", () => { + const el = fakeDomEl("hf-abc", "0", "0"); + let queryCount = 0; + const iframe = { + contentDocument: { + querySelector(_sel: string) { + queryCount++; + return el; + }, + }, + } as unknown as HTMLIFrameElement; + const adapter = createIframePreviewAdapter(iframe); + adapter.applyDraft("hf-abc", { dx: 1, dy: 1 }); + adapter.applyDraft("hf-abc", { dx: 2, dy: 2 }); + adapter.applyDraft("hf-abc", { dx: 3, dy: 3 }); + // Queried once on the first call; the next two reuse the connected cache. + expect(queryCount).toBe(1); + }); + it("commitPreview without a dispatch callback is a no-op", () => { const el = fakeDomEl("hf-abc", "0", "0"); const adapter = createIframePreviewAdapter(fakeIframe(el)); diff --git a/packages/sdk/src/adapters/iframe.ts b/packages/sdk/src/adapters/iframe.ts index 139f689cbb..a0af067f94 100644 --- a/packages/sdk/src/adapters/iframe.ts +++ b/packages/sdk/src/adapters/iframe.ts @@ -102,7 +102,7 @@ class IframePreviewAdapter implements PreviewAdapter { /** Tracked id and element for the in-progress drag. */ private _draftId: string | null = null; - private _draftEl: Element | null = null; + private _draftEl: HTMLElement | null = null; constructor(iframe: HTMLIFrameElement, dispatch?: (op: EditOp) => void) { this.iframe = iframe; @@ -141,9 +141,14 @@ class IframePreviewAdapter implements PreviewAdapter { const doc = this.iframe.contentDocument; if (!doc) return; - const el = doc.querySelector( - `[data-hf-id="${id.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"]`, - ); + // Reuse the tracked element across the 60fps drag; only re-query when the id + // changes or the cached node detached (e.g. an iframe reload mid-drag). + const cached = id === this._draftId && this._draftEl?.isConnected ? this._draftEl : null; + const el = + cached ?? + doc.querySelector( + `[data-hf-id="${id.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"]`, + ); if (!el) return; this._draftId = id; @@ -167,11 +172,11 @@ class IframePreviewAdapter implements PreviewAdapter { return; } - const el = this._draftEl as HTMLElement; + const el = this._draftEl; const dx = parseFloat(el.style.getPropertyValue(VAR_DX) || "0") || 0; const dy = parseFloat(el.style.getPropertyValue(VAR_DY) || "0") || 0; - const dataX = (this._draftEl as Element).getAttribute("data-x"); - const dataY = (this._draftEl as Element).getAttribute("data-y"); + const dataX = el.getAttribute("data-x"); + const dataY = el.getAttribute("data-y"); const { x, y } = computeDraftPosition(dataX, dataY, dx, dy); this._dispatch({ type: "moveElement", target: this._draftId, x, y }); @@ -185,9 +190,8 @@ class IframePreviewAdapter implements PreviewAdapter { private _clearDraft(): void { if (this._draftEl) { - const el = this._draftEl as HTMLElement; - el.style.removeProperty(VAR_DX); - el.style.removeProperty(VAR_DY); + this._draftEl.style.removeProperty(VAR_DX); + this._draftEl.style.removeProperty(VAR_DY); } this._draftId = null; this._draftEl = null;