mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
Replaces the sequential loop in `forceRefreshAllProfiles` with `batchFetchProfiles` to reduce network requests. Updates `batchFetchProfilesFromRelays` to support `forceRefresh`, ensuring fresh data is requested while maintaining optimistic UI updates. Adds unit tests for `forceRefresh` behavior. Co-authored-by: PR0M3TH3AN <[email protected]>
81 lines
2.0 KiB
JavaScript
81 lines
2.0 KiB
JavaScript
// js/utils/profileBatchFetcher.js
|
|
|
|
import { nostrClient } from "../nostrClientFacade.js";
|
|
import { devLogger } from "./logger.js";
|
|
import { fetchProfileMetadataBatch } from "../services/profileMetadataService.js";
|
|
import { HEX64_REGEX } from "./hex.js";
|
|
|
|
const DEFAULT_PROFILE_IMAGE = "assets/svg/default-profile.svg";
|
|
|
|
|
|
export async function batchFetchProfilesFromRelays({
|
|
authorSet,
|
|
forceRefresh = false,
|
|
getProfileCacheEntry,
|
|
setProfileCacheEntry,
|
|
updateProfileInDOM,
|
|
nostr = nostrClient,
|
|
logger = devLogger,
|
|
hex64Regex = HEX64_REGEX,
|
|
defaultProfileImage = DEFAULT_PROFILE_IMAGE,
|
|
} = {}) {
|
|
if (typeof getProfileCacheEntry !== "function") {
|
|
throw new TypeError("getProfileCacheEntry callback is required");
|
|
}
|
|
if (typeof setProfileCacheEntry !== "function") {
|
|
throw new TypeError("setProfileCacheEntry callback is required");
|
|
}
|
|
if (typeof updateProfileInDOM !== "function") {
|
|
throw new TypeError("updateProfileInDOM callback is required");
|
|
}
|
|
|
|
const pubkeys = Array.isArray(authorSet)
|
|
? authorSet
|
|
: Array.from(authorSet || []);
|
|
if (!pubkeys.length) {
|
|
return;
|
|
}
|
|
|
|
const toFetch = [];
|
|
|
|
pubkeys.forEach((pubkey) => {
|
|
const normalizedPubkey =
|
|
typeof pubkey === "string" ? pubkey.trim() : "";
|
|
const hasValidPubkey =
|
|
normalizedPubkey && hex64Regex.test(normalizedPubkey);
|
|
|
|
if (!hasValidPubkey) {
|
|
return;
|
|
}
|
|
|
|
const cacheEntry = getProfileCacheEntry(normalizedPubkey);
|
|
if (cacheEntry) {
|
|
updateProfileInDOM(normalizedPubkey, cacheEntry.profile);
|
|
if (forceRefresh) {
|
|
toFetch.push(normalizedPubkey);
|
|
}
|
|
} else {
|
|
toFetch.push(normalizedPubkey);
|
|
}
|
|
});
|
|
|
|
if (!toFetch.length) {
|
|
return;
|
|
}
|
|
|
|
const profileResults = await fetchProfileMetadataBatch({
|
|
pubkeys: toFetch,
|
|
nostr,
|
|
logger,
|
|
defaultProfileImage,
|
|
});
|
|
|
|
profileResults.forEach((entry, pubkey) => {
|
|
if (!entry?.profile) {
|
|
return;
|
|
}
|
|
setProfileCacheEntry(pubkey, entry.profile);
|
|
updateProfileInDOM(pubkey, entry.profile);
|
|
});
|
|
}
|