apollographql / graphql-subscriptions

:newspaper: A small module that implements GraphQL subscriptions for Node.js
MIT License
1.58k stars 133 forks source link

Can the pubsub-async-iterator properties and methods be protected instead of private? #271

Open jisaacks opened 6 months ago

jisaacks commented 6 months ago

I am trying to make a version of the pubsub async iterator that throws an error if the pushQueue goes over a certain size. Right now it is difficult to extend the existing pubsub async iterator to add this because most things are set to private and cannot be accessed by an overriding class.

Right now I am having to do this:

class MaxQueuePubSubAsyncIterator<T> extends PubSubAsyncIterator<T> {
  constructor(pubsub: PubSubEngine, eventNames: string | string[]) {
    super(pubsub, eventNames)
    // pushValue and pushQueue are private so this is a hack to access/override them.
    const super_pushValue = this["pushValue"] as ((event:T) => Promise<void>)
    this["pushValue"] = async (event: T) => {
      if ((this["pushQueue"] as T[]).length >= MAX_QUEUE) {
        throw new MaxQueueError('Maximum Queue Size Reached')
      }
      return await super_pushValue(event)
    }
  }
}

However if pushValue and pushQueue were marked as protected instead of private, they would still be inaccessible in normal usage but could be accessed from extending classes and simplify this code quite a bit:

class MaxQueuePubSubAsyncIterator<T> extends PubSubAsyncIterator<T> {
  override async pushValue(event: T) {
    if (this.pushQueue.length >= MAX_QUEUE) {
      throw new MaxQueueError('Maximum Queue Size Reached')
    }
    return super.pushValue(event)
  }
}