diff --git a/DatadogSessionReplay/Sources/Feature/WebViewRecordReceiver.swift b/DatadogSessionReplay/Sources/Feature/WebViewRecordReceiver.swift index 188a3267e3..10794bdf7b 100644 --- a/DatadogSessionReplay/Sources/Feature/WebViewRecordReceiver.swift +++ b/DatadogSessionReplay/Sources/Feature/WebViewRecordReceiver.swift @@ -33,8 +33,8 @@ internal final class WebViewRecordReceiver: FeatureMessageReceiver { var event = event - if let timestamp = event["timestamp"] as? Int { - event["timestamp"] = Int64(timestamp) + self.offset(forView: view.id, context: context) + if let timestamp = event["timestamp"] as? Int, let offset = rumContext.viewServerTimeOffset { + event["timestamp"] = Int64(timestamp) + offset.toInt64Milliseconds } let record = WebRecord( @@ -54,20 +54,4 @@ internal final class WebViewRecordReceiver: FeatureMessageReceiver { return true } - - // MARK: - Time offsets - - private var offsets: [(id: String, value: Int64)] = [] - - private func offset(forView id: String, context: DatadogContext) -> Int64 { - if let found = offsets.first(where: { $0.id == id }) { - return found.value - } - - let offset = context.serverTimeOffset.toInt64Milliseconds - offsets.insert((id, offset), at: 0) - // only retain 3 offsets - offsets = Array(offsets.prefix(3)) - return offset - } } diff --git a/DatadogSessionReplay/Tests/Feature/WebViewRecordReceiverTests.swift b/DatadogSessionReplay/Tests/Feature/WebViewRecordReceiverTests.swift index f62739d346..eeda1adf4b 100644 --- a/DatadogSessionReplay/Tests/Feature/WebViewRecordReceiverTests.swift +++ b/DatadogSessionReplay/Tests/Feature/WebViewRecordReceiverTests.swift @@ -12,11 +12,15 @@ import TestUtilities class WebViewRecordReceiverTests: XCTestCase { func testGivenRUMContextAvailable_whenReceivingWebRecord_itCreatesSegment() throws { - let rumContext: RUMContext = .mockRandom() + let serverTimeOffset: TimeInterval = .mockRandom(min: -10, max: 10).rounded() + + let rumContext: RUMContext = .mockWith( + serverTimeOffset: serverTimeOffset + ) + let core = PassthroughCoreMock( context: .mockWith( source: "react-native", - serverTimeOffset: .mockRandom(min: -10, max: 10).rounded(), baggages: ["rum": FeatureBaggage(rumContext)] ) ) @@ -44,7 +48,7 @@ class WebViewRecordReceiverTests: XCTestCase { "viewID": browserViewID, "records": [ [ - "timestamp": 100_000 + core.context.serverTimeOffset.toInt64Milliseconds, + "timestamp": 100_000 + serverTimeOffset.toInt64Milliseconds, "type": 2 ].merging(random, uniquingKeysWith: { old, _ in old }) ]