Commit a120b2dd authored by Robert Knight's avatar Robert Knight

Defer initial WebSocket connection until profile is fetched

Following https://github.com/hypothesis/client/pull/2837 the
`SidebarView` component may now be rendered before the profile has been
fetched. This component contains an effect which triggers the initial
WebSocket connection. Since the WebSocket reconnects after the profile userid
changes, this was causing the initial connection to almost immediately
be disconnected if the user was logged in. Fix this by deferring the initial
connection until after the profile has been fetched.
parent 013c82e5
...@@ -123,11 +123,12 @@ function SidebarView({ ...@@ -123,11 +123,12 @@ function SidebarView({
]); ]);
// Connect to the streamer when the sidebar has opened or if user is logged in // Connect to the streamer when the sidebar has opened or if user is logged in
const hasFetchedProfile = store.hasFetchedProfile();
useEffect(() => { useEffect(() => {
if (sidebarHasOpened || isLoggedIn) { if (hasFetchedProfile && (sidebarHasOpened || isLoggedIn)) {
streamer.connect(); streamer.connect();
} }
}, [streamer, sidebarHasOpened, isLoggedIn]); }, [hasFetchedProfile, isLoggedIn, sidebarHasOpened, streamer]);
return ( return (
<div> <div>
......
...@@ -51,6 +51,7 @@ describe('SidebarView', () => { ...@@ -51,6 +51,7 @@ describe('SidebarView', () => {
focusedGroupId: sinon.stub(), focusedGroupId: sinon.stub(),
hasAppliedFilter: sinon.stub(), hasAppliedFilter: sinon.stub(),
hasFetchedAnnotations: sinon.stub(), hasFetchedAnnotations: sinon.stub(),
hasFetchedProfile: sinon.stub().returns(true),
hasSelectedAnnotations: sinon.stub(), hasSelectedAnnotations: sinon.stub(),
hasSidebarOpened: sinon.stub(), hasSidebarOpened: sinon.stub(),
isLoading: sinon.stub().returns(false), isLoading: sinon.stub().returns(false),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment