import Combine

protocol ChangeObservableItem: ObservableObject {

    var cancellables: Set<AnyCancellable> { get set }

    func needsSaving()
}

extension ChangeObservableItem {

    func observeChanges() {
        objectWillChange
            .sink { [weak self] _ in
                self?.needsSaving()
            }
            .store(in: &cancellables)
    }

    func observe<T>(_ object: T) where T: ObservableObject {
        object.objectWillChange
            .sink { [weak self] _ in
                self?.needsSaving()
            }
            .store(in: &cancellables)
    }
}

protocol ObservableContentItem: ChangeObservableItem {

    var content: Content { get }
}

extension ObservableContentItem {

    func needsSaving() {
        content.needsSave()
    }
}
