Change subscribe to simply return an unsubscribe function

This commit is contained in:
Jonathan Staab 2023-07-08 11:23:30 -07:00
parent d137456c16
commit ebf6f1dc9a
7 changed files with 24 additions and 37 deletions

View File

@ -72,7 +72,7 @@ const load = ({
}
if (isDone || force) {
subPromise.then(sub => sub.unsub())
unsubscribe()
resolve(allEvents)
completed = true
}
@ -81,7 +81,7 @@ const load = ({
// If a relay takes too long, give up
setTimeout(() => attemptToComplete(true), timeout)
const subPromise = network.subscribe({
const unsubscribe = network.subscribe({
relays,
filter,
shouldProcess,
@ -283,7 +283,7 @@ const streamContext = ({notes, onChunk, maxDepth = 2}) => {
events.forEach(e => seen.add(e.id))
// Unsubscribe our current listeners since we're about to replace them
subs.map(sub => sub.then(s => s.unsub()))
subs.map(unsubscribe => unsubscribe())
// Add a subscription for each chunk to listen for new likes/replies/zaps
chunk(256, Array.from(seen)).forEach(ids => {
@ -329,7 +329,7 @@ const streamContext = ({notes, onChunk, maxDepth = 2}) => {
return {
unsub: () => {
subs.map(sub => sub.then(s => s.unsub()))
subs.map(unsubscribe => unsubscribe())
},
}
}

View File

@ -26,7 +26,7 @@
export let hideControls = false
export let onEvent = null
let sub, scroller, cursor
let unsubscribe, scroller, cursor
let key = Math.random()
let search = ""
let notes = []
@ -153,7 +153,7 @@
notesBuffer = []
scroller?.stop()
feedScroller?.stop()
sub?.then(s => s?.unsub())
unsubscribe?.()
key = Math.random()
}
@ -169,7 +169,7 @@
// No point in subscribing if we have an end date
if (!filter.until) {
sub = network.subscribe({
unsubscribe = network.subscribe({
relays: getRelays(),
filter: compileFilter({...filter, since}),
onEvent: batch(500, chunk => {

View File

@ -31,14 +31,10 @@
}
onMount(() => {
const sub = network.subscribe({
return network.subscribe({
relays: getRelays(),
filter: [{kinds: [42], "#e": [id]}],
})
return () => {
sub.then(s => s.unsub())
}
})
document.title = $channel.name || "Coracle Chat"

View File

@ -26,14 +26,10 @@
document.title = "Chat"
onMount(() => {
const sub = network.subscribe({
return network.subscribe({
relays: routing.getUserHints(3, "read"),
filter: [{kinds: [40, 41]}],
})
return () => {
sub.then(s => s?.unsub())
}
})
</script>

View File

@ -33,17 +33,13 @@
}
onMount(() => {
const sub = network.subscribe({
return network.subscribe({
relays: getRelays(),
filter: [
{kinds: [4], authors: [keys.getPubkey()], "#p": [pubkey]},
{kinds: [4], authors: [pubkey], "#p": [keys.getPubkey()]},
],
})
return () => {
sub.then(s => s.unsub())
}
})
document.title = `DMs with ${directory.displayProfile($profile)}`

View File

@ -13,7 +13,7 @@
export let note
let sub
let unsubscribe
let zap = {
amount: settings.getSetting("defaultZap"),
message: "",
@ -69,7 +69,7 @@
}
// Listen for the zap confirmation
sub = network.subscribe({
unsubscribe = network.subscribe({
relays,
filter: {
kinds: [9735],
@ -85,7 +85,7 @@
}
onDestroy(() => {
sub?.then(s => s.unsub())
unsubscribe?.()
})
</script>

View File

@ -106,6 +106,7 @@ export default class Network extends EventEmitter {
const {limitation} = this.routing.getRelayMeta(socket.url)
const waitForBoot = limitation?.payment_required || limitation?.auth_required
// This happens automatically, but kick it off anyway
socket.connect()
// Delay REQ/EVENT until AUTH flow happens. Highly hacky, as this relies on
@ -122,9 +123,9 @@ export default class Network extends EventEmitter {
return executor
}
publish = async ({relays, event, onProgress, timeout = 3000, verb = "EVENT"}) => {
publish = ({relays, event, onProgress, timeout = 3000, verb = "EVENT"}) => {
const urls = getUrls(relays)
const executor = await this.getExecutor(urls, {bypassBoot: verb === "AUTH"})
const executor = this.getExecutor(urls, {bypassBoot: verb === "AUTH"})
this.emit("publish", urls)
@ -184,9 +185,9 @@ export default class Network extends EventEmitter {
attemptToResolve()
})
}
subscribe = async ({relays, filter, onEvent, onEose, shouldProcess = true}: SubscribeOpts) => {
subscribe = ({relays, filter, onEvent, onEose, shouldProcess = true}: SubscribeOpts) => {
const urls = getUrls(relays)
const executor = await this.getExecutor(urls)
const executor = this.getExecutor(urls)
const filters = ensurePlural(filter)
const now = Date.now()
const seen = new Map()
@ -247,20 +248,18 @@ export default class Network extends EventEmitter {
},
})
return {
unsub: () => {
log(`Closing subscription`, filters)
return () => {
log(`Closing subscription`, filters)
sub.unsubscribe()
executor.target.cleanup()
sub.unsubscribe()
executor.target.cleanup()
this.emit("sub:close", urls)
},
this.emit("sub:close", urls)
}
}
count = async filter => {
const filters = ensurePlural(filter)
const executor = await this.getExecutor(COUNT_RELAYS)
const executor = this.getExecutor(COUNT_RELAYS)
return new Promise(resolve => {
const sub = executor.count(filters, {