I have a CurrentValueSubject in an object and I want another CurrentValueSubject to copy the changes of the first one.
I can't find a way of easily doing that without having to do something like this:
subjectA.assign(to: \.value, on: subjectB).store(in: &cancelables)
It would be nice to be able to initialize the subjectB using subjectA. Is there a way of doing something like that?
Edit: For example I was thinking it would be great to be able to do something like:
let subjectB = CurrentValueSubject(from: subjectA)
I would just have the second publisher be a subscriber to the first publisher. Example:
let pub1 = CurrentValueSubject<String,Never>("howdy")
lazy var pub2 : AnyPublisher<String,Never> = {
self.pub1.eraseToAnyPublisher()
}()
Example of usage:
pub1.sink {print($0)}.store(in: &storage)
pub2.sink {print($0)}.store(in: &storage)
delay(1) {
self.pub1.send("hey")
delay(2) {
self.pub1.send("ho")
}
}
Output:
howdy
howdy
[delay]
hey
hey
[delay]
ho
ho
So both publishers are giving out the same value, which seems to be what you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With