Skip to content

Instantly share code, notes, and snippets.

@Obbut
Created January 12, 2021 19:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Obbut/b270369eb8134708d4092d86d2163ee4 to your computer and use it in GitHub Desktop.
Save Obbut/b270369eb8134708d4092d86d2163ee4 to your computer and use it in GitHub Desktop.
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