fix async race condition that caused pool.publish() callbacks to not be called.
fixes https://github.com/nbd-wtf/nostr-tools/issues/169
This commit is contained in:
parent
f1eb9a3bc7
commit
9ee58bd6c7
30
pool.ts
30
pool.ts
|
@ -159,22 +159,36 @@ export class SimplePool {
|
|||
}
|
||||
|
||||
publish(relays: string[], event: Event): Pub {
|
||||
const pubs: Pub[] = []
|
||||
relays.forEach(async relay => {
|
||||
const pubPromises: Promise<Pub>[] = relays.map(async relay => {
|
||||
let r
|
||||
try {
|
||||
r = await this.ensureRelay(relay)
|
||||
pubs.push(r.publish(event))
|
||||
} catch (_) {}
|
||||
return r.publish(event)
|
||||
} catch (_) {
|
||||
return {on() {}, off() {}}
|
||||
}
|
||||
})
|
||||
|
||||
const callbackMap = new Map()
|
||||
|
||||
return {
|
||||
on(type, cb) {
|
||||
pubs.forEach((pub, i) => {
|
||||
pub.on(type, () => cb(relays[i]))
|
||||
relays.forEach(async (relay, i) => {
|
||||
let pub = await pubPromises[i]
|
||||
let callback = () => cb(relay)
|
||||
callbackMap.set(cb, callback)
|
||||
pub.on(type, callback)
|
||||
})
|
||||
},
|
||||
off() {
|
||||
// do nothing here, FIXME
|
||||
|
||||
off(type, cb) {
|
||||
relays.forEach(async (_, i) => {
|
||||
let callback = callbackMap.get(cb)
|
||||
if (callback) {
|
||||
let pub = await pubPromises[i]
|
||||
pub.off(type, callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue