I'm using the Mirror API to access the underlying publisher of properties wrapped with @Published. For some reason, when setting a published property, values aren't received by the publisher I'm accessing after the initial value.
In the following code, I would expect the "mirrored" publisher to receive events for eternity.
import Combine
class Dinosaur: ObservableObject {
@Published var angry: Bool = false
}
let raptor = Dinosaur()
//let subscription1 = raptor.$angry
// .print("NORMAL")
// .sink { value in }
var published = Mirror(reflecting: raptor).children.first!.value as! Published<Bool>
let publisher = published.projectedValue
let subscription2 = publisher
.print("MIRRORED")
.sink { value in }
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in
raptor.angry = !raptor.angry
}
But, it only has the following output.
MIRRORED: receive subscription: (PublishedSubject)
MIRRORED: request unlimited
MIRRORED: receive value: (false)
Interestingly, if the let subscription1 = ... line is uncommented, both sinks receive each successive value successfully.
NORMAL: receive subscription: (PublishedSubject)
NORMAL: request unlimited
NORMAL: receive value: (false)
MIRRORED: receive subscription: (PublishedSubject)
MIRRORED: request unlimited
MIRRORED: receive value: (false)
MIRRORED: receive value: (true)
NORMAL: receive value: (true)
MIRRORED: receive value: (false)
NORMAL: receive value: (false)
... etc
It's almost like the compiler has to "see" the magic raptor.$angry to set up a proper subscription.
What am I doing wrong?