mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-13 17:27:09 +00:00
Fix memory leaks (#1054)
This commit is contained in:
@@ -34,6 +34,8 @@ export default class TwoUp extends HTMLElement {
|
|||||||
*/
|
*/
|
||||||
private _everConnected = false;
|
private _everConnected = false;
|
||||||
|
|
||||||
|
private _resizeObserver?: ResizeObserver;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this._handle.className = styles.twoUpHandle;
|
this._handle.className = styles.twoUpHandle;
|
||||||
@@ -45,13 +47,6 @@ export default class TwoUp extends HTMLElement {
|
|||||||
childList: true,
|
childList: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Watch for element size changes.
|
|
||||||
if ('ResizeObserver' in window) {
|
|
||||||
new ResizeObserver(() => this._resetPosition()).observe(this);
|
|
||||||
} else {
|
|
||||||
window.addEventListener('resize', () => this._resetPosition());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Watch for pointers on the handle.
|
// Watch for pointers on the handle.
|
||||||
const pointerTracker: PointerTracker = new PointerTracker(this._handle, {
|
const pointerTracker: PointerTracker = new PointerTracker(this._handle, {
|
||||||
start: (_, event) => {
|
start: (_, event) => {
|
||||||
@@ -68,8 +63,6 @@ export default class TwoUp extends HTMLElement {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('keydown', (event) => this._onKeyDown(event));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
@@ -84,12 +77,28 @@ export default class TwoUp extends HTMLElement {
|
|||||||
}</svg>
|
}</svg>
|
||||||
`}</div>`;
|
`}</div>`;
|
||||||
|
|
||||||
|
// Watch for element size changes.
|
||||||
|
if ('ResizeObserver' in window) {
|
||||||
|
this._resizeObserver = new ResizeObserver(() => this._resetPosition());
|
||||||
|
this._resizeObserver.observe(this);
|
||||||
|
} else {
|
||||||
|
window.addEventListener('resize', this._onResize);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('keydown', this._onKeyDown);
|
||||||
|
|
||||||
if (!this._everConnected) {
|
if (!this._everConnected) {
|
||||||
this._resetPosition();
|
this._resetPosition();
|
||||||
this._everConnected = true;
|
this._everConnected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
disconnectedCallback() {
|
||||||
|
window.removeEventListener('keydown', this._onKeyDown);
|
||||||
|
window.removeEventListener('resize', this._onResize);
|
||||||
|
if (this._resizeObserver) this._resizeObserver.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
attributeChangedCallback(name: string) {
|
attributeChangedCallback(name: string) {
|
||||||
if (name === orientationAttr) {
|
if (name === orientationAttr) {
|
||||||
this._resetPosition();
|
this._resetPosition();
|
||||||
@@ -97,7 +106,7 @@ export default class TwoUp extends HTMLElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// KeyDown event handler
|
// KeyDown event handler
|
||||||
private _onKeyDown(event: KeyboardEvent) {
|
private _onKeyDown = (event: KeyboardEvent) => {
|
||||||
const target = event.target;
|
const target = event.target;
|
||||||
if (target instanceof HTMLElement && target.closest('input')) return;
|
if (target instanceof HTMLElement && target.closest('input')) return;
|
||||||
|
|
||||||
@@ -122,7 +131,9 @@ export default class TwoUp extends HTMLElement {
|
|||||||
this._relativePosition = this._position / bounds[dimensionAxis];
|
this._relativePosition = this._position / bounds[dimensionAxis];
|
||||||
this._setPosition();
|
this._setPosition();
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
|
private _onResize = () => this._resetPosition();
|
||||||
|
|
||||||
private _resetPosition() {
|
private _resetPosition() {
|
||||||
// Set the initial position of the handle.
|
// Set the initial position of the handle.
|
||||||
|
|||||||
@@ -377,6 +377,7 @@ export default class Compress extends Component<Props, State> {
|
|||||||
|
|
||||||
componentWillUnmount(): void {
|
componentWillUnmount(): void {
|
||||||
updateDocumentTitle({ loading: false });
|
updateDocumentTitle({ loading: false });
|
||||||
|
this.widthQuery.removeListener(this.onMobileWidthChange);
|
||||||
this.mainAbortController.abort();
|
this.mainAbortController.abort();
|
||||||
for (const controller of this.sideAbortControllers) {
|
for (const controller of this.sideAbortControllers) {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
|
|||||||
Reference in New Issue
Block a user