-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
WebSocket is closed before the connection is established
warni…
…ng (#8042)
- Loading branch information
Showing
5 changed files
with
28 additions
and
29 deletions.
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) | ||
} |