Skip to content

Commit

Permalink
fix: WebSocket is closed before the connection is established warning
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Jul 29, 2024
1 parent ef01b92 commit 0bd30a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type ReactNode, useContext, useMemo} from 'react'
import {type ReactNode, useContext, useState} from 'react'
import {ResourceCacheContext} from 'sanity/_singletons'

import {createMultiKeyWeakMap, type MultiKeyWeakMap} from './createMultiKeyWeakMap'
Expand All @@ -16,7 +16,7 @@ export interface ResourceCacheProviderProps {

/** @internal */
export function ResourceCacheProvider({children}: ResourceCacheProviderProps) {
const resourceCache = useMemo((): ResourceCache => {
const [resourceCache] = useState((): ResourceCache => {
const namespaces = new Map<string, MultiKeyWeakMap>()

// this is used to replace the `null` values in any `dependencies` so that
Expand All @@ -41,7 +41,7 @@ export function ResourceCacheProvider({children}: ResourceCacheProviderProps) {
namespaceMap.set(dependenciesWithoutNull, value)
},
}
}, [])
})

return (
<ResourceCacheContext.Provider value={resourceCache}>{children}</ResourceCacheContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import {useEffect, useState} from 'react'
import {useMemo} from 'react'
import {useObservable} from 'react-rx'

import {usePresenceStore} from '../datastores'
import {type DocumentPresence} from './types'

/** @internal */
export function useDocumentPresence(documentId: string): DocumentPresence[] {
const presenceStore = usePresenceStore()
const [presence, setPresence] = useState<DocumentPresence[]>([])

useEffect(() => {
const subscription = presenceStore.documentPresence(documentId).subscribe(setPresence)
return () => {
subscription.unsubscribe()
}
}, [documentId, presenceStore])

return presence
const observable = useMemo(
() => presenceStore.documentPresence(documentId),
[documentId, presenceStore],
)
return useObservable(observable, [])
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import {useEffect, useState} from 'react'
import {useMemo} from 'react'
import {useObservable} from 'react-rx'

import {usePresenceStore} from '../datastores'
import {type GlobalPresence} from './types'

/** @internal */
export function useGlobalPresence(): GlobalPresence[] {
const [presence, setPresence] = useState<GlobalPresence[]>([])
const presenceStore = usePresenceStore()

useEffect(() => {
const subscription = presenceStore.globalPresence$.subscribe(setPresence)

return () => {
subscription.unsubscribe()
}
}, [presenceStore])

return presence
const observable = useMemo(() => presenceStore.globalPresence$, [presenceStore])
return useObservable(observable, [])
}

0 comments on commit 0bd30a5

Please sign in to comment.