Merge pull request #2131 from PR0M3TH3AN/optimistic-publish-relays-4183170839738909569

⚡ Make relay publishing optimistic (wait for first success)
This commit is contained in:
thePR0M3TH3AN authored and GitHub committed 2026-02-05 16:08:04 -05:00
commit 151e2a70d8
2 files changed
+59 -3

No files matched your search

+6 -1
View File
@@ -1460,7 +1460,12 @@ class WatchHistoryManager {
devLogger.warn("[nostr] Failed to sign watch history event:", error);
return { ok: false, error: "signing-failed", retryable: false };
}
const publishResults = await publishEventToRelays(pool, relays, signedEvent);
const publishResults = await publishEventToRelays(
pool,
relays,
signedEvent,
{ waitForAll: true },
);
const relayStatus = formatRelayStatus(publishResults);
const acceptedCount = relayStatus.filter((entry) => entry.success).length;
let anyRejected = false;
+53 -2
View File
@@ -156,9 +156,60 @@ export function publishEventToRelay(pool, url, event, options = {}) {
export function publishEventToRelays(pool, urls, event, options = {}) {
const list = Array.isArray(urls) ? urls : [];
return Promise.all(
list.map((url) => publishEventToRelay(pool, url, event, options)),
if (list.length === 0) {
return Promise.resolve([]);
}
const promises = list.map((url) =>
publishEventToRelay(pool, url, event, options),
);
if (options.waitForAll === true) {
return Promise.all(promises);
}
return new Promise((resolve) => {
let resolved = false;
let pending = promises.length;
const results = [];
promises.forEach((p) => {
p.then(
(result) => {
// If we already resolved, we don't need to track further results
// for the return value, but the promises will still settle in background.
if (resolved) {
return;
}
results.push(result);
pending--;
if (result.success) {
resolved = true;
resolve(results);
} else if (pending === 0) {
resolved = true;
resolve(results);
}
},
(error) => {
if (resolved) {
return;
}
// Treat rejection as a failure result so we don't hang
results.push({ success: false, error });
pending--;
if (pending === 0) {
resolved = true;
resolve(results);
}
},
);
});
});
}
export function summarizePublishResults(results = []) {