mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-26 09:05:20 +00:00
Always queue subscriptions and flush on connection; avoid immediate sends
- Prevents early send/cancel churn at launch and during reconnects - If relays are already connected, flush immediately; otherwise pending until connected
This commit is contained in:
@@ -226,9 +226,6 @@ class NostrRelayManager: ObservableObject {
|
|||||||
}
|
}
|
||||||
messageHandlers[id] = handler
|
messageHandlers[id] = handler
|
||||||
|
|
||||||
SecureLogger.log("📡 Subscribing to Nostr filter id=\(id) kinds=\(filter.kinds ?? []) since=\(filter.since ?? 0)",
|
|
||||||
category: SecureLogger.session, level: .debug)
|
|
||||||
|
|
||||||
let req = NostrRequest.subscribe(id: id, filters: [filter])
|
let req = NostrRequest.subscribe(id: id, filters: [filter])
|
||||||
|
|
||||||
do {
|
do {
|
||||||
@@ -244,63 +241,23 @@ class NostrRelayManager: ObservableObject {
|
|||||||
// Target specific relays if provided; else default. Filter permanently failed relays.
|
// Target specific relays if provided; else default. Filter permanently failed relays.
|
||||||
let baseUrls = relayUrls ?? Self.defaultRelays
|
let baseUrls = relayUrls ?? Self.defaultRelays
|
||||||
let urls = baseUrls.filter { !isPermanentlyFailed($0) }
|
let urls = baseUrls.filter { !isPermanentlyFailed($0) }
|
||||||
// If no connections exist yet (e.g., app launch), avoid initiating connections
|
// Always queue subscriptions; sending happens when a relay reports connected
|
||||||
// from subscribe. Just ensure relays are listed and queue subs to flush later.
|
let existingSet = Set(relays.map { $0.url })
|
||||||
if connections.isEmpty {
|
for url in urls where !existingSet.contains(url) {
|
||||||
let existingSet = Set(relays.map { $0.url })
|
relays.append(Relay(url: url))
|
||||||
for url in urls where !existingSet.contains(url) {
|
|
||||||
relays.append(Relay(url: url))
|
|
||||||
}
|
|
||||||
for url in urls {
|
|
||||||
var map = self.pendingSubscriptions[url] ?? [:]
|
|
||||||
map[id] = messageString
|
|
||||||
self.pendingSubscriptions[url] = map
|
|
||||||
}
|
|
||||||
SecureLogger.log("📋 Queued subscription id=\(id) for \(urls.count) relay(s)",
|
|
||||||
category: SecureLogger.session, level: .debug)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
ensureConnections(to: urls)
|
for url in urls {
|
||||||
let targets: [(String, URLSessionWebSocketTask)] = urls.compactMap { url in
|
var map = self.pendingSubscriptions[url] ?? [:]
|
||||||
connections[url].map { (url, $0) }
|
map[id] = messageString
|
||||||
|
self.pendingSubscriptions[url] = map
|
||||||
}
|
}
|
||||||
|
SecureLogger.log("📋 Queued subscription id=\(id) for \(urls.count) relay(s)",
|
||||||
for (relayUrl, connection) in targets {
|
category: SecureLogger.session, level: .debug)
|
||||||
// Skip if already subscribed for this relay
|
// If some targets are already connected, flush immediately for them
|
||||||
if self.subscriptions[relayUrl]?.contains(id) == true {
|
for url in urls {
|
||||||
continue
|
if let r = relays.first(where: { $0.url == url }), r.isConnected {
|
||||||
|
flushPendingSubscriptions(for: url)
|
||||||
}
|
}
|
||||||
connection.send(.string(messageString)) { error in
|
|
||||||
if let error = error {
|
|
||||||
SecureLogger.log("❌ Failed to send subscription to \(relayUrl): \(error)",
|
|
||||||
category: SecureLogger.session, level: .error)
|
|
||||||
} else {
|
|
||||||
// SecureLogger.log("✅ Subscription '\(id)' sent to relay: \(relayUrl)",
|
|
||||||
// category: SecureLogger.session, level: .debug)
|
|
||||||
// Subscription sent successfully
|
|
||||||
Task { @MainActor in
|
|
||||||
var subs = self.subscriptions[relayUrl] ?? Set<String>()
|
|
||||||
subs.insert(id)
|
|
||||||
self.subscriptions[relayUrl] = subs
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If some target relays are not connected yet, queue desired subscriptions to flush later
|
|
||||||
let notConnected = urls.filter { connections[$0] == nil }
|
|
||||||
if !notConnected.isEmpty {
|
|
||||||
for url in notConnected {
|
|
||||||
var map = self.pendingSubscriptions[url] ?? [:]
|
|
||||||
map[id] = messageString
|
|
||||||
self.pendingSubscriptions[url] = map
|
|
||||||
}
|
|
||||||
SecureLogger.log("⚠️ Queued subscription '" + id + "' for \(notConnected.count) pending relay(s)",
|
|
||||||
category: SecureLogger.session, level: .debug)
|
|
||||||
}
|
|
||||||
if connections.isEmpty {
|
|
||||||
SecureLogger.log("⚠️ No relay connections available for subscription",
|
|
||||||
category: SecureLogger.session, level: .warning)
|
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
SecureLogger.log("❌ Failed to encode subscription request: \(error)",
|
SecureLogger.log("❌ Failed to encode subscription request: \(error)",
|
||||||
|
|||||||
Reference in New Issue
Block a user