Skip to content

Commit e16308b

Browse files
committed
fix: memoryleaks in hoverpreviews
1 parent fc111ea commit e16308b

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

packages/webui/src/client/ui/PreviewPopUp/PreviewPopUp.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,27 @@ export const PreviewPopUp = React.forwardRef<
7373

7474
useEffect(() => {
7575
updateRef.current = update
76+
}, [update])
7677

78+
useEffect(() => {
7779
if (trackMouse) {
7880
const listener = ({ clientX: x }: MouseEvent) => {
7981
virtualElement.current.getBoundingClientRect = generateGetBoundingClientRect(
8082
x,
8183
anchor?.getBoundingClientRect().y ?? 0
8284
)
83-
if (update) update().catch((e) => console.error(e))
85+
// If update is available, call it to reposition the popper:
86+
if (updateRef.current) {
87+
updateRef.current().catch((e) => console.error(e))
88+
}
8489
}
8590
document.addEventListener('mousemove', listener)
8691

8792
return () => {
8893
document.removeEventListener('mousemove', listener)
8994
}
9095
}
91-
}, [update, anchor])
96+
}, [trackMouse, anchor])
9297

9398
useImperativeHandle(ref, () => {
9499
return {

packages/webui/src/client/ui/PreviewPopUp/Previews/IFramePreview.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@ export function IFramePreview({ content }: IFramePreviewProps): React.ReactEleme
2020
const url = new URL(content.href)
2121
iFrameElement.current?.contentWindow?.postMessage(content.postMessage, url.origin)
2222
}
23-
}, [])
23+
}, [content.postMessage, content.href])
2424

2525
useEffect(() => {
26-
if (!iFrameElement) return
26+
// Create a stable reference to the iframe element:
27+
const currentIFrame = iFrameElement.current
28+
if (!currentIFrame) return
29+
currentIFrame.addEventListener('load', onLoadListener)
2730

28-
iFrameElement.current?.addEventListener('load', onLoadListener)
29-
30-
return () => iFrameElement.current?.removeEventListener('load', onLoadListener)
31-
}, [iFrameElement.current, onLoadListener])
31+
return () => currentIFrame.removeEventListener('load', onLoadListener)
32+
}, [onLoadListener])
3233

3334
const style: Record<string, string | number> = {}
3435
if (content.dimensions) {

packages/webui/src/client/ui/PreviewPopUp/Previews/VTPreview.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ export function VTPreviewElement({ content, time }: VTPreviewProps): React.React
3636
const studioContext = useContext(StudioContext)
3737

3838
useEffect(() => {
39-
if (!videoElement.current) return
39+
// Create a stable reference to the video element:
40+
const currentVideoElement = videoElement.current
41+
if (!currentVideoElement) return
4042

4143
setVideoElementPosition(
42-
videoElement.current,
44+
currentVideoElement,
4345
time ?? 0,
4446
content.itemDuration ?? 0,
4547
content.seek ?? 0,
4648
content.loop ?? false
4749
)
48-
}, [videoElement.current, time])
50+
}, [time, content.itemDuration, content.seek, content.loop])
4951

5052
const itemDuration = content.itemDuration ?? 0
5153
const offsetTimePosition = (time ?? 0) + (content.seek ?? 0)

0 commit comments

Comments
 (0)