Skip to content

Instantly share code, notes, and snippets.

@T1T4N
Created June 22, 2024 10:14
Show Gist options
  • Save T1T4N/92568c69ca247226705656dc879fa88e to your computer and use it in GitHub Desktop.
Save T1T4N/92568c69ca247226705656dc879fa88e to your computer and use it in GitHub Desktop.
Combine extensions for weak assignment and sink
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