Compare commits

...

2 Commits

Author SHA1 Message Date
fiatjaf
4f0cae0eb8 add missing id arguments. 2021-12-13 21:22:23 -03:00
fiatjaf
06e867b675 stop sending repeated REQs. 2021-12-13 20:58:49 -03:00
3 changed files with 21 additions and 13 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "0.8.1", "version": "0.9.1",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -21,7 +21,7 @@ export function relayPool(globalPrivateKey) {
.filter(({policy}) => policy.read) .filter(({policy}) => policy.read)
.map(({relay}) => [ .map(({relay}) => [
relay.url, relay.url,
relay.sub({filter, cb: event => cb(event, relay.url)}) relay.sub({filter, cb: event => cb(event, relay.url)}, id)
]) ])
) )
@@ -37,7 +37,7 @@ export function relayPool(globalPrivateKey) {
return activeSubscriptions[id] return activeSubscriptions[id]
}, },
addRelay: relay => { addRelay: relay => {
subControllers[relay.url] = relay.sub({cb, filter}) subControllers[relay.url] = relay.sub({cb, filter}, id)
return activeSubscriptions[id] return activeSubscriptions[id]
}, },
removeRelay: relayURL => { removeRelay: relayURL => {

View File

@@ -13,7 +13,7 @@ export function normalizeRelayURL(url) {
export function relayConnect(url, onNotice) { export function relayConnect(url, onNotice) {
url = normalizeRelayURL(url) url = normalizeRelayURL(url)
var ws, resolveOpen, untilOpen var ws, resolveOpen, untilOpen, wasClosed
var openSubs = {} var openSubs = {}
let attemptNumber = 1 let attemptNumber = 1
let nextAttemptSeconds = 1 let nextAttemptSeconds = 1
@@ -34,12 +34,15 @@ export function relayConnect(url, onNotice) {
resolveOpen() resolveOpen()
// restablish old subscriptions // restablish old subscriptions
if (wasClosed) {
wasClosed = false
for (let channel in openSubs) { for (let channel in openSubs) {
let filters = openSubs[channel] let filters = openSubs[channel]
let cb = channels[channel] let cb = channels[channel]
sub({cb, filter: filters}, channel) sub({cb, filter: filters}, channel)
} }
} }
}
ws.onerror = () => { ws.onerror = () => {
console.log('error connecting to relay', url) console.log('error connecting to relay', url)
} }
@@ -58,6 +61,8 @@ export function relayConnect(url, onNotice) {
connect() connect()
} catch (err) {} } catch (err) {}
}, nextAttemptSeconds * 1000) }, nextAttemptSeconds * 1000)
wasClosed = true
} }
ws.onmessage = async e => { ws.onmessage = async e => {
@@ -140,12 +145,15 @@ export function relayConnect(url, onNotice) {
try { try {
await trySend(['EVENT', event]) await trySend(['EVENT', event])
statusCallback(0) statusCallback(0)
let {unsub} = relay.sub({ let {unsub} = relay.sub(
{
cb: () => { cb: () => {
statusCallback(1) statusCallback(1)
}, },
filter: {id: event.id} filter: {id: event.id}
}) },
`monitor-${event.id.slice(0, 5)}`
)
setTimeout(unsub, 5000) setTimeout(unsub, 5000)
} catch (err) { } catch (err) {
statusCallback(-1) statusCallback(-1)