Skip to content
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

Update useDimensionsChanged hook so that it causes re-render #387

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/hooks/useVideoTrackDimensions/useVideoTrackDimensions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ describe('the useVideoTrackDimensions hook', () => {
expect(result.current).toEqual({ height: 300, width: 600 });
});

it('should return a new object reference on "dimensionsChanged" event', () => {
mockTrack.dimensions = { height: 123, width: 456 };
const { result } = renderHook(() => useVideoTrackDimensions(mockTrack));

act(() => {
mockTrack.dimensions.height = 300;
mockTrack.dimensions.width = 600;
mockTrack.emit('dimensionsChanged', mockTrack);
});

expect(result.current).toEqual({ height: 300, width: 600 });
expect(result.current).not.toBe(mockTrack.dimensions);
});

it('should clean up listeners on unmount', () => {
const { unmount } = renderHook(() => useVideoTrackDimensions(mockTrack));
unmount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ export default function useVideoTrackDimensions(track?: TrackType) {
setDimensions(track?.dimensions);

if (track) {
const handleDimensionsChanged = (track: TrackType) => setDimensions(track?.dimensions);
const handleDimensionsChanged = (track: TrackType) => setDimensions({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the ? postfix operator on track here? We could also do something like setDimensions({ ...track?.dimensions }) so we get all of the properties like we used to, just in case something relied on any other track properties.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you need the null coaslescing, the types are wrong, so change those rather than reinstating what appears to be a redundant operator.

You're probably right about cloning the track dimensions to keep other properties, although you might have other deeply-nested objects that will suffer from the 'same reference' problem.

It might be better to specify exactly the return shape of this hook, rather than have it infer it from elsewhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need the ? postfix operator on track here?

No - it isn't needed inside the if (track) { block.

if (track) {
  // track will never be undefined in here
}

We could also do something like setDimensions({ ...track?.dimensions }) so we get all of the properties like we used to, just in case something relied on any other track properties.

We could, but width and height are the only properties on this object, so I think the code is fine as is. https://media.twiliocdn.com/sdk/js/video/releases/2.10.0/docs/VideoTrack.html#.Dimensions

width: track.dimensions.width,
height: track.dimensions.height
});
track.on('dimensionsChanged', handleDimensionsChanged);
return () => {
track.off('dimensionsChanged', handleDimensionsChanged);
Expand Down