Skip to content

Commit 9042fcb

Browse files
committed
.every(_:perform:) disconnect on view disappear
1 parent f22edbc commit 9042fcb

File tree

1 file changed

+8
-3
lines changed
  • Sources/ShinySwiftUI/Extensions/SwiftUI/View

1 file changed

+8
-3
lines changed

Sources/ShinySwiftUI/Extensions/SwiftUI/View/View.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,14 @@ public extension View {
308308
- parameter action: The action to perform.
309309
*/
310310
func every(_ interval: TimeInterval, perform action: @escaping () -> Void) -> some View {
311-
self.onReceive(Timer.publish(every: interval, on: .main, in: .common).autoconnect()) { _ in
312-
action()
313-
}
311+
let timer = Timer.publish(every: interval, on: .main, in: .common).autoconnect()
312+
return self
313+
.onReceive(timer) { _ in
314+
action()
315+
}
316+
.onDisappear {
317+
timer.upstream.connect().cancel()
318+
}
314319
}
315320

316321
/**

0 commit comments

Comments
 (0)