return sub object from .sub() and other methods.

This commit is contained in:
fiatjaf
2021-12-12 06:47:49 -03:00
parent a99188e4cf
commit 02cacd4446
2 changed files with 7 additions and 3 deletions

View File

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

View File

@@ -29,19 +29,23 @@ export function relayPool(globalPrivateKey) {
const activeFilters = filter const activeFilters = filter
activeSubscriptions[id] = { activeSubscriptions[id] = {
sub: ({cb = activeCallback, filter = activeFilters}) => sub: ({cb = activeCallback, filter = activeFilters}) => {
Object.entries(subControllers).map(([relayURL, sub]) => [ Object.entries(subControllers).map(([relayURL, sub]) => [
relayURL, relayURL,
sub.sub({cb, filter}, id) sub.sub({cb, filter}, id)
]), ])
return activeSubscriptions[id]
},
addRelay: relay => { addRelay: relay => {
subControllers[relay.url] = relay.sub({cb, filter}) subControllers[relay.url] = relay.sub({cb, filter})
return activeSubscriptions[id]
}, },
removeRelay: relayURL => { removeRelay: relayURL => {
if (relayURL in subControllers) { if (relayURL in subControllers) {
subControllers[relayURL].unsub() subControllers[relayURL].unsub()
if (Object.keys(subControllers).length === 0) unsub() if (Object.keys(subControllers).length === 0) unsub()
} }
return activeSubscriptions[id]
}, },
unsub: () => { unsub: () => {
Object.values(subControllers).forEach(sub => sub.unsub()) Object.values(subControllers).forEach(sub => sub.unsub())