fix: return immediately from publish event when published to at least one relay and keep publishing to other in background

This commit is contained in:
SH 2024-07-09 17:17:57 +05:00
parent 0aaa20092e
commit 7df6ab8c84
2 changed files with 24 additions and 15 deletions

View File

@ -223,29 +223,31 @@ export class NostrController extends EventEmitter {
/** /**
* Function will publish provided event to the provided relays * Function will publish provided event to the provided relays
*
* @param event - The event to publish.
* @param relays - An array of relay URLs to publish the event to.
* @returns A promise that resolves to an array of relays where the event was successfully published.
*/ */
publishEvent = async (event: Event, relays: string[]) => { publishEvent = async (event: Event, relays: string[]) => {
const simplePool = new SimplePool() const simplePool = new SimplePool()
// Publish the event to all relays
const promises = simplePool.publish(relays, event) const promises = simplePool.publish(relays, event)
const results = await Promise.allSettled(promises) // Use Promise.race to wait for the first successful publish
const firstSuccessfulPublish = await Promise.race(
promises.map((promise, index) =>
promise.then(() => relays[index]).catch(() => null)
)
)
const publishedRelays: string[] = [] if (!firstSuccessfulPublish) {
// If no publish was successful, collect the reasons for failures
console.log('results of publish event :>> ', results)
results.forEach((result, index) => {
if (result.status === 'fulfilled') {
publishedRelays.push(relays[index])
}
})
if (publishedRelays.length === 0) {
const failedPublishes: any[] = [] const failedPublishes: any[] = []
const fallbackRejectionReason = const fallbackRejectionReason =
'Attempt to publish an event has been rejected with unknown reason.' 'Attempt to publish an event has been rejected with unknown reason.'
const results = await Promise.allSettled(promises)
results.forEach((res, index) => { results.forEach((res, index) => {
if (res.status === 'rejected') { if (res.status === 'rejected') {
failedPublishes.push({ failedPublishes.push({
@ -260,7 +262,14 @@ export class NostrController extends EventEmitter {
throw failedPublishes throw failedPublishes
} }
return publishedRelays // Continue publishing to other relays in the background
promises.forEach((promise, index) => {
promise.catch((err) => {
console.log(`Failed to publish to ${relays[index]}`, err)
})
})
return [firstSuccessfulPublish]
} }
/** /**

View File

@ -562,7 +562,7 @@ export const updateUsersAppData = async (meta: Meta) => {
console.log(`publishing event kind: ${kinds.Application}`) console.log(`publishing event kind: ${kinds.Application}`)
const publishResult = await Promise.race([ const publishResult = await Promise.race([
nostrController.publishEvent(signedEvent, writeRelays), nostrController.publishEvent(signedEvent, writeRelays),
timeout(1000 * 60 * 2) timeout(1000 * 30)
]).catch((err) => { ]).catch((err) => {
console.log('err :>> ', err) console.log('err :>> ', err)
if (err.message === 'Timeout') { if (err.message === 'Timeout') {
@ -906,7 +906,7 @@ export const sendNotification = async (receiver: string, meta: Meta) => {
// Attempt to publish the event to the relays, with a timeout of 2 minutes // Attempt to publish the event to the relays, with a timeout of 2 minutes
await Promise.race([ await Promise.race([
nostrController.publishEvent(wrappedEvent, relaySet.read), nostrController.publishEvent(wrappedEvent, relaySet.read),
timeout(1000 * 60 * 2) timeout(1000 * 30)
]).catch((err) => { ]).catch((err) => {
// Log an error if publishing the notification event fails // Log an error if publishing the notification event fails
console.log( console.log(