mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-09 00:28:51 +00:00
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:
30
pool.ts
30
pool.ts
@@ -159,22 +159,36 @@ export class SimplePool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
publish(relays: string[], event: Event): Pub {
|
publish(relays: string[], event: Event): Pub {
|
||||||
const pubs: Pub[] = []
|
const pubPromises: Promise<Pub>[] = relays.map(async relay => {
|
||||||
relays.forEach(async relay => {
|
|
||||||
let r
|
let r
|
||||||
try {
|
try {
|
||||||
r = await this.ensureRelay(relay)
|
r = await this.ensureRelay(relay)
|
||||||
pubs.push(r.publish(event))
|
return r.publish(event)
|
||||||
} catch (_) {}
|
} catch (_) {
|
||||||
|
return {on() {}, off() {}}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const callbackMap = new Map()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
on(type, cb) {
|
on(type, cb) {
|
||||||
pubs.forEach((pub, i) => {
|
relays.forEach(async (relay, i) => {
|
||||||
pub.on(type, () => cb(relays[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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user