Integrate shadow cache into convo agent
This commit is contained in:
committed by
Samuel Newman
parent
7379ad3c3b
commit
d3b0452401
Vendored
+13
-1
@@ -53,6 +53,17 @@ const shadows: WeakMap<
|
||||
> = new WeakMap()
|
||||
const emitter = new EventEmitter()
|
||||
|
||||
export function useOnUpdateProfileShadow(
|
||||
onUpdate: (props: {did: string; shadow: Partial<ProfileShadow>}) => void,
|
||||
) {
|
||||
useEffect(() => {
|
||||
emitter.addListener('shadow-update', onUpdate)
|
||||
return () => {
|
||||
emitter.removeListener('shadow-update', onUpdate)
|
||||
}
|
||||
}, [onUpdate])
|
||||
}
|
||||
|
||||
export function useProfileShadow<
|
||||
TProfileView extends bsky.profile.AnyProfileView,
|
||||
>(profile: TProfileView): Shadow<TProfileView> {
|
||||
@@ -206,10 +217,11 @@ export function updateProfileShadow(
|
||||
}
|
||||
batchedUpdates(() => {
|
||||
emitter.emit(did, value)
|
||||
emitter.emit('shadow-update', {did, shadow: value})
|
||||
})
|
||||
}
|
||||
|
||||
function mergeShadow<TProfileView extends bsky.profile.AnyProfileView>(
|
||||
export function mergeShadow<TProfileView extends bsky.profile.AnyProfileView>(
|
||||
profile: TProfileView,
|
||||
shadow: Partial<ProfileShadow>,
|
||||
): Shadow<TProfileView> {
|
||||
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
isNetworkError,
|
||||
} from '#/lib/strings/errors'
|
||||
import {Logger} from '#/logger'
|
||||
import {mergeShadow, type ProfileShadow} from '#/state/cache/profile-shadow'
|
||||
import {
|
||||
ACTIVE_POLL_INTERVAL,
|
||||
BACKGROUND_POLL_INTERVAL,
|
||||
@@ -131,6 +132,19 @@ export class Convo {
|
||||
recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined
|
||||
snapshot: ConvoState | undefined
|
||||
|
||||
mergeProfileShadow(did: string, shadow: Partial<ProfileShadow>) {
|
||||
this.recipients?.map((recipient, i) => {
|
||||
if (recipient.did === did) {
|
||||
const merged = mergeShadow(recipient, shadow)
|
||||
this.recipients![i] = merged
|
||||
}
|
||||
})
|
||||
const related = this.relatedProfiles.get(did)
|
||||
if (related) {
|
||||
this.relatedProfiles.set(did, mergeShadow(related, shadow))
|
||||
}
|
||||
}
|
||||
|
||||
constructor(params: ConvoParams) {
|
||||
this.id = nanoid(3)
|
||||
this.convoId = params.convoId
|
||||
|
||||
@@ -11,6 +11,7 @@ import {useFocusEffect} from '@react-navigation/native'
|
||||
import {useQueryClient} from '@tanstack/react-query'
|
||||
|
||||
import {useAppState} from '#/lib/appState'
|
||||
import {useOnUpdateProfileShadow} from '#/state/cache/profile-shadow'
|
||||
import {Convo} from '#/state/messages/convo/agent'
|
||||
import {
|
||||
type ConvoParams,
|
||||
@@ -112,6 +113,10 @@ export function ConvoProvider({
|
||||
}, [isActive, convo, convoId, markAsRead]),
|
||||
)
|
||||
|
||||
useOnUpdateProfileShadow(({did, shadow}) => {
|
||||
convo.mergeProfileShadow(did, shadow)
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
return convo.on(event => {
|
||||
switch (event.type) {
|
||||
|
||||
Reference in New Issue
Block a user