nostr-tools
Advanced tools
Comparing version 0.17.0 to 0.18.0
{ | ||
"name": "nostr-tools", | ||
"version": "0.17.0", | ||
"version": "0.18.0", | ||
"description": "Tools for making a Nostr client.", | ||
@@ -5,0 +5,0 @@ "repository": { |
53
pool.js
@@ -9,3 +9,7 @@ import {getEventHash, signEvent} from './event' | ||
// set of relays only, instead of publishing to all relays all the time | ||
randomChoice: null | ||
randomChoice: null, | ||
// setting this to true will cause .publish() calls to wait until the event has | ||
// been published -- or at least attempted to be published -- to all relays | ||
wait: false | ||
} | ||
@@ -115,3 +119,3 @@ const relays = {} | ||
}, | ||
async publish(event, statusCallback = (status, relayURL) => {}) { | ||
async publish(event, statusCallback) { | ||
event.id = getEventHash(event) | ||
@@ -141,24 +145,33 @@ | ||
for (let i = 0; i < writeable.length; i++) { | ||
let {relay} = writeable[i] | ||
if (poolPolicy.wait) { | ||
for (let i = 0; i < writeable.length; i++) { | ||
let {relay} = writeable[i] | ||
try { | ||
await new Promise(async (resolve, reject) => { | ||
try { | ||
await relay.publish(event, status => { | ||
statusCallback(status, relay.url) | ||
resolve() | ||
}) | ||
} catch (err) { | ||
statusCallback(-1, relay.url) | ||
try { | ||
await new Promise(async (resolve, reject) => { | ||
try { | ||
await relay.publish(event, status => { | ||
if (statusCallback) statusCallback(status, relay.url) | ||
resolve() | ||
}) | ||
} catch (err) { | ||
if (statusCallback) statusCallback(-1, relay.url) | ||
} | ||
}) | ||
successes++ | ||
if (successes >= maxTargets) { | ||
break | ||
} | ||
}) | ||
successes++ | ||
if (successes >= maxTargets) { | ||
break | ||
} catch (err) { | ||
/***/ | ||
} | ||
} catch (err) { | ||
/***/ | ||
} | ||
} else { | ||
writeable.forEach(async ({relay}) => { | ||
let callback = statusCallback | ||
? status => statusCallback(status, relay.url) | ||
: null | ||
relay.publish(event, callback) | ||
}) | ||
} | ||
@@ -165,0 +178,0 @@ |
22184
655