mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-16 14:27:26 +00:00
perf(sdk): cache draft element in applyDraft, drop HTMLElement casts (R1 #1490a)
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<HTMLElement>), 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) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
5b3db8de62
commit
5dcd168a28
@@ -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));
|
||||
|
||||
@@ -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<HTMLElement>(
|
||||
`[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<HTMLElement>(
|
||||
`[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;
|
||||
|
||||
Reference in New Issue
Block a user