34 lines
809 B
Swift
34 lines
809 B
Swift
|
//
|
||
|
// OptionalObservedObject.swift
|
||
|
// ComposeUI
|
||
|
//
|
||
|
// Created by Shadowfacts on 4/15/23.
|
||
|
//
|
||
|
|
||
|
import SwiftUI
|
||
|
import Combine
|
||
|
|
||
|
@propertyWrapper
|
||
|
struct OptionalObservedObject<T: ObservableObject>: DynamicProperty {
|
||
|
private class Republisher: ObservableObject {
|
||
|
var cancellable: AnyCancellable?
|
||
|
var wrapped: T? {
|
||
|
didSet {
|
||
|
cancellable?.cancel()
|
||
|
cancellable = wrapped?.objectWillChange
|
||
|
.receive(on: RunLoop.main)
|
||
|
.sink { [unowned self] _ in
|
||
|
self.objectWillChange.send()
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@StateObject private var republisher = Republisher()
|
||
|
var wrappedValue: T?
|
||
|
|
||
|
func update() {
|
||
|
republisher.wrapped = wrappedValue
|
||
|
}
|
||
|
}
|