yattee/Model/MenuModel.swift
Toni Förster 74b6adb247
fix some potential crashes
Signed-off-by: Toni Förster <toni.foerster@gmail.com>
2024-08-25 23:41:06 +02:00

28 lines
847 B
Swift

import Combine
import Foundation
final class MenuModel: ObservableObject {
static let shared = MenuModel()
private var cancellables = Set<AnyCancellable>()
init() {
registerChildModel(AccountsModel.shared)
registerChildModel(NavigationModel.shared)
registerChildModel(PlayerModel.shared)
}
func registerChildModel<T: ObservableObject>(_ model: T?) {
guard let model else {
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)
}
}