@@ -16,19 +16,28 @@ import Foundation
16
16
17
17
/// An object that provides API to log and flush heartbeats from a synchronized storage container.
18
18
public final class HeartbeatController {
19
+ /// Used for standardizing dates for calendar-day comparison.
20
+ private enum DateStandardizer {
21
+ private static let calendar : Calendar = {
22
+ var calendar = Calendar ( identifier: . iso8601)
23
+ calendar. locale = Locale ( identifier: " en_US_POSIX " )
24
+ calendar. timeZone = TimeZone ( secondsFromGMT: 0 ) !
25
+ return calendar
26
+ } ( )
27
+
28
+ static func standardize( _ date: Date ) -> ( Date ) {
29
+ return calendar. startOfDay ( for: date)
30
+ }
31
+ }
32
+
19
33
/// The thread-safe storage object to log and flush heartbeats from.
20
34
private let storage : HeartbeatStorageProtocol
21
35
/// The max capacity of heartbeats to store in storage.
22
36
private let heartbeatsStorageCapacity : Int = 30
23
37
/// Current date provider. It is used for testability.
24
38
private let dateProvider : ( ) -> Date
25
39
/// Used for standardizing dates for calendar-day comparison.
26
- static let dateStandardizer : ( Date ) -> ( Date ) = {
27
- var calendar = Calendar ( identifier: . iso8601)
28
- calendar. locale = Locale ( identifier: " en_US_POSIX " )
29
- calendar. timeZone = TimeZone ( secondsFromGMT: 0 ) !
30
- return calendar. startOfDay ( for: )
31
- } ( )
40
+ private static let dateStandardizer = DateStandardizer . self
32
41
33
42
/// Public initializer.
34
43
/// - Parameter id: The `id` to associate this controller's heartbeat storage with.
@@ -54,7 +63,7 @@ public final class HeartbeatController {
54
63
init ( storage: HeartbeatStorageProtocol ,
55
64
dateProvider: @escaping ( ) -> Date = Date . init) {
56
65
self . storage = storage
57
- self . dateProvider = { Self . dateStandardizer ( dateProvider ( ) ) }
66
+ self . dateProvider = { Self . dateStandardizer. standardize ( dateProvider ( ) ) }
58
67
}
59
68
60
69
/// Asynchronously logs a new heartbeat, if needed.
0 commit comments