A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: preserve window listeners with current anchors (#11272)
Yaqoubjb0282348dfd2
diff --git a/frontend/src/hooks/__tests__/useEventListener.test.tsx b/frontend/src/hooks/__tests__/useEventListener.test.tsxindex 5635bb5868..e3a7b16883 100644--- a/frontend/src/hooks/__tests__/useEventListener.test.tsx+++ b/frontend/src/hooks/__tests__/useEventListener.test.tsx@@ -2,7 +2,7 @@import { renderHook } from "@testing-library/react";import { useRef } from "react";-import { beforeEach, describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import {type HTMLElementNotDerivedFromRef,isRefObject,@@ -28,6 +28,50 @@ describe("isRefObject", () => {});});+describe.each([1, 2])("with %i anchors named current", (count) => {+ let iframe: HTMLIFrameElement;+ let browserWindow: Window;++ beforeEach(() => {+ // An iframe preserves named properties hidden by Vitest's global window.+ iframe = document.createElement("iframe");+ document.body.append(iframe);+ if (!iframe.contentWindow) {+ throw new Error("Expected an iframe window");+ }+ browserWindow = iframe.contentWindow;+ const anchors = Array.from({ length: count }, () => {+ const anchor = browserWindow.document.createElement("a");+ anchor.id = "current";+ return anchor;+ });+ browserWindow.document.body.append(...anchors);+ });++ afterEach(() => {+ iframe.remove();+ });++ it("does not mistake window for a React ref", () => {+ expect("current" in browserWindow).toBe(true);+ expect(isRefObject(browserWindow)).toBe(false);+ });++ it("attaches and removes the listener on window", () => {+ const listener = vi.fn();+ const { unmount } = renderHook(() =>+ useEventListener(browserWindow, "resize", listener),+ );++ browserWindow.dispatchEvent(new Event("resize"));+ expect(listener).toHaveBeenCalledTimes(1);++ unmount();+ browserWindow.dispatchEvent(new Event("resize"));+ expect(listener).toHaveBeenCalledTimes(1);+ });+});+describe("useEventListener", () => {beforeEach(() => {vi.clearAllMocks();diff --git a/frontend/src/hooks/useEventListener.ts b/frontend/src/hooks/useEventListener.tsindex 5ef2a1f7bf..a889e25d7c 100644--- a/frontend/src/hooks/useEventListener.ts+++ b/frontend/src/hooks/useEventListener.ts@@ -13,7 +13,12 @@ export type HTMLElementNotDerivedFromRef<T = HTMLElement> = T & {};export function isRefObject<T>(target: unknown): target is RefObject<T | null> {- return target !== null && typeof target === "object" && "current" in target;+ return (+ target !== null &&+ typeof target === "object" &&+ !("addEventListener" in target) &&+ "current" in target+ );}export function useEventListener<K extends keyof DocumentEventMap>(