use ref instead of source (#8471)

This commit is contained in:
Samuel Newman
2025-06-10 23:29:39 +03:00
committed by GitHub
parent c6fc298d1e
commit 1380c5ac67
+10 -21
View File
@@ -1,4 +1,4 @@
import {createContext, useCallback, useContext, useState} from 'react'
import {createContext, useCallback, useContext, useRef, useState} from 'react'
import {type AppBskyFeedDefs} from '@atproto/api'
import {type FeedDescriptor} from './queries/post-feed'
@@ -22,30 +22,19 @@ const ConsumeUnstablePostSourceContext = createContext<
>(() => undefined)
export function Provider({children}: {children: React.ReactNode}) {
const [sources, setSources] = useState<Map<string, Source>>(() => new Map())
const sourcesRef = useRef<Map<string, Source>>(new Map())
const setUnstablePostSource = useCallback((key: string, source: Source) => {
setSources(prev => {
const newMap = new Map(prev)
newMap.set(key, source)
return newMap
})
sourcesRef.current.set(key, source)
}, [])
const consumeUnstablePostSource = useCallback(
(uri: string) => {
const source = sources.get(uri)
if (source) {
setSources(prev => {
const newMap = new Map(prev)
newMap.delete(uri)
return newMap
})
}
return source
},
[sources],
)
const consumeUnstablePostSource = useCallback((uri: string) => {
const source = sourcesRef.current.get(uri)
if (source) {
sourcesRef.current.delete(uri)
}
return source
}, [])
return (
<SetUnstablePostSourceContext.Provider value={setUnstablePostSource}>