Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): remove the scroll listener of the scrolling container on cleanup (closes #11630)

Scott Campbell22b1383e4216
diff --git a/apps/client/src/widgets/containers/scrolling_container.spec.ts b/apps/client/src/widgets/containers/scrolling_container.spec.ts
index 20b49a0746..885394a3c3 100644
--- a/apps/client/src/widgets/containers/scrolling_container.spec.ts
+++ b/apps/client/src/widgets/containers/scrolling_container.spec.ts
@@ -78,7 +78,7 @@ describe("ScrollingContainer", () => {
expect(firstRect).not.toHaveBeenCalled();
});
- it("stops observing on cleanup", () => {
+ it("stops observing and listening to scrolls on cleanup", () => {
const { scrollingContainer, container, observer } = render();
const cancelAnimationFrame = vi.fn();
vi.stubGlobal("cancelAnimationFrame", cancelAnimationFrame);
@@ -88,6 +88,10 @@ describe("ScrollingContainer", () => {
expect(observer.disconnected).toBe(true);
expect(cancelAnimationFrame).toHaveBeenCalledWith(frames.length);
+
+ const framesBefore = frames.length;
+ container.dispatchEvent(new Event("scroll"));
+ expect(frames).toHaveLength(framesBefore);
});
/** Renders a container scrolled so that a paragraph sits right at its top edge. */
diff --git a/apps/client/src/widgets/containers/scrolling_container.ts b/apps/client/src/widgets/containers/scrolling_container.ts
index ae5c8c4d94..a9230f53b9 100644
--- a/apps/client/src/widgets/containers/scrolling_container.ts
+++ b/apps/client/src/widgets/containers/scrolling_container.ts
@@ -11,6 +11,7 @@ export default class ScrollingContainer extends Container<BasicWidget> {
private scrollAnchor: ScrollAnchor | null = null;
private pendingAnchorFrame: number | null = null;
private resizeObserver?: ResizeObserver;
+ private scrollListener?: AbortController;
constructor() {
super();
@@ -36,13 +37,14 @@ export default class ScrollingContainer extends Container<BasicWidget> {
: null;
};
+ this.scrollListener = new AbortController();
container.addEventListener("scroll", () => {
if (this.pendingAnchorFrame !== null) return;
this.pendingAnchorFrame = requestAnimationFrame(() => {
this.pendingAnchorFrame = null;
recordAnchor();
});
- }, { passive: true });
+ }, { passive: true, signal: this.scrollListener.signal });
let wasHidden = false;
this.resizeObserver = new ResizeObserver(([ entry ]) => {
@@ -62,9 +64,11 @@ export default class ScrollingContainer extends Container<BasicWidget> {
}
cleanup() {
+ this.scrollListener?.abort();
this.resizeObserver?.disconnect();
if (this.pendingAnchorFrame !== null) {
cancelAnimationFrame(this.pendingAnchorFrame);
+ this.pendingAnchorFrame = null;
}
}