Skip posting resize updates if only the width changes

We only care about height changes. This drastically cuts down on the number of messages we post on resize
This commit is contained in:
Matt Bierner 2021-06-10 16:17:14 -07:00
parent 492ed968f6
commit 8b674fee2c
No known key found for this signature in database
GPG key ID: 099C331567E11888

View file

@ -203,7 +203,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
const dimensionUpdater = new class {
private readonly pending = new Map<string, webviewMessages.DimensionUpdate>();
update(id: string, height: number, options: { init?: boolean; isOutput?: boolean }) {
updateHeight(id: string, height: number, options: { init?: boolean; isOutput?: boolean }) {
if (!this.pending.size) {
setTimeout(() => {
this.updateImmediately();
@ -232,7 +232,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
private readonly _observer: ResizeObserver;
private readonly _observedElements = new WeakMap<Element, { id: string, output: boolean }>();
private readonly _observedElements = new WeakMap<Element, { id: string, output: boolean, lastKnownHeight: number }>();
constructor() {
this._observer = new ResizeObserver(entries => {
@ -255,9 +255,13 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
}
}
dimensionUpdater.update(observedElementInfo.id, entry.target.clientHeight, {
isOutput: observedElementInfo.output
});
const clientHeight = entry.target.clientHeight;
if (observedElementInfo.lastKnownHeight !== clientHeight) {
observedElementInfo.lastKnownHeight = clientHeight;
dimensionUpdater.updateHeight(observedElementInfo.id, clientHeight, {
isOutput: observedElementInfo.output
});
}
}
}
});
@ -268,7 +272,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
return;
}
this._observedElements.set(container, { id, output });
this._observedElements.set(container, { id, output, lastKnownHeight: -1 });
this._observer.observe(container);
}
};
@ -603,14 +607,14 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
if (clientHeight !== 0 && cps.padding === '0px') {
// we set padding to zero if the output height is zero (then we can have a zero-height output DOM node)
// thus we need to ensure the padding is accounted when updating the init height of the output
dimensionUpdater.update(outputId, clientHeight + style.outputNodePadding * 2, {
dimensionUpdater.updateHeight(outputId, clientHeight + style.outputNodePadding * 2, {
isOutput: true,
init: true,
});
outputNode.style.padding = `${style.outputNodePadding}px ${style.outputNodePadding}px ${style.outputNodePadding}px ${style.outputNodeLeftPadding}px`;
} else {
dimensionUpdater.update(outputId, outputNode.clientHeight, {
dimensionUpdater.updateHeight(outputId, outputNode.clientHeight, {
isOutput: true,
init: true,
});
@ -1163,7 +1167,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
});
}
dimensionUpdater.update(this.id, this.element.clientHeight, {
dimensionUpdater.updateHeight(this.id, this.element.clientHeight, {
isOutput: false
});
}
@ -1188,7 +1192,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
}
private async updateMarkupDimensions() {
dimensionUpdater.update(this.id, this.element.clientHeight, {
dimensionUpdater.updateHeight(this.id, this.element.clientHeight, {
isOutput: false
});
}
@ -1284,7 +1288,7 @@ async function webviewPreloads(style: PreloadStyles, options: PreloadOptions, re
this.element.style.visibility = 'visible';
this.element.style.top = `${top}px`;
dimensionUpdater.update(outputId, outputContainer.clientHeight, {
dimensionUpdater.updateHeight(outputId, outputContainer.clientHeight, {
isOutput: true,
});
}