-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve resize listener with the loadedmetadata
event in DailyVideo
#11
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,13 +167,13 @@ export const DailyVideo = forwardRef<HTMLVideoElement, Props>( | |
if (!onResize || !video) return; | ||
|
||
let frame: ReturnType<typeof requestAnimationFrame>; | ||
const handleResize = () => { | ||
if (!video) return; | ||
function handleResize() { | ||
if (frame) cancelAnimationFrame(frame); | ||
frame = requestAnimationFrame(() => { | ||
if (document.hidden) return; | ||
const videoWidth = video?.videoWidth; | ||
const videoHeight = video?.videoHeight; | ||
const video = videoEl.current; | ||
if (!video || document.hidden) return; | ||
const videoWidth = video.videoWidth; | ||
const videoHeight = video.videoHeight; | ||
if (videoWidth && videoHeight) { | ||
onResize({ | ||
aspectRatio: videoWidth / videoHeight, | ||
|
@@ -182,14 +182,19 @@ export const DailyVideo = forwardRef<HTMLVideoElement, Props>( | |
}); | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
handleResize(); | ||
video?.addEventListener('resize', handleResize); | ||
video.addEventListener('loadedmetadata', handleResize); | ||
video.addEventListener('resize', handleResize); | ||
|
||
return () => video?.removeEventListener('resize', handleResize); | ||
return () => { | ||
if (frame) cancelAnimationFrame(frame); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not sure why all of these If these are only intended to reorder the React lifecycle, I think moving from a In any case, I think adding a |
||
video.removeEventListener('loadedmetadata', handleResize); | ||
video.removeEventListener('resize', handleResize); | ||
}; | ||
}, | ||
[onResize, videoTrack] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
[onResize] | ||
); | ||
|
||
return ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe
video
would have always been defined at this point. Instead, I added another check forvideoEl.current
further down.