32 lines
725 B
Swift
32 lines
725 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()
|
|
observerRef.id = DIContainer.shared.addObserver(for: keyPath) {
|
|
Task { @MainActor in
|
|
StateManager.updateViews()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private final class ObserverRef {
|
|
var id: UUID?
|
|
deinit {
|
|
if let id {
|
|
DIContainer.shared.removeObserver(id)
|
|
}
|
|
}
|
|
}
|