Skip to content

Commit ea0c6b3

Browse files
Invalidation helpers
1 parent f941001 commit ea0c6b3

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Created by Axel Ancona Esselmann on 12/3/24.
2+
//
3+
4+
import Combine
5+
6+
public extension IDedLoadableViewModel {
7+
func invalidate<P: Publisher>(on publisher: P) -> AnyCancellable
8+
where P.Failure == Never, P.Output == ID
9+
{
10+
publisher
11+
.eraseToAnyPublisher()
12+
.filter { [weak self] observedId in
13+
guard let id = self?.id else {
14+
return false
15+
}
16+
return observedId == id
17+
}
18+
.map { _ in return}
19+
.sink { [weak self] in
20+
self?.invalidate()
21+
}
22+
}
23+
}
24+
25+
public extension BaseLoadableViewModel {
26+
func invalidate<P: Publisher>(on publisher: P) -> AnyCancellable
27+
where P.Failure == Never, P.Output == Void
28+
{
29+
publisher
30+
.eraseToAnyPublisher()
31+
.sink { [weak self] in
32+
self?.invalidate()
33+
}
34+
}
35+
}

Sources/LoadableView/Protocols/LoadableView/BaseLoadableViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public protocol BaseLoadableViewModel: ObservableObject, AnyObject {
2929
func onDisappear()
3030
func setError(_ error: Error?)
3131
func setIsLoading(_ isLoading: Bool)
32+
func invalidate()
3233

3334
func refresh(showLoading: Bool)
3435
func refresh(on changePublisher: AnyPublisher<ObservationType, Never>, showLoading: Bool) -> AnyCancellable

0 commit comments

Comments
 (0)