yattee/Model/MenuModel.swift

28 lines
847 B
Swift
Raw Normal View History

2021-11-08 23:14:28 +00:00
import Combine
import Foundation
final class MenuModel: ObservableObject {
static let shared = MenuModel()
private var cancellables = Set<AnyCancellable>()
2021-11-08 23:14:28 +00:00
init() {
registerChildModel(AccountsModel.shared)
registerChildModel(NavigationModel.shared)
registerChildModel(PlayerModel.shared)
}
2021-11-08 23:14:28 +00:00
func registerChildModel<T: ObservableObject>(_ model: T?) {
guard let model else {
2021-11-08 23:14:28 +00:00
return
}
model.objectWillChange
.receive(on: DispatchQueue.main) // Ensure the update occurs on the main thread
.debounce(for: .milliseconds(10), scheduler: DispatchQueue.main) // Debounce to avoid immediate feedback loops
.sink { [weak self] _ in
self?.objectWillChange.send()
}
.store(in: &cancellables)
2021-11-08 23:14:28 +00:00
}
}