@@ -45,7 +45,9 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
4545 private let crucialUpdateInterval : TimeInterval
4646 private let isActive : Bool
4747 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
4951 private var lastUpdateTime : Date ?
5052
5153 public nonisolated init (
@@ -112,17 +114,38 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
112114
113115 nonisolated public func healthCheck( ) {
114116 Task { @HealthCheckActor in
115- guard isActive else { return }
117+ guard canPerformHealthCheck else { return }
116118 lastUpdateTime = . now
117119 updateHealthCheckTimerSubscription ( )
118- healthCheckInternal ( )
120+
121+ Task {
122+ await healthCheckInternal ( )
123+ guard Task . isCancelled else { return }
124+ performHealthCheckWhenBecomeActive = true
125+ } . store ( in: & healthCheckSubscriptions)
119126 }
120127 }
121128
122- open func healthCheckInternal( ) { }
129+ open func healthCheckInternal( ) async { }
123130}
124131
125132private 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+
126149 func configure( nodes: AnyObservable < [ Node ] > , connection: AnyObservable < Bool > ) {
127150 let connection = connection
128151 . removeDuplicates ( )
@@ -149,7 +172,7 @@ private extension HealthCheckWrapper {
149172
150173 NotificationCenter . default
151174 . notifications ( named: UIApplication . willResignActiveNotification, object: nil )
152- . sink { @HealthCheckActor [ weak self] _ in self ? . previousAppState = . background }
175+ . sink { @HealthCheckActor [ weak self] _ in self ? . willResignActiveAction ( ) }
153176 . store ( in: & subscriptions)
154177 }
155178
@@ -172,17 +195,22 @@ private extension HealthCheckWrapper {
172195 }
173196
174197 func didBecomeActiveAction( ) {
175- defer { previousAppState = . active }
198+ guard appState != . active else { return }
199+ appState = . active
176200
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
182203
204+ guard performHealthCheckWhenBecomeActive || Date . now >= timeToUpdate else { return }
205+ performHealthCheckWhenBecomeActive = false
183206 healthCheck ( )
184207 }
185208
209+ func willResignActiveAction( ) {
210+ appState = . background
211+ healthCheckSubscriptions = . init( )
212+ }
213+
186214 func updateNodes( _ newNodes: [ Node ] ) {
187215 nodes = newNodes
188216 updateSortedNodes ( )
0 commit comments