title: Swift Combine custom Publisher: Storing a reference to an array of Subscribers?
body: <p>I'm trying to write a custom Combine <code>Publisher</code> that will send decibel and timestamps from an <code>AVAudioEngine</code> tap. After going through numerous tutorials and the WWDC videos, I <em>still</em> can't find an example of how a <code>Publisher</code> keeps track of the <code>Subscriber</code>s that have subscribed to it.</p>
  
  <pre><code>public typealias AudioVolume = Double
  
  public struct AudioVolumePublisher: Publisher {
      public typealias Output = AudioVolume
      public typealias Failure = Error
  }
  
  public class AudioVolumeSubscription&lt;S: Subscriber&gt;: 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
      }
  }
  
  </code></pre>
  
  <p>I assume that the <code>AudioVolumePublisher</code> should store a list of its active subscribers, but adding a property like</p>
  
  <pre><code>var subscribers = [S]()
  </code></pre>
  
  <p>won't compile because <code>Subscriber</code> has associated types. Is this even the right approach to handling <code>Subscriber</code>s, and if so, what's the best way to store them? Is type erasure my only practical option?</p>