30 lines
730 B
Swift
30 lines
730 B
Swift
import Adwaita
|
|
import Foundation
|
|
|
|
@propertyWrapper
|
|
public struct Injected<T> {
|
|
|
|
private let keyPath: KeyPath<InjectionValues, T?>
|
|
private let observerRef: ObserverRef
|
|
|
|
public var wrappedValue: T {
|
|
DIContainer.shared.resolve(keyPath)
|
|
}
|
|
|
|
public init(_ keyPath: KeyPath<InjectionValues, T?>) {
|
|
self.keyPath = keyPath
|
|
self.observerRef = ObserverRef(
|
|
id: DIContainer.shared.addObserver(for: keyPath) {
|
|
Task { @MainActor in
|
|
StateManager.updateViews()
|
|
}
|
|
}
|
|
)
|
|
}
|
|
}
|
|
|
|
private final class ObserverRef {
|
|
let id: UUID
|
|
init(id: UUID) { self.id = id }
|
|
deinit { DIContainer.shared.removeObserver(id) }
|
|
}
|