Created
June 22, 2024 10:14
-
-
Save T1T4N/92568c69ca247226705656dc879fa88e to your computer and use it in GitHub Desktop.
Combine extensions for weak assignment and sink
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 where Failure == Never { | |
func weakAssign<T: AnyObject>( | |
to keyPath: ReferenceWritableKeyPath<T, Output>, on object: T | |
) -> AnyCancellable { | |
sink { [weak object] value in | |
object?[keyPath: keyPath] = value | |
} | |
} | |
func weakSink<T: AnyObject>( | |
weaklyCaptured: T, | |
receiveValue: @escaping (T, Self.Output) -> Void | |
) -> AnyCancellable { | |
sink { [weak weaklyCaptured] output in | |
guard let strongRef = weaklyCaptured else { return } | |
receiveValue(strongRef, output) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment