Fix memory race condition
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
@@ -99,6 +99,7 @@ class RelayPool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
func register_handler(sub_id: String, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
|
func register_handler(sub_id: String, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
|
||||||
for handler in handlers {
|
for handler in handlers {
|
||||||
// don't add duplicate handlers
|
// don't add duplicate handlers
|
||||||
@@ -201,9 +202,11 @@ class RelayPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func subscribe(sub_id: String, filters: [NostrFilter], handler: @escaping (RelayURL, NostrConnectionEvent) -> (), to: [RelayURL]? = nil) {
|
func subscribe(sub_id: String, filters: [NostrFilter], handler: @escaping (RelayURL, NostrConnectionEvent) -> (), to: [RelayURL]? = nil) {
|
||||||
register_handler(sub_id: sub_id, handler: handler)
|
Task {
|
||||||
|
await register_handler(sub_id: sub_id, handler: handler)
|
||||||
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
|
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Subscribes to data from the `RelayPool` based on a filter and a list of desired relays.
|
/// Subscribes to data from the `RelayPool` based on a filter and a list of desired relays.
|
||||||
///
|
///
|
||||||
@@ -264,9 +267,11 @@ class RelayPool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func subscribe_to(sub_id: String, filters: [NostrFilter], to: [RelayURL]?, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
|
func subscribe_to(sub_id: String, filters: [NostrFilter], to: [RelayURL]?, handler: @escaping (RelayURL, NostrConnectionEvent) -> ()) {
|
||||||
register_handler(sub_id: sub_id, handler: handler)
|
Task {
|
||||||
|
await register_handler(sub_id: sub_id, handler: handler)
|
||||||
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
|
send(.subscribe(.init(filters: filters, sub_id: sub_id)), to: to)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func count_queued(relay: RelayURL) -> Int {
|
func count_queued(relay: RelayURL) -> Int {
|
||||||
var c = 0
|
var c = 0
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ class PostBox {
|
|||||||
init(pool: RelayPool) {
|
init(pool: RelayPool) {
|
||||||
self.pool = pool
|
self.pool = pool
|
||||||
self.events = [:]
|
self.events = [:]
|
||||||
pool.register_handler(sub_id: "postbox", handler: handle_event)
|
Task { await pool.register_handler(sub_id: "postbox", handler: handle_event) }
|
||||||
}
|
}
|
||||||
|
|
||||||
// only works reliably on delay-sent events
|
// only works reliably on delay-sent events
|
||||||
|
|||||||
Reference in New Issue
Block a user