diff --git a/Sources/CombineViewModel/ViewModel.swift b/Sources/CombineViewModel/ViewModel.swift index 75239fd..bc8ac9a 100644 --- a/Sources/CombineViewModel/ViewModel.swift +++ b/Sources/CombineViewModel/ViewModel.swift @@ -32,35 +32,27 @@ public struct ViewModel { observer[keyPath: storageKeyPath].object = newValue - var observations: AnyPublisher<(Object, Observer?), Never>! - - let checkObserverReady: (Object) -> (Object, Observer?) = { [weak observer] object in - if let observer = observer, observer.isReadyForUpdates { - return (object, observer) - } else { - return (object, nil) - } - } + var objectDidChange: AnyPublisher<(), Never>! #if canImport(UIKit) if let viewController = observer as? UIViewController { dispatchPrecondition(condition: .onQueue(.main)) _combinevm_hook_viewDidLoad(viewController) - observations = newValue.observe(on: DispatchQueue.main) - .combineLatest(viewController.viewDidLoadPublisher) { object, _ in checkObserverReady(object) } + objectDidChange = newValue.observe(on: DispatchQueue.main) + .combineLatest(viewController.viewDidLoadPublisher) { _, _ in } .eraseToAnyPublisher() } #endif - if observations == nil { - observations = newValue.observe(on: DispatchQueue.main) - .map(checkObserverReady) + if objectDidChange == nil { + objectDidChange = newValue.observe(on: DispatchQueue.main) + .map { _ in } .eraseToAnyPublisher() } - observations - .sink { _, observer in observer?.updateView() } + objectDidChange + .sink { [weak observer] in observer?.updateView() } .store(in: &observer.subscriptions) } } diff --git a/Sources/CombineViewModel/ViewModelObserver.swift b/Sources/CombineViewModel/ViewModelObserver.swift index b3533fe..91a899f 100644 --- a/Sources/CombineViewModel/ViewModelObserver.swift +++ b/Sources/CombineViewModel/ViewModelObserver.swift @@ -1,23 +1,6 @@ import Combine public protocol ViewModelObserver: AnyObject { - var isReadyForUpdates: Bool { get } var subscriptions: Set { get set } func updateView() } - -extension ViewModelObserver { - public var isReadyForUpdates: Bool { - true - } -} - -#if canImport(UIKit) -import UIKit - -extension UIViewController { - @objc open var isReadyForUpdates: Bool { - isViewLoaded - } -} -#endif