Browse Source

Fixed reactivity inefficiency

master
silberengel 7 months ago
parent
commit
9ea1462bcb
  1. 31
      src/lib/components/Notifications.svelte
  2. 8
      src/routes/+layout.svelte
  3. 8
      src/routes/events/+page.svelte

31
src/lib/components/Notifications.svelte

@ -804,18 +804,32 @@
// Calculate relay set when recipients change // AI-NOTE: Refactored to avoid blocking $effect with async operations
// Calculate relay set when recipients change - non-blocking approach
$effect(() => { $effect(() => {
const senderPubkey = $userStore.pubkey; const senderPubkey = $userStore.pubkey;
console.log("[Relay Effect] Recipients changed:", selectedRecipients.length, "Sender:", senderPubkey?.slice(0, 8)); console.log("[Relay Effect] Recipients changed:", selectedRecipients.length, "Sender:", senderPubkey?.slice(0, 8));
if (selectedRecipients.length > 0 && senderPubkey) { if (selectedRecipients.length > 0 && senderPubkey) {
const recipientPubkeys = selectedRecipients.map(r => { // Start async relay set calculation without blocking the effect
updateRelaySet(selectedRecipients, senderPubkey);
} else {
console.log("[Relay Effect] Clearing relays - no recipients or sender");
newMessageRelays = [];
}
});
/**
* Updates relay set asynchronously to avoid blocking the reactive system
*/
async function updateRelaySet(recipients: any[], senderPubkey: string) {
try {
const recipientPubkeys = recipients.map(r => {
const pubkey = r.pubkey!; const pubkey = r.pubkey!;
// Convert npub to hex if needed // Convert npub to hex if needed
if (pubkey.startsWith('npub')) { if (pubkey.startsWith('npub')) {
try { try {
const decoded = nip19.decode(pubkey); const decoded = nip19.decode(pubkey) as unknown as { type: string; data: string };
if (decoded.type === 'npub') { if (decoded.type === 'npub') {
return decoded.data; return decoded.data;
} }
@ -832,8 +846,9 @@
getKind24RelaySet(senderPubkey, recipientPubkey) getKind24RelaySet(senderPubkey, recipientPubkey)
); );
Promise.all(relaySetPromises).then(relaySets => { const relaySets = await Promise.all(relaySetPromises);
console.log("[Relay Effect] Received relay sets:", relaySets); console.log("[Relay Effect] Received relay sets:", relaySets);
// Combine and deduplicate all relay sets // Combine and deduplicate all relay sets
const allRelays = relaySets.flat(); const allRelays = relaySets.flat();
const uniqueRelays = [...new Set(allRelays)]; const uniqueRelays = [...new Set(allRelays)];
@ -847,17 +862,13 @@
} else { } else {
newMessageRelays = uniqueRelays; newMessageRelays = uniqueRelays;
} }
}).catch(error => { } catch (error) {
console.error("[Relay Effect] Error getting relay set:", error); console.error("[Relay Effect] Error getting relay set:", error);
console.log("[Relay Effect] Using fallback relays due to error"); console.log("[Relay Effect] Using fallback relays due to error");
const fallbackRelays = getAvailableRelays(); const fallbackRelays = getAvailableRelays();
newMessageRelays = fallbackRelays.slice(0, 5); newMessageRelays = fallbackRelays.slice(0, 5);
});
} else {
console.log("[Relay Effect] Clearing relays - no recipients or sender");
newMessageRelays = [];
} }
}); }
</script> </script>
{#if isOwnProfile && $userStore.signedIn} {#if isOwnProfile && $userStore.signedIn}

8
src/routes/+layout.svelte

@ -19,17 +19,21 @@
let summary = let summary =
"Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages."; "Alexandria is a digital library, utilizing Nostr events for curated publications and wiki pages.";
// Reactive effect to log relay configuration when stores change // AI-NOTE: Refactored to avoid blocking $effect with logging operations
$effect(() => { // Reactive effect to log relay configuration when stores change - non-blocking approach
$effect.pre(() => {
const inboxRelays = $activeInboxRelays; const inboxRelays = $activeInboxRelays;
const outboxRelays = $activeOutboxRelays; const outboxRelays = $activeOutboxRelays;
// Only log if we have relays (not empty arrays) // Only log if we have relays (not empty arrays)
if (inboxRelays.length > 0 || outboxRelays.length > 0) { if (inboxRelays.length > 0 || outboxRelays.length > 0) {
// Defer logging to avoid blocking the reactive system
requestAnimationFrame(() => {
console.log('🔌 Relay Configuration Updated:'); console.log('🔌 Relay Configuration Updated:');
console.log('📥 Inbox Relays:', inboxRelays); console.log('📥 Inbox Relays:', inboxRelays);
console.log('📤 Outbox Relays:', outboxRelays); console.log('📤 Outbox Relays:', outboxRelays);
console.log(`📊 Total: ${inboxRelays.length} inbox, ${outboxRelays.length} outbox`); console.log(`📊 Total: ${inboxRelays.length} inbox, ${outboxRelays.length} outbox`);
});
} }
}); });

8
src/routes/events/+page.svelte

@ -392,17 +392,21 @@ import CommentViewer from "$lib/components/CommentViewer.svelte";
// Reactive effect to log relay configuration when stores change // AI-NOTE: Refactored to avoid blocking $effect with logging operations
$effect(() => { // Reactive effect to log relay configuration when stores change - non-blocking approach
$effect.pre(() => {
const inboxRelays = $activeInboxRelays; const inboxRelays = $activeInboxRelays;
const outboxRelays = $activeOutboxRelays; const outboxRelays = $activeOutboxRelays;
// Only log if we have relays (not empty arrays) // Only log if we have relays (not empty arrays)
if (inboxRelays.length > 0 || outboxRelays.length > 0) { if (inboxRelays.length > 0 || outboxRelays.length > 0) {
// Defer logging to avoid blocking the reactive system
requestAnimationFrame(() => {
console.log('🔌 Events Page - Relay Configuration Updated:'); console.log('🔌 Events Page - Relay Configuration Updated:');
console.log('📥 Inbox Relays:', inboxRelays); console.log('📥 Inbox Relays:', inboxRelays);
console.log('📤 Outbox Relays:', outboxRelays); console.log('📤 Outbox Relays:', outboxRelays);
console.log(`📊 Total: ${inboxRelays.length} inbox, ${outboxRelays.length} outbox`); console.log(`📊 Total: ${inboxRelays.length} inbox, ${outboxRelays.length} outbox`);
});
} }
}); });

Loading…
Cancel
Save