1515import Foundation
1616
1717/// An object that provides API to log and flush heartbeats from a synchronized storage container.
18- public final class HeartbeatController {
18+ public final class HeartbeatController : Sendable {
1919 /// Used for standardizing dates for calendar-day comparison.
2020 private enum DateStandardizer {
2121 private static let calendar : Calendar = {
@@ -31,11 +31,11 @@ public final class HeartbeatController {
3131 }
3232
3333 /// The thread-safe storage object to log and flush heartbeats from.
34- private let storage : HeartbeatStorageProtocol
34+ private let storage : any HeartbeatStorageProtocol
3535 /// The max capacity of heartbeats to store in storage.
36- private let heartbeatsStorageCapacity : Int = 30
36+ private static let heartbeatsStorageCapacity : Int = 30
3737 /// Current date provider. It is used for testability.
38- private let dateProvider : ( ) -> Date
38+ private let dateProvider : @ Sendable ( ) -> Date
3939 /// Used for standardizing dates for calendar-day comparison.
4040 private static let dateStandardizer = DateStandardizer . self
4141
@@ -51,7 +51,7 @@ public final class HeartbeatController {
5151 /// - Parameters:
5252 /// - id: The id to associate this controller's heartbeat storage with.
5353 /// - dateProvider: A date provider.
54- convenience init ( id: String , dateProvider: @escaping ( ) -> Date ) {
54+ convenience init ( id: String , dateProvider: @escaping @ Sendable ( ) -> Date ) {
5555 let storage = HeartbeatStorage . getInstance ( id: id)
5656 self . init ( storage: storage, dateProvider: dateProvider)
5757 }
@@ -61,7 +61,7 @@ public final class HeartbeatController {
6161 /// - storage: A heartbeat storage container.
6262 /// - dateProvider: A date provider. Defaults to providing the current date.
6363 init ( storage: HeartbeatStorageProtocol ,
64- dateProvider: @escaping ( ) -> Date = Date . init) {
64+ dateProvider: @escaping @ Sendable ( ) -> Date = Date . init) {
6565 self . storage = storage
6666 self . dateProvider = { Self . dateStandardizer. standardize ( dateProvider ( ) ) }
6767 }
@@ -76,7 +76,7 @@ public final class HeartbeatController {
7676
7777 storage. readAndWriteAsync { heartbeatsBundle in
7878 var heartbeatsBundle = heartbeatsBundle ??
79- HeartbeatsBundle ( capacity: self . heartbeatsStorageCapacity)
79+ HeartbeatsBundle ( capacity: Self . heartbeatsStorageCapacity)
8080
8181 // Filter for the time periods where the last heartbeat to be logged for
8282 // that time period was logged more than one time period (i.e. day) ago.
@@ -109,7 +109,7 @@ public final class HeartbeatController {
109109 // The new value that's stored will use the old's cache to prevent the
110110 // logging of duplicates after flushing.
111111 return HeartbeatsBundle (
112- capacity: self . heartbeatsStorageCapacity,
112+ capacity: Self . heartbeatsStorageCapacity,
113113 cache: oldHeartbeatsBundle. lastAddedHeartbeatDates
114114 )
115115 }
@@ -129,14 +129,14 @@ public final class HeartbeatController {
129129 @available ( iOS 13 . 0 , macOS 10 . 15 , macCatalyst 13 . 0 , tvOS 13 . 0 , watchOS 6 . 0 , * )
130130 public func flushAsync( ) async -> HeartbeatsPayload {
131131 return await withCheckedContinuation { continuation in
132- let resetTransform = { ( heartbeatsBundle: HeartbeatsBundle ? ) -> HeartbeatsBundle ? in
132+ let resetTransform = { @ Sendable ( heartbeatsBundle: HeartbeatsBundle ? ) -> HeartbeatsBundle ? in
133133 guard let oldHeartbeatsBundle = heartbeatsBundle else {
134134 return nil // Storage was empty.
135135 }
136136 // The new value that's stored will use the old's cache to prevent the
137137 // logging of duplicates after flushing.
138138 return HeartbeatsBundle (
139- capacity: self . heartbeatsStorageCapacity,
139+ capacity: Self . heartbeatsStorageCapacity,
140140 cache: oldHeartbeatsBundle. lastAddedHeartbeatDates
141141 )
142142 }
0 commit comments