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

localTracks can be captured in stale state when calling connect #667

Open
shaibt opened this issue Apr 6, 2022 · 1 comment
Open

localTracks can be captured in stale state when calling connect #667

shaibt opened this issue Apr 6, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@shaibt
Copy link

shaibt commented Apr 6, 2022

Describe the bug
The useRoom (src/components/VideoProvider/useRoom/useRoom.tsx) hook component uses a React ref (useRef) for storing the state of the connectoptions object. According to the inline comments:

useEffect(() => {
// This allows the connect function to always access the most recent version of the options object. This allows us to
// reliably use the connect function at any time.
optionsRef.current = options;
  }, [options]);

localTracks prop does not get the same treatment so potentially, although they are used as a dependency for the connect function, the localTracks state can be captured by a consuming function that does not re-render every time connect is updated.

const connect = useCallback(
token => {
  setIsConnecting(true);
  return Video.connect(token, { ...optionsRef.current, tracks: localTracks }).then(
    ...
  );
},
[localTracks, onError]
  );

IMHO, It would be safer to also store the localTracks state in the options ref so connect is indeed reliable to use at any time. Something like:

useEffect(() => {
// This allows the connect function to always access the most recent version of the options object. This allows us to
// reliably use the connect function at any time.
optionsRef.current =  { ...optionsRef.current, tracks: localTracks };
}, [options, localTracks]);

and then:

return Video.connect(token, optionsRef.current).then(
@shaibt shaibt added the bug Something isn't working label Apr 6, 2022
@timmydoza
Copy link
Contributor

Thanks for the interesting post @shaibt!

I think you raise a very good point. I'm not sure why the localTracks array doesn't get the same treatment as the options object here. It makes sense that it could be a ref too. I'm in the middle of building some new features for these apps, so I might not get around to this one for a bit, but I'll definitely give it some thought.

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants