Created
January 12, 2021 19:11
-
-
Save Obbut/b270369eb8134708d4092d86d2163ee4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Combine | |
import Foundation | |
@available(iOS 13.0, macOS 10.15, tvOS 13, watchOS 6, *) | |
public struct ObjectDidChangePublisher<ObjectWillChangePublisher: Publisher>: Publisher { | |
private let upstream: ObjectWillChangePublisher | |
public typealias Output = ObjectWillChangePublisher.Output | |
public typealias Failure = ObjectWillChangePublisher.Failure | |
fileprivate init(upstream: ObjectWillChangePublisher) { | |
self.upstream = upstream | |
} | |
public func receive<S>(subscriber: S) where S : Subscriber, Self.Failure == S.Failure, Self.Output == S.Input { | |
self.upstream | |
.receive(on: DispatchQueue.main) /// This essentially just delays the publisher until after the `willSet` property observer. Published property changes must happen on the main thread anyway. | |
.receive(subscriber: subscriber) | |
} | |
} | |
@available(iOS 13.0, macOS 10.15, tvOS 13, watchOS 6, *) | |
extension ObservableObject { | |
public var objectDidChange: ObjectDidChangePublisher<ObjectWillChangePublisher> { | |
ObjectDidChangePublisher(upstream: objectWillChange) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment