From 9b9ecf76d6d49eb1e8f22a13519d88ac8d6caaf9 Mon Sep 17 00:00:00 2001 From: codytseng Date: Sun, 9 Nov 2025 18:53:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=92=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/providers/UserTrustProvider.tsx | 4 +-- src/services/client.service.ts | 43 ++++++++++++++++++++-------- src/services/custom-emoji.service.ts | 2 +- 3 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/providers/UserTrustProvider.tsx b/src/providers/UserTrustProvider.tsx index e01d212..e4bc333 100644 --- a/src/providers/UserTrustProvider.tsx +++ b/src/providers/UserTrustProvider.tsx @@ -41,7 +41,7 @@ export function UserTrustProvider({ children }: { children: React.ReactNode }) { if (!currentPubkey) return const initWoT = async () => { - const followings = await client.fetchFollowings(currentPubkey) + const followings = await client.fetchFollowings(currentPubkey, false) followings.forEach((pubkey) => wotSet.add(pubkey)) const batchSize = 20 @@ -49,7 +49,7 @@ export function UserTrustProvider({ children }: { children: React.ReactNode }) { const batch = followings.slice(i, i + batchSize) await Promise.allSettled( batch.map(async (pubkey) => { - const _followings = await client.fetchFollowings(pubkey) + const _followings = await client.fetchFollowings(pubkey, false) _followings.forEach((following) => { wotSet.add(following) }) diff --git a/src/services/client.service.ts b/src/services/client.service.ts index 7d940e6..9042a26 100644 --- a/src/services/client.service.ts +++ b/src/services/client.service.ts @@ -963,11 +963,13 @@ class ClientService extends EventTarget { /** =========== Followings =========== */ 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++) { if (signal.aborted) return 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)) } @@ -1078,7 +1080,11 @@ class ClientService extends EventTarget { return results.map((res) => (res.status === 'fulfilled' ? res.value : null)) }) - async fetchProfile(id: string, skipCache = false): Promise { + async fetchProfile( + id: string, + skipCache = false, + updateCacheInBackground = true + ): Promise { if (skipCache) { return this._fetchProfile(id) } @@ -1086,7 +1092,9 @@ class ClientService extends EventTarget { const pubkey = userIdToPubkey(id, true) const localProfileEvent = await indexedDb.getReplaceableEvent(pubkey, kinds.Metadata) if (localProfileEvent) { - this.profileDataloader.load(id) // update cache in background + if (updateCacheInBackground) { + this.profileDataloader.load(id) // update cache in background + } const localProfile = getProfileFromEvent(localProfileEvent) 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) 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 } @@ -1335,12 +1350,12 @@ class ClientService extends EventTarget { /** =========== Replaceable event =========== */ - async fetchFollowListEvent(pubkey: string) { - return await this.fetchReplaceableEvent(pubkey, kinds.Contacts) + async fetchFollowListEvent(pubkey: string, updateCache = true) { + return await this.fetchReplaceableEvent(pubkey, kinds.Contacts, undefined, updateCache) } - async fetchFollowings(pubkey: string) { - const followListEvent = await this.fetchFollowListEvent(pubkey) + async fetchFollowings(pubkey: string, updateCache = true) { + const followListEvent = await this.fetchFollowListEvent(pubkey, updateCache) return followListEvent ? getPubkeysFromPTags(followListEvent.tags) : [] } @@ -1377,7 +1392,7 @@ class ClientService extends EventTarget { await this.updateReplaceableEventCache(evt) } - async fetchEmojiSetEvents(pointers: string[]) { + async fetchEmojiSetEvents(pointers: string[], updateCacheInBackground = true) { const params = pointers .map((pointer) => { const [kindStr, pubkey, d = ''] = pointer.split(':') @@ -1389,7 +1404,11 @@ class ClientService extends EventTarget { return { pubkey, kind, d } }) .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 ================= diff --git a/src/services/custom-emoji.service.ts b/src/services/custom-emoji.service.ts index d7f0811..3a30fc5 100644 --- a/src/services/custom-emoji.service.ts +++ b/src/services/custom-emoji.service.ts @@ -29,7 +29,7 @@ class CustomEmojiService { const { emojis, emojiSetPointers } = getEmojisAndEmojiSetsFromEvent(userEmojiListEvent) await this.addEmojisToIndex(emojis) - const emojiSetEvents = await client.fetchEmojiSetEvents(emojiSetPointers) + const emojiSetEvents = await client.fetchEmojiSetEvents(emojiSetPointers, false) await Promise.allSettled( emojiSetEvents.map(async (event) => { if (!event || event instanceof Error) return