From 10c770317901a80bda31be707dac055ce1bbc8d8 Mon Sep 17 00:00:00 2001 From: mplorentz Date: Tue, 16 Jul 2024 13:50:40 -0400 Subject: [PATCH] Probably fix a crash in PagedRelaySubscription --- .../Relay/PagedRelaySubscription.swift | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Nos/Service/Relay/PagedRelaySubscription.swift b/Nos/Service/Relay/PagedRelaySubscription.swift index a616bee3b..8b610aa80 100644 --- a/Nos/Service/Relay/PagedRelaySubscription.swift +++ b/Nos/Service/Relay/PagedRelaySubscription.swift @@ -92,22 +92,29 @@ class PagedRelaySubscription { to: relayAddress ) - pagedEventSubscription.events.sink { [weak self] jsonEvent in - self?.track(event: jsonEvent, from: relayAddress) - } - .store(in: &cancellables) + pagedEventSubscription.events + .sink { [weak self] jsonEvent in + Task { + await self?.track(event: jsonEvent, from: relayAddress) + } + } + .store(in: &cancellables) pagedSubscriptionIDs.insert(pagedEventSubscription.id) } } } - func track(event: JSONEvent, from relay: URL) { - if let oldestSeen = oldestEventByRelay[relay], + func updateOldestEvent(for relay: URL, to date: Date) { + oldestEventByRelay[relay] = date + } + + nonisolated func track(event: JSONEvent, from relay: URL) async { + if let oldestSeen = await oldestEventByRelay[relay], event.createdDate < oldestSeen { - oldestEventByRelay[relay] = event.createdDate + await updateOldestEvent(for: relay, to: event.createdDate) } else { - oldestEventByRelay[relay] = event.createdDate + await updateOldestEvent(for: relay, to: event.createdDate) } } }