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()
|
> = new WeakMap()
|
||||||
const emitter = new EventEmitter()
|
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<
|
export function useProfileShadow<
|
||||||
TProfileView extends bsky.profile.AnyProfileView,
|
TProfileView extends bsky.profile.AnyProfileView,
|
||||||
>(profile: TProfileView): Shadow<TProfileView> {
|
>(profile: TProfileView): Shadow<TProfileView> {
|
||||||
@@ -206,10 +217,11 @@ export function updateProfileShadow(
|
|||||||
}
|
}
|
||||||
batchedUpdates(() => {
|
batchedUpdates(() => {
|
||||||
emitter.emit(did, value)
|
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,
|
profile: TProfileView,
|
||||||
shadow: Partial<ProfileShadow>,
|
shadow: Partial<ProfileShadow>,
|
||||||
): Shadow<TProfileView> {
|
): Shadow<TProfileView> {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
isNetworkError,
|
isNetworkError,
|
||||||
} from '#/lib/strings/errors'
|
} from '#/lib/strings/errors'
|
||||||
import {Logger} from '#/logger'
|
import {Logger} from '#/logger'
|
||||||
|
import {mergeShadow, type ProfileShadow} from '#/state/cache/profile-shadow'
|
||||||
import {
|
import {
|
||||||
ACTIVE_POLL_INTERVAL,
|
ACTIVE_POLL_INTERVAL,
|
||||||
BACKGROUND_POLL_INTERVAL,
|
BACKGROUND_POLL_INTERVAL,
|
||||||
@@ -131,6 +132,19 @@ export class Convo {
|
|||||||
recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined
|
recipients: ChatBskyActorDefs.ProfileViewBasic[] | undefined
|
||||||
snapshot: ConvoState | 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) {
|
constructor(params: ConvoParams) {
|
||||||
this.id = nanoid(3)
|
this.id = nanoid(3)
|
||||||
this.convoId = params.convoId
|
this.convoId = params.convoId
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {useFocusEffect} from '@react-navigation/native'
|
|||||||
import {useQueryClient} from '@tanstack/react-query'
|
import {useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {useAppState} from '#/lib/appState'
|
import {useAppState} from '#/lib/appState'
|
||||||
|
import {useOnUpdateProfileShadow} from '#/state/cache/profile-shadow'
|
||||||
import {Convo} from '#/state/messages/convo/agent'
|
import {Convo} from '#/state/messages/convo/agent'
|
||||||
import {
|
import {
|
||||||
type ConvoParams,
|
type ConvoParams,
|
||||||
@@ -112,6 +113,10 @@ export function ConvoProvider({
|
|||||||
}, [isActive, convo, convoId, markAsRead]),
|
}, [isActive, convo, convoId, markAsRead]),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
useOnUpdateProfileShadow(({did, shadow}) => {
|
||||||
|
convo.mergeProfileShadow(did, shadow)
|
||||||
|
})
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
return convo.on(event => {
|
return convo.on(event => {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
|
|||||||
Reference in New Issue
Block a user