Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up unecessary subscriptions in PagedRelaySubscription #1205

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Nos/Models/RelaySubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import Logger

/// Models a request to a relay for Nostr Events.
struct RelaySubscription: Identifiable {
struct RelaySubscription: Identifiable, Hashable {

var id: String

Expand Down
14 changes: 9 additions & 5 deletions Nos/Service/Relay/PagedRelaySubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class PagedRelaySubscription {
private var subscriptionManager: RelaySubscriptionManager

/// A set of subscriptions fetching older events.
private var pagedSubscriptionIDs = [RelaySubscription.ID]()
private var pagedSubscriptionIDs = Set<RelaySubscription.ID>()

/// A set of subscriptions always listening for new events published after the `startDate`.
private var newEventsSubscriptionIDs = [RelaySubscription.ID]()
private var newEventsSubscriptionIDs = Set<RelaySubscription.ID>()

init(
startDate: Date,
Expand All @@ -36,10 +36,10 @@ class PagedRelaySubscription {
var newEventsFilter = filter
newEventsFilter.since = startDate
for relayAddress in relayAddresses {
newEventsSubscriptionIDs.append(
newEventsSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: filter, to: relayAddress)
)
pagedSubscriptionIDs.append(
pagedSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: pagedEventsFilter, to: relayAddress)
)
}
Expand All @@ -61,6 +61,7 @@ class PagedRelaySubscription {
func loadMore() {
Task { [self] in
var newUntilDates = [URL: Date]()
var subscriptionsToRemove = Set<RelaySubscription.ID>()

for subscriptionID in pagedSubscriptionIDs {
if let subscription = await subscriptionManager.subscription(from: subscriptionID),
Expand All @@ -73,13 +74,16 @@ class PagedRelaySubscription {

newUntilDates[subscription.relayAddress] = newDate
await subscriptionManager.decrementSubscriptionCount(for: subscriptionID)
subscriptionsToRemove.insert(subscription.id)
}
}

pagedSubscriptionIDs.subtract(subscriptionsToRemove)

for (relayAddress, until) in newUntilDates {
var newEventsFilter = self.filter
newEventsFilter.until = until
pagedSubscriptionIDs.append(
pagedSubscriptionIDs.insert(
await subscriptionManager.queueSubscription(with: newEventsFilter, to: relayAddress)
)
}
Expand Down
Loading