microsoft / live-share-sdk

A framework for building collaborative Microsoft Teams and M365 experiences.
Other
94 stars 29 forks source link

Fix some hooks in React SDK not supporting `uniqueKey` changes #757

Closed ryanbliss closed 6 months ago

ryanbliss commented 6 months ago

Fixed useLiveState, useLivePresence, useLiveFollowMode, and useLiveEvent bugs that caused the uniqueKey field not fetching the correct default values when the new DDS is loaded. Other hooks do not appear to have this same issue.

Bug originally happened because I was trying to reduce what I saw as redundant renders, but I guess React already skips re-render if the value is unchanged. For useLivePresence it may actually cause duplicate re-renders, but it's better to do that than to miss getting the initial values.

image

In this image the text input is the uniqueKey field passed into the useLiveState field. Since both users have different values, the shared count displayed is different. If I switch the right to have the same value as the left, it updates to this:

image

And now if I increment the counter it updates for both clients:

image

And if I then change the id again on the left, the value updates to the correct default of 1 (but not the right, as expected):

image

Prior to this change, it would have kept the same value from the previous DDS instance. So if I were to have changed the key to "tests 2" in the same way as that last image, the default value would have been 4 rather than 1 (even though internally LiveState knew the initial value was 1.

The other hooks had similar solutions, but some like useLiveTimer and useLiveEvent needed special treatment. For that, I reset it to the defaults of undefined whenever the value changed. Still seems to work fine. For useLiveEvent I simply reset the locally tracked latestEvent and allEventsRef to their default values whenever the underlying DDS is swapped for a new one. This works fine due to the internal nuances of those DDS's.