Created
April 27, 2023 09:48
-
-
Save woodycatliu/02380a5709576d6e21527371912bf0c3 to your computer and use it in GitHub Desktop.
Combine.Publisher.Filter extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Publisher { | |
public func filterTo(_ isIncluded: @escaping (Self.Output) -> Bool) -> AnyPublisher<Output, Failure> { | |
self.filter(isIncluded).eraseToAnyPublisher() | |
} | |
public func filterTo(equalTo value: Output) -> AnyPublisher<Output, Failure> where Output: AnyObject&Equatable { | |
return self.filterTo { [weak value] output in | |
value == output | |
} | |
} | |
public func filterTo<Value: AnyObject&Equatable>(equalTo value: Value, _ keyPath: KeyPath<Output, Value>) -> AnyPublisher<Output, Failure> { | |
let filtor: (Output) -> Bool = { [weak value] output in | |
return output[keyPath: keyPath] == value | |
} | |
return self.filter(filtor).eraseToAnyPublisher() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment