-
Notifications
You must be signed in to change notification settings - Fork 432
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
fix: WebSocket is closed before the connection is established
warning
#8042
Open
stipsan
wants to merge
1
commit into
next
Choose a base branch
from
fix-websocket-disconnect-warning
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+28
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 11 additions & 11 deletions
22
packages/sanity/src/core/store/_legacy/presence/useDocumentPresence.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import {useEffect, useState} from 'react' | ||
import {startTransition, useEffect, useReducer} from 'react' | ||
import {useObservable} from 'react-rx' | ||
import {of} from 'rxjs' | ||
|
||
import {usePresenceStore} from '../datastores' | ||
import {type DocumentPresence} from './types' | ||
|
||
const initial: DocumentPresence[] = [] | ||
const fallback = of(initial) | ||
|
||
/** @internal */ | ||
export function useDocumentPresence(documentId: string): DocumentPresence[] { | ||
const presenceStore = usePresenceStore() | ||
const [presence, setPresence] = useState<DocumentPresence[]>([]) | ||
const [mounted, mount] = useReducer(() => true, false) | ||
// Using `startTransition` here ensures that rapid re-renders that affect the deps used by `usePresenceStore` delay the transition to `mounted=true`, thus avoiding creating websocket connections that will be closed immediately. | ||
useEffect(() => startTransition(mount), []) | ||
|
||
useEffect(() => { | ||
const subscription = presenceStore.documentPresence(documentId).subscribe(setPresence) | ||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, [documentId, presenceStore]) | ||
|
||
return presence | ||
const presenceStore = usePresenceStore() | ||
return useObservable(mounted ? presenceStore.documentPresence(documentId) : fallback, initial) | ||
} |
23 changes: 11 additions & 12 deletions
23
packages/sanity/src/core/store/_legacy/presence/useGlobalPresence.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,19 @@ | ||
import {useEffect, useState} from 'react' | ||
import {startTransition, useEffect, useReducer} from 'react' | ||
import {useObservable} from 'react-rx' | ||
import {of} from 'rxjs' | ||
|
||
import {usePresenceStore} from '../datastores' | ||
import {type GlobalPresence} from './types' | ||
|
||
const initial: GlobalPresence[] = [] | ||
const fallback = of(initial) | ||
|
||
/** @internal */ | ||
export function useGlobalPresence(): GlobalPresence[] { | ||
const [presence, setPresence] = useState<GlobalPresence[]>([]) | ||
const presenceStore = usePresenceStore() | ||
|
||
useEffect(() => { | ||
const subscription = presenceStore.globalPresence$.subscribe(setPresence) | ||
const [mounted, mount] = useReducer(() => true, false) | ||
// Using `startTransition` here ensures that rapid re-renders that affect the deps used by `usePresenceStore` delay the transition to `mounted=true`, thus avoiding creating websocket connections that will be closed immediately. | ||
useEffect(() => startTransition(mount), []) | ||
|
||
return () => { | ||
subscription.unsubscribe() | ||
} | ||
}, [presenceStore]) | ||
|
||
return presence | ||
const presenceStore = usePresenceStore() | ||
return useObservable(mounted ? presenceStore.globalPresence$ : fallback, initial) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A great rule of thumb for determining wether a
useMemo
is safe and side-effects free is wether you can remove it and what happens to your app:In this case it's b), as the assumption here is that
useMemo
with zero dependencies meansresourceCache
will run exactly once, and have a stable instance for hte lifecycle of components calling it. This is false. In fact, it can choose to throw away the memoized value in certain suspense scenarios: https://react.dev/reference/react/useMemo#caveatsA better solution is to use
useState
, as it does have a guarantee that it's never throwing away the value, and it has a lazy init function so that our function that creates the map is still only run once.This is also reflected in how much simpler the React Compiler optimization is for
useState
, compared to theuseMemo
.