@@ -45,7 +45,9 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
45
45
private let crucialUpdateInterval : TimeInterval
46
46
private let isActive : Bool
47
47
private var healthCheckTimerSubscription : AnyCancellable ?
48
- private var previousAppState : UIApplication . State ?
48
+ private var healthCheckSubscriptions = Set < AnyCancellable > ( )
49
+ private var appState : AppState = . active
50
+ private var performHealthCheckWhenBecomeActive = false
49
51
private var lastUpdateTime : Date ?
50
52
51
53
public nonisolated init (
@@ -112,17 +114,38 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
112
114
113
115
nonisolated public func healthCheck( ) {
114
116
Task { @HealthCheckActor in
115
- guard isActive else { return }
117
+ guard canPerformHealthCheck else { return }
116
118
lastUpdateTime = . now
117
119
updateHealthCheckTimerSubscription ( )
118
- healthCheckInternal ( )
120
+
121
+ Task {
122
+ await healthCheckInternal ( )
123
+ guard Task . isCancelled else { return }
124
+ performHealthCheckWhenBecomeActive = true
125
+ } . store ( in: & healthCheckSubscriptions)
119
126
}
120
127
}
121
128
122
- open func healthCheckInternal( ) { }
129
+ open func healthCheckInternal( ) async { }
123
130
}
124
131
125
132
private extension HealthCheckWrapper {
133
+ private enum AppState {
134
+ case active
135
+ case background
136
+ }
137
+
138
+ var canPerformHealthCheck : Bool {
139
+ guard isActive else { return false }
140
+
141
+ switch appState {
142
+ case . active:
143
+ return true
144
+ case . background:
145
+ return false
146
+ }
147
+ }
148
+
126
149
func configure( nodes: AnyObservable < [ Node ] > , connection: AnyObservable < Bool > ) {
127
150
let connection = connection
128
151
. removeDuplicates ( )
@@ -149,7 +172,7 @@ private extension HealthCheckWrapper {
149
172
150
173
NotificationCenter . default
151
174
. notifications ( named: UIApplication . willResignActiveNotification, object: nil )
152
- . sink { @HealthCheckActor [ weak self] _ in self ? . previousAppState = . background }
175
+ . sink { @HealthCheckActor [ weak self] _ in self ? . willResignActiveAction ( ) }
153
176
. store ( in: & subscriptions)
154
177
}
155
178
@@ -172,17 +195,22 @@ private extension HealthCheckWrapper {
172
195
}
173
196
174
197
func didBecomeActiveAction( ) {
175
- defer { previousAppState = . active }
198
+ guard appState != . active else { return }
199
+ appState = . active
176
200
177
- guard
178
- previousAppState == . background,
179
- let timeToUpdate = lastUpdateTime? . addingTimeInterval ( normalUpdateInterval / 3 ) ,
180
- Date . now > timeToUpdate
181
- else { return }
201
+ let timeToUpdate = lastUpdateTime? . addingTimeInterval ( normalUpdateInterval / 3 )
202
+ ?? . adamantNullDate
182
203
204
+ guard performHealthCheckWhenBecomeActive || Date . now >= timeToUpdate else { return }
205
+ performHealthCheckWhenBecomeActive = false
183
206
healthCheck ( )
184
207
}
185
208
209
+ func willResignActiveAction( ) {
210
+ appState = . background
211
+ healthCheckSubscriptions = . init( )
212
+ }
213
+
186
214
func updateNodes( _ newNodes: [ Node ] ) {
187
215
nodes = newNodes
188
216
updateSortedNodes ( )
0 commit comments