I'm trying to write a custom Combine Publisher
that will send decibel and timestamps from an AVAudioEngine
tap. After going through numerous tutorials and the WWDC videos, I still can't find an example of how a Publisher
keeps track of the Subscriber
s that have subscribed to it.
public typealias AudioVolume = Double
public struct AudioVolumePublisher: Publisher {
public typealias Output = AudioVolume
public typealias Failure = Error
}
public class AudioVolumeSubscription<S: Subscriber>: NSObject, Subscription {
private var subscriber: S?
public var combineIdentifier = CombineIdentifier()
public init(for subscriber: S) {
self.subscriber = subscriber
}
public func request(_ demand: Subscribers.Demand) {
...
}
public func cancel() {
subscriber = nil
}
}
I assume that the AudioVolumePublisher
should store a list of its active subscribers, but adding a property like
var subscribers = [S]()
won't compile because Subscriber
has associated types. Is this even the right approach to handling Subscriber
s, and if so, what's the best way to store them? Is type erasure my only practical option?