[Clipclops] External store, suspend/resume (#3829)
* Initial working external store * Clean up WIP, explore suspend/resume * Clean up state, bindings, snapshots, add some logs * Reduce snapshots, add better logic check * Bump interval a smidge * Remove unused type
This commit is contained in:
parent
c13685a0cf
commit
c9cf608f78
6 changed files with 344 additions and 172 deletions
|
@ -1,14 +1,12 @@
|
|||
import React, {useContext, useEffect, useMemo, useState} from 'react'
|
||||
import React, {useContext, useState, useSyncExternalStore} from 'react'
|
||||
import {BskyAgent} from '@atproto-labs/api'
|
||||
import {useFocusEffect} from '@react-navigation/native'
|
||||
|
||||
import {Convo, ConvoParams} from '#/state/messages/convo'
|
||||
import {Convo, ConvoParams, ConvoState} from '#/state/messages/convo'
|
||||
import {useAgent} from '#/state/session'
|
||||
import {useDmServiceUrlStorage} from '#/screens/Messages/Temp/useDmServiceUrlStorage'
|
||||
|
||||
const ChatContext = React.createContext<{
|
||||
service: Convo
|
||||
state: Convo['state']
|
||||
} | null>(null)
|
||||
const ChatContext = React.createContext<ConvoState | null>(null)
|
||||
|
||||
export function useChat() {
|
||||
const ctx = useContext(ChatContext)
|
||||
|
@ -24,7 +22,7 @@ export function ChatProvider({
|
|||
}: Pick<ConvoParams, 'convoId'> & {children: React.ReactNode}) {
|
||||
const {serviceUrl} = useDmServiceUrlStorage()
|
||||
const {getAgent} = useAgent()
|
||||
const [service] = useState(
|
||||
const [convo] = useState(
|
||||
() =>
|
||||
new Convo({
|
||||
convoId,
|
||||
|
@ -34,21 +32,17 @@ export function ChatProvider({
|
|||
__tempFromUserDid: getAgent().session?.did!,
|
||||
}),
|
||||
)
|
||||
const [state, setState] = useState(service.state)
|
||||
const service = useSyncExternalStore(convo.subscribe, convo.getSnapshot)
|
||||
|
||||
useEffect(() => {
|
||||
service.initialize()
|
||||
}, [service])
|
||||
useFocusEffect(
|
||||
React.useCallback(() => {
|
||||
convo.resume()
|
||||
|
||||
useEffect(() => {
|
||||
const update = () => setState(service.state)
|
||||
service.on('update', update)
|
||||
return () => {
|
||||
service.destroy()
|
||||
}
|
||||
}, [service])
|
||||
return () => {
|
||||
convo.background()
|
||||
}
|
||||
}, [convo]),
|
||||
)
|
||||
|
||||
const value = useMemo(() => ({service, state}), [service, state])
|
||||
|
||||
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>
|
||||
return <ChatContext.Provider value={service}>{children}</ChatContext.Provider>
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue