fix(trace viewer): fix memory leak

This commit is contained in:
Simon Knott 2024-08-29 16:25:54 +02:00
parent 6763d5ab6b
commit 08a4045525
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -32,6 +32,7 @@ export class SnapshotRenderer {
private _resources: ResourceSnapshot[]; private _resources: ResourceSnapshot[];
private _snapshot: FrameSnapshot; private _snapshot: FrameSnapshot;
private _callId: string; private _callId: string;
private _renderResults = new WeakMap<FrameSnapshot, string>();
constructor(resources: ResourceSnapshot[], snapshots: FrameSnapshot[], index: number) { constructor(resources: ResourceSnapshot[], snapshots: FrameSnapshot[], index: number) {
this._resources = resources; this._resources = resources;
@ -61,7 +62,6 @@ export class SnapshotRenderer {
return escapeHTML(n); return escapeHTML(n);
} }
if (!(n as any)._string) {
if (isSubtreeReferenceSnapshot(n)) { if (isSubtreeReferenceSnapshot(n)) {
// Node reference. // Node reference.
const referenceIndex = snapshotIndex - n[0][0]; const referenceIndex = snapshotIndex - n[0][0];
@ -69,7 +69,7 @@ export class SnapshotRenderer {
const nodes = snapshotNodes(this._snapshots[referenceIndex]); const nodes = snapshotNodes(this._snapshots[referenceIndex]);
const nodeIndex = n[0][1]; const nodeIndex = n[0][1];
if (nodeIndex >= 0 && nodeIndex < nodes.length) if (nodeIndex >= 0 && nodeIndex < nodes.length)
(n as any)._string = visit(nodes[nodeIndex], referenceIndex, parentTag, parentAttrs); return visit(nodes[nodeIndex], referenceIndex, parentTag, parentAttrs);
} }
} else if (isNodeNameAttributesChildNodesSnapshot(n)) { } else if (isNodeNameAttributesChildNodesSnapshot(n)) {
const [name, nodeAttrs, ...children] = n; const [name, nodeAttrs, ...children] = n;
@ -114,17 +114,18 @@ export class SnapshotRenderer {
builder.push(visit(child, snapshotIndex, nodeName, attrs)); builder.push(visit(child, snapshotIndex, nodeName, attrs));
if (!autoClosing.has(nodeName)) if (!autoClosing.has(nodeName))
builder.push('</', nodeName, '>'); builder.push('</', nodeName, '>');
(n as any)._string = builder.join(''); return builder.join('');
} else { } else {
// Why are we here? Let's not throw, just in case. // Why are we here? Let's not throw, just in case.
(n as any)._string = ''; return '';
} }
}
return (n as any)._string;
}; };
const snapshot = this._snapshot; const snapshot = this._snapshot;
let html = visit(snapshot.html, this._index, undefined, undefined); if (!this._renderResults.has(snapshot))
this._renderResults.set(snapshot, visit(snapshot.html, this._index, undefined, undefined));
let html = this._renderResults.get(snapshot);
if (!html) if (!html)
return { html: '', pageId: snapshot.pageId, frameId: snapshot.frameId, index: this._index }; return { html: '', pageId: snapshot.pageId, frameId: snapshot.frameId, index: this._index };