|
|
|
@ -124,7 +124,17 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
const subscriptionId = `comments-${Date.now()}`; |
|
|
|
const subscriptionId = `comments-${Date.now()}`; |
|
|
|
const receivedEventIds = new Set<string>(); |
|
|
|
const receivedEventIds = new Set<string>(); |
|
|
|
let eoseCount = 0; |
|
|
|
let responseCount = 0; |
|
|
|
|
|
|
|
const totalRelays = uniqueRelays.length; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// AI-NOTE: Helper to check if all relays have responded and clear loading state early |
|
|
|
|
|
|
|
const checkAllResponses = () => { |
|
|
|
|
|
|
|
responseCount++; |
|
|
|
|
|
|
|
if (responseCount >= totalRelays && loading) { |
|
|
|
|
|
|
|
console.log(`[CommentLayer] All ${totalRelays} relays have responded, clearing loading state`); |
|
|
|
|
|
|
|
loading = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const fetchPromises = uniqueRelays.map(async (relayUrl) => { |
|
|
|
const fetchPromises = uniqueRelays.map(async (relayUrl) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
@ -154,6 +164,7 @@ |
|
|
|
const safeResolve = () => { |
|
|
|
const safeResolve = () => { |
|
|
|
if (!resolved) { |
|
|
|
if (!resolved) { |
|
|
|
resolved = true; |
|
|
|
resolved = true; |
|
|
|
|
|
|
|
checkAllResponses(); |
|
|
|
resolve(); |
|
|
|
resolve(); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
@ -186,8 +197,7 @@ |
|
|
|
console.log(`[CommentLayer] Added comment, total now: ${comments.length}`); |
|
|
|
console.log(`[CommentLayer] Added comment, total now: ${comments.length}`); |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (message[0] === "EOSE" && message[1] === subscriptionId) { |
|
|
|
} else if (message[0] === "EOSE" && message[1] === subscriptionId) { |
|
|
|
eoseCount++; |
|
|
|
console.log(`[CommentLayer] EOSE from ${relayUrl} (${responseCount + 1}/${totalRelays})`); |
|
|
|
console.log(`[CommentLayer] EOSE from ${relayUrl} (${eoseCount}/${uniqueRelays.length})`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close subscription and release connection |
|
|
|
// Close subscription and release connection |
|
|
|
releaseConnection(); |
|
|
|
releaseConnection(); |
|
|
|
@ -219,11 +229,16 @@ |
|
|
|
}); |
|
|
|
}); |
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
console.error(`[CommentLayer] Error connecting to ${relayUrl}:`, err); |
|
|
|
console.error(`[CommentLayer] Error connecting to ${relayUrl}:`, err); |
|
|
|
|
|
|
|
// Mark this relay as responded if connection fails |
|
|
|
|
|
|
|
checkAllResponses(); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Wait for all relays to respond or timeout |
|
|
|
// Wait for all relays to respond or timeout |
|
|
|
await Promise.all(fetchPromises); |
|
|
|
await Promise.allSettled(fetchPromises); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ensure loading is cleared even if checkAllResponses didn't fire |
|
|
|
|
|
|
|
loading = false; |
|
|
|
|
|
|
|
|
|
|
|
console.log(`[CommentLayer] Fetched ${comments.length} comments`); |
|
|
|
console.log(`[CommentLayer] Fetched ${comments.length} comments`); |
|
|
|
|
|
|
|
|
|
|
|
@ -235,8 +250,6 @@ |
|
|
|
}))); |
|
|
|
}))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
loading = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
console.error(`[CommentLayer] Error fetching comments:`, err); |
|
|
|
console.error(`[CommentLayer] Error fetching comments:`, err); |
|
|
|
loading = false; |
|
|
|
loading = false; |
|
|
|
|