@@ -12,7 +12,7 @@ import LoopCore
12
12
import LoopAlgorithm
13
13
14
14
protocol CGMStalenessMonitorDelegate : AnyObject {
15
- func getLatestCGMGlucose( since: Date , completion : @escaping ( _ result : Swift . Result < StoredGlucoseSample ? , Error > ) -> Void )
15
+ func getLatestCGMGlucose( since: Date ) async throws -> StoredGlucoseSample ?
16
16
}
17
17
18
18
class CGMStalenessMonitor {
@@ -21,13 +21,7 @@ class CGMStalenessMonitor {
21
21
22
22
private var cgmStalenessTimer : Timer ?
23
23
24
- weak var delegate : CGMStalenessMonitorDelegate ? = nil {
25
- didSet {
26
- if delegate != nil {
27
- checkCGMStaleness ( )
28
- }
29
- }
30
- }
24
+ weak var delegate : CGMStalenessMonitorDelegate ?
31
25
32
26
@Published var cgmDataIsStale : Bool = true {
33
27
didSet {
@@ -57,29 +51,27 @@ class CGMStalenessMonitor {
57
51
cgmStalenessTimer? . invalidate ( )
58
52
cgmStalenessTimer = Timer . scheduledTimer ( withTimeInterval: expiration. timeIntervalSinceNow, repeats: false ) { [ weak self] _ in
59
53
self ? . log. debug ( " cgmStalenessTimer fired " )
60
- self ? . checkCGMStaleness ( )
54
+ Task {
55
+ await self ? . checkCGMStaleness ( )
56
+ }
61
57
}
62
58
cgmStalenessTimer? . tolerance = CGMStalenessMonitor . cgmStalenessTimerTolerance
63
59
}
64
60
65
- private func checkCGMStaleness( ) {
66
- delegate? . getLatestCGMGlucose ( since: Date ( timeIntervalSinceNow: - LoopAlgorithm. inputDataRecencyInterval) ) { ( result) in
67
- DispatchQueue . main. async {
68
- self . log. debug ( " Fetched latest CGM Glucose for checkCGMStaleness: %{public}@ " , String ( describing: result) )
69
- switch result {
70
- case . success( let sample) :
71
- if let sample = sample {
72
- self . cgmDataIsStale = false
73
- self . updateCGMStalenessTimer ( expiration: sample. startDate. addingTimeInterval ( LoopAlgorithm . inputDataRecencyInterval + CGMStalenessMonitor. cgmStalenessTimerTolerance) )
74
- } else {
75
- self . cgmDataIsStale = true
76
- }
77
- case . failure( let error) :
78
- self . log. error ( " Unable to get latest CGM clucose: %{public}@ " , String ( describing: error) )
79
- // Some kind of system error; check again in 5 minutes
80
- self . updateCGMStalenessTimer ( expiration: Date ( timeIntervalSinceNow: . minutes( 5 ) ) )
81
- }
61
+ func checkCGMStaleness( ) async {
62
+ do {
63
+ let sample = try await delegate? . getLatestCGMGlucose ( since: Date ( timeIntervalSinceNow: - LoopAlgorithm. inputDataRecencyInterval) )
64
+ self . log. debug ( " Fetched latest CGM Glucose for checkCGMStaleness: %{public}@ " , String ( describing: sample) )
65
+ if let sample = sample {
66
+ self . cgmDataIsStale = false
67
+ self . updateCGMStalenessTimer ( expiration: sample. startDate. addingTimeInterval ( LoopAlgorithm . inputDataRecencyInterval + CGMStalenessMonitor. cgmStalenessTimerTolerance) )
68
+ } else {
69
+ self . cgmDataIsStale = true
82
70
}
71
+ } catch {
72
+ self . log. error ( " Unable to get latest CGM clucose: %{public}@ " , String ( describing: error) )
73
+ // Some kind of system error; check again in 5 minutes
74
+ self . updateCGMStalenessTimer ( expiration: Date ( timeIntervalSinceNow: . minutes( 5 ) ) )
83
75
}
84
76
}
85
77
}
0 commit comments