I'm trying to write a custom publisher that generates some values. Something like this:
class MyPublisher: Publisher {
typealias Output = Int
typealias Failure = Never
private let subject = PassThroughSubject<Int, Never>()
func receive<S>(subscriber: S) where S: Subscriber, S.Failure == Failure, S.Input == Output {
subject.receive(subscriber: subscriber)
startSending()
}
func startSending() {
subject.send(1)
subject.send(2)
subject.send(3)
subject.send(completion: .finished)
}
}
I'm trying to figure out how to call startSending()
automatically after a subscribing attaches, but I'm not sure if I'm doing it right.
I've just been reading about ConnectablePublisher
and was wondering if that might help, but I'm not sure how.
Has anyone tried something like this? How did you do it?