diff --git a/package/ios/React/CameraView.swift b/package/ios/React/CameraView.swift index 9922d6b93a..271ddda9de 100644 --- a/package/ios/React/CameraView.swift +++ b/package/ios/React/CameraView.swift @@ -378,7 +378,7 @@ public final class CameraView: UIView, CameraSessionDelegate, FpsSampleCollector return } onAverageFpsChanged([ - "averageFps": averageFps + "averageFps": averageFps, ]) } diff --git a/package/ios/React/FpsSampleCollector.swift b/package/ios/React/FpsSampleCollector.swift index 34adf84866..c3a5a6a147 100644 --- a/package/ios/React/FpsSampleCollector.swift +++ b/package/ios/React/FpsSampleCollector.swift @@ -9,11 +9,11 @@ import Foundation class FpsSampleCollector { private var timestamps: [UInt64] = [] - private var timer: Timer? = nil - var delegate: Delegate? = nil - + private var timer: Timer? + var delegate: Delegate? + func start() { - self.timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] timer in + timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { [weak self] timer in guard let self else { timer.invalidate() return @@ -22,26 +22,26 @@ class FpsSampleCollector { delegate.onAverageFpsChanged(averageFps: self.averageFps) }) } - + func stop() { - self.timer?.invalidate() - self.timer = nil + timer?.invalidate() + timer = nil } - + /** Add a new timestamp to the FPS samples. */ func onTick() { let now = DispatchTime.now().uptimeNanoseconds timestamps.append(now) - + // filter out any timestamps that are older than 1 second - timestamps = timestamps.filter({ timestamp in + timestamps = timestamps.filter { timestamp in let differenceMs = (now - timestamp) / 1_000_000 return differenceMs < 1000 - }) + } } - + /** Gets the current average FPS. */ @@ -50,13 +50,13 @@ class FpsSampleCollector { let lastTimestamp = timestamps.last else { return 0.0 } - + let totalDurationMs = (lastTimestamp - firstTimestamp) / 1_000_000 let averageFrameDurationMs = Double(totalDurationMs) / Double(timestamps.count - 1) - + return 1000.0 / averageFrameDurationMs } - + protocol Delegate { func onAverageFpsChanged(averageFps: Double) }