feat: 💨

This commit is contained in:
codytseng
2025-11-09 18:53:04 +08:00
parent 7139211fa1
commit 9b9ecf76d6
3 changed files with 34 additions and 15 deletions

View File

@@ -41,7 +41,7 @@ export function UserTrustProvider({ children }: { children: React.ReactNode }) {
if (!currentPubkey) return if (!currentPubkey) return
const initWoT = async () => { const initWoT = async () => {
const followings = await client.fetchFollowings(currentPubkey) const followings = await client.fetchFollowings(currentPubkey, false)
followings.forEach((pubkey) => wotSet.add(pubkey)) followings.forEach((pubkey) => wotSet.add(pubkey))
const batchSize = 20 const batchSize = 20
@@ -49,7 +49,7 @@ export function UserTrustProvider({ children }: { children: React.ReactNode }) {
const batch = followings.slice(i, i + batchSize) const batch = followings.slice(i, i + batchSize)
await Promise.allSettled( await Promise.allSettled(
batch.map(async (pubkey) => { batch.map(async (pubkey) => {
const _followings = await client.fetchFollowings(pubkey) const _followings = await client.fetchFollowings(pubkey, false)
_followings.forEach((following) => { _followings.forEach((following) => {
wotSet.add(following) wotSet.add(following)
}) })

View File

@@ -963,11 +963,13 @@ class ClientService extends EventTarget {
/** =========== Followings =========== */ /** =========== Followings =========== */
async initUserIndexFromFollowings(pubkey: string, signal: AbortSignal) { async initUserIndexFromFollowings(pubkey: string, signal: AbortSignal) {
const followings = await this.fetchFollowings(pubkey) const followings = await this.fetchFollowings(pubkey, false)
for (let i = 0; i * 20 < followings.length; i++) { for (let i = 0; i * 20 < followings.length; i++) {
if (signal.aborted) return if (signal.aborted) return
await Promise.all( await Promise.all(
followings.slice(i * 20, (i + 1) * 20).map((pubkey) => this.fetchProfile(pubkey)) followings
.slice(i * 20, (i + 1) * 20)
.map((pubkey) => this.fetchProfile(pubkey, false, false))
) )
await new Promise((resolve) => setTimeout(resolve, 1000)) await new Promise((resolve) => setTimeout(resolve, 1000))
} }
@@ -1078,7 +1080,11 @@ class ClientService extends EventTarget {
return results.map((res) => (res.status === 'fulfilled' ? res.value : null)) return results.map((res) => (res.status === 'fulfilled' ? res.value : null))
}) })
async fetchProfile(id: string, skipCache = false): Promise<TProfile | null> { async fetchProfile(
id: string,
skipCache = false,
updateCacheInBackground = true
): Promise<TProfile | null> {
if (skipCache) { if (skipCache) {
return this._fetchProfile(id) return this._fetchProfile(id)
} }
@@ -1086,7 +1092,9 @@ class ClientService extends EventTarget {
const pubkey = userIdToPubkey(id, true) const pubkey = userIdToPubkey(id, true)
const localProfileEvent = await indexedDb.getReplaceableEvent(pubkey, kinds.Metadata) const localProfileEvent = await indexedDb.getReplaceableEvent(pubkey, kinds.Metadata)
if (localProfileEvent) { if (localProfileEvent) {
this.profileDataloader.load(id) // update cache in background if (updateCacheInBackground) {
this.profileDataloader.load(id) // update cache in background
}
const localProfile = getProfileFromEvent(localProfileEvent) const localProfile = getProfileFromEvent(localProfileEvent)
return localProfile return localProfile
} }
@@ -1310,10 +1318,17 @@ class ClientService extends EventTarget {
}) })
} }
private async fetchReplaceableEvent(pubkey: string, kind: number, d?: string) { private async fetchReplaceableEvent(
pubkey: string,
kind: number,
d?: string,
updateCache = true
) {
const storedEvent = await indexedDb.getReplaceableEvent(pubkey, kind, d) const storedEvent = await indexedDb.getReplaceableEvent(pubkey, kind, d)
if (storedEvent !== undefined) { if (storedEvent !== undefined) {
this.replaceableEventDataLoader.load({ pubkey, kind, d }) // update cache in background if (updateCache) {
this.replaceableEventDataLoader.load({ pubkey, kind, d }) // update cache in background
}
return storedEvent return storedEvent
} }
@@ -1335,12 +1350,12 @@ class ClientService extends EventTarget {
/** =========== Replaceable event =========== */ /** =========== Replaceable event =========== */
async fetchFollowListEvent(pubkey: string) { async fetchFollowListEvent(pubkey: string, updateCache = true) {
return await this.fetchReplaceableEvent(pubkey, kinds.Contacts) return await this.fetchReplaceableEvent(pubkey, kinds.Contacts, undefined, updateCache)
} }
async fetchFollowings(pubkey: string) { async fetchFollowings(pubkey: string, updateCache = true) {
const followListEvent = await this.fetchFollowListEvent(pubkey) const followListEvent = await this.fetchFollowListEvent(pubkey, updateCache)
return followListEvent ? getPubkeysFromPTags(followListEvent.tags) : [] return followListEvent ? getPubkeysFromPTags(followListEvent.tags) : []
} }
@@ -1377,7 +1392,7 @@ class ClientService extends EventTarget {
await this.updateReplaceableEventCache(evt) await this.updateReplaceableEventCache(evt)
} }
async fetchEmojiSetEvents(pointers: string[]) { async fetchEmojiSetEvents(pointers: string[], updateCacheInBackground = true) {
const params = pointers const params = pointers
.map((pointer) => { .map((pointer) => {
const [kindStr, pubkey, d = ''] = pointer.split(':') const [kindStr, pubkey, d = ''] = pointer.split(':')
@@ -1389,7 +1404,11 @@ class ClientService extends EventTarget {
return { pubkey, kind, d } return { pubkey, kind, d }
}) })
.filter(Boolean) as { pubkey: string; kind: number; d: string }[] .filter(Boolean) as { pubkey: string; kind: number; d: string }[]
return await this.replaceableEventDataLoader.loadMany(params) return await Promise.all(
params.map(({ pubkey, kind, d }) =>
this.fetchReplaceableEvent(pubkey, kind, d, updateCacheInBackground)
)
)
} }
// ================= Utils ================= // ================= Utils =================

View File

@@ -29,7 +29,7 @@ class CustomEmojiService {
const { emojis, emojiSetPointers } = getEmojisAndEmojiSetsFromEvent(userEmojiListEvent) const { emojis, emojiSetPointers } = getEmojisAndEmojiSetsFromEvent(userEmojiListEvent)
await this.addEmojisToIndex(emojis) await this.addEmojisToIndex(emojis)
const emojiSetEvents = await client.fetchEmojiSetEvents(emojiSetPointers) const emojiSetEvents = await client.fetchEmojiSetEvents(emojiSetPointers, false)
await Promise.allSettled( await Promise.allSettled(
emojiSetEvents.map(async (event) => { emojiSetEvents.map(async (event) => {
if (!event || event instanceof Error) return if (!event || event instanceof Error) return