use ref instead of source (#8471)
This commit is contained in:
@@ -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 AppBskyFeedDefs} from '@atproto/api'
|
||||||
|
|
||||||
import {type FeedDescriptor} from './queries/post-feed'
|
import {type FeedDescriptor} from './queries/post-feed'
|
||||||
@@ -22,30 +22,19 @@ const ConsumeUnstablePostSourceContext = createContext<
|
|||||||
>(() => undefined)
|
>(() => undefined)
|
||||||
|
|
||||||
export function Provider({children}: {children: React.ReactNode}) {
|
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) => {
|
const setUnstablePostSource = useCallback((key: string, source: Source) => {
|
||||||
setSources(prev => {
|
sourcesRef.current.set(key, source)
|
||||||
const newMap = new Map(prev)
|
|
||||||
newMap.set(key, source)
|
|
||||||
return newMap
|
|
||||||
})
|
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const consumeUnstablePostSource = useCallback(
|
const consumeUnstablePostSource = useCallback((uri: string) => {
|
||||||
(uri: string) => {
|
const source = sourcesRef.current.get(uri)
|
||||||
const source = sources.get(uri)
|
if (source) {
|
||||||
if (source) {
|
sourcesRef.current.delete(uri)
|
||||||
setSources(prev => {
|
}
|
||||||
const newMap = new Map(prev)
|
return source
|
||||||
newMap.delete(uri)
|
}, [])
|
||||||
return newMap
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return source
|
|
||||||
},
|
|
||||||
[sources],
|
|
||||||
)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SetUnstablePostSourceContext.Provider value={setUnstablePostSource}>
|
<SetUnstablePostSourceContext.Provider value={setUnstablePostSource}>
|
||||||
|
|||||||
Reference in New Issue
Block a user