mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-08 16:28:49 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c362212778 | ||
|
|
a8938a3a0f | ||
|
|
a21329da3f |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "nostr-tools",
|
||||
"version": "1.6.2",
|
||||
"version": "1.7.0",
|
||||
"description": "Tools for making a Nostr client.",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
||||
29
pool.ts
29
pool.ts
@@ -8,8 +8,13 @@ export class SimplePool {
|
||||
private _conn: {[url: string]: Relay}
|
||||
private _seenOn: {[id: string]: Set<string>} = {} // a map of all events we've seen in each relay
|
||||
|
||||
constructor() {
|
||||
private eoseSubTimeout: number
|
||||
private getTimeout: number
|
||||
|
||||
constructor(options: {eoseSubTimeout?: number; getTimeout?: number} = {}) {
|
||||
this._conn = {}
|
||||
this.eoseSubTimeout = options.eoseSubTimeout || 3400
|
||||
this.getTimeout = options.getTimeout || 3400
|
||||
}
|
||||
|
||||
close(relays: string[]): void {
|
||||
@@ -51,7 +56,7 @@ export class SimplePool {
|
||||
let eoseTimeout = setTimeout(() => {
|
||||
eoseSent = true
|
||||
for (let cb of eoseListeners.values()) cb()
|
||||
}, 3500)
|
||||
}, this.eoseSubTimeout)
|
||||
|
||||
relays.forEach(async relay => {
|
||||
let r
|
||||
@@ -120,7 +125,7 @@ export class SimplePool {
|
||||
let timeout = setTimeout(() => {
|
||||
sub.unsub()
|
||||
resolve(null)
|
||||
}, 3500)
|
||||
}, this.getTimeout)
|
||||
sub.on('event', (event: Event) => {
|
||||
resolve(event)
|
||||
clearTimeout(timeout)
|
||||
@@ -150,13 +155,23 @@ export class SimplePool {
|
||||
})
|
||||
}
|
||||
|
||||
publish(relays: string[], event: Event): Pub[] {
|
||||
return relays.map(relay => {
|
||||
publish(relays: string[], event: Event): Pub {
|
||||
let pubs = relays.map(relay => {
|
||||
let r = this._conn[normalizeURL(relay)]
|
||||
if (!r) return badPub(relay)
|
||||
let s = r.publish(event)
|
||||
return s
|
||||
return r.publish(event)
|
||||
})
|
||||
|
||||
return {
|
||||
on(type, cb) {
|
||||
pubs.forEach((pub, i) => {
|
||||
pub.on(type, () => cb(relays[i]))
|
||||
})
|
||||
},
|
||||
off() {
|
||||
// do nothing here, FIXME
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
seenOn(id: string): string[] {
|
||||
|
||||
5
relay.ts
5
relay.ts
@@ -175,7 +175,10 @@ export function relayInit(url: string): Relay {
|
||||
async function trySend(params: [string, ...any]) {
|
||||
let msg = JSON.stringify(params)
|
||||
if (!connected()) {
|
||||
return
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
if (!connected()) {
|
||||
return
|
||||
}
|
||||
}
|
||||
try {
|
||||
ws.send(msg)
|
||||
|
||||
Reference in New Issue
Block a user