From 5889552b7bee362ecfeabd9554e4adbc3f70c5a9 Mon Sep 17 00:00:00 2001 From: Ganesh Jangir Date: Mon, 26 Jun 2023 13:59:24 +0200 Subject: [PATCH] RUMM-3151 convert for to compactMap --- Sources/Datadog/RUM/RUMV2Configuration.swift | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Sources/Datadog/RUM/RUMV2Configuration.swift b/Sources/Datadog/RUM/RUMV2Configuration.swift index 201b365786..29d25f1d11 100644 --- a/Sources/Datadog/RUM/RUMV2Configuration.swift +++ b/Sources/Datadog/RUM/RUMV2Configuration.swift @@ -42,30 +42,27 @@ internal struct RUMViewEventsFilter: EventsFilter { func filter(events: [Event]) -> [Event] { var seen = Set() - var filtered = [Event]() // reversed is O(1) and no copy because it is view on the original array - for event in events.reversed() { + let filtered = events.reversed().compactMap { event in guard let metadata = event.metadata else { // If there is no metadata, we can't filter it. - filtered.append(event) - continue + return event } guard let viewMetadata = try? decoder.decode(RUMViewEvent.Metadata.self, from: metadata) else { // If we can't decode the metadata, we can't filter it. - filtered.append(event) - continue + return event } guard seen.contains(viewMetadata.id) == false else { // If we've already seen this view, we can skip this DD.logger.debug("Skipping RUMViewEvent with id: \(viewMetadata.id)") - continue + return nil } - filtered.append(event) seen.insert(viewMetadata.id) + return event } return filtered.reversed()